BRL-CAD
density.c
Go to the documentation of this file.
1 /* D E N S I T Y . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2009-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "analyze.h"
26 
27 int
28 parse_densities_buffer(char *buf, size_t len, struct density_entry *densities, struct bu_vls *result_str, int *num_densities)
29 {
30  char *p, *q, *last;
31  long idx;
32  double density;
33 
34  buf[len] = '\0';
35  last = &buf[len];
36 
37  p = buf;
38 
39  /* Skip initial whitespace */
40  while (*p && (*p == '\t' || *p == ' ' || *p == '\n')) p++;
41 
42  /* Skip initial comments */
43  while (*p == '#') {
44  /* Skip comment */
45  while (*p && *p != '\n') p++;
46  }
47 
48  /* Skip whitespace */
49  while (*p && (*p == '\t' || *p == ' ' || *p == '\n')) p++;
50 
51  while (*p) {
52  /* Skip comments */
53  if (*p == '#') {
54  /* Skip comment */
55  while (*p && *p != '\n') p++;
56 
57  /* Skip whitespace */
58  while (*p && (*p == '\t' || *p == ' ' || *p == '\n')) p++;
59 
60  continue;
61  }
62 
63  idx = strtol(p, &q, 10);
64  if (q == (char *)NULL) {
65  bu_vls_printf(result_str, "could not convert idx\n");
66  return ANALYZE_ERROR;
67  }
68 
69  if (idx < 0) {
70  bu_vls_printf(result_str, "bad density index (%ld < 0)\n", idx);
71  return ANALYZE_ERROR;
72  }
73 
74  density = strtod(q, &p);
75  if (q == p) {
76  bu_vls_printf(result_str, "could not convert density\n");
77  return ANALYZE_ERROR;
78  }
79 
80  if (density < 0.0) {
81  bu_vls_printf(result_str, "bad density (%lf < 0)\n", density);
82  return ANALYZE_ERROR;
83  }
84 
85  /* Skip tabs and spaces */
86  while (*p && (*p == '\t' || *p == ' ')) p++;
87  if (!*p)
88  break;
89 
90  q = strchr(p, '\n');
91  if (q)
92  *q++ = '\0';
93  else
94  q = last;
95 
96  while (idx >= *num_densities) {
97  densities = (struct density_entry *)bu_realloc(densities, sizeof(struct density_entry)*(*num_densities)*2,
98  "density entries");
99  *num_densities *= 2;
100  }
101 
102  densities[idx].magic = DENSITY_MAGIC;
103  /* since BRL-CAD does computation in mm, but the table is in
104  * grams / (cm^3) we convert the table on input
105  */
106  densities[idx].grams_per_cu_mm = density / 1000.0;
107  densities[idx].name = bu_strdup(p);
108 
109  p = q;
110 
111  /* Skip whitespace */
112  while (*p && (*p == '\t' || *p == ' ' || *p == '\n')) p++;
113  }
114 
115 #ifdef PRINT_DENSITIES
116  for (idx = 0; idx < &num_densities; idx++)
117  if (densities[idx].magic == DENSITY_MAGIC)
119  idx,
120  densities[idx].density,
121  densities[idx].name);
122 #endif
123 
124  return ANALYZE_OK;
125 }
126 
127 
128 /*
129  * Local Variables:
130  * tab-width: 8
131  * mode: C
132  * indent-tabs-mode: t
133  * c-file-style: "stroustrup"
134  * End:
135  * ex: shiftwidth=4 tabstop=8
136  */
#define DENSITY_MAGIC
Definition: analyze.h:55
int parse_densities_buffer(char *buf, size_t len, struct density_entry *densities, struct bu_vls *result_str, int *num_densities)
Definition: density.c:28
Header file for the BRL-CAD common definitions.
double grams_per_cu_mm
Definition: analyze.h:60
char * strchr(const char *sp, int c)
struct density_entry * densities
Definition: gqa.c:167
oldeumate l2 magic
Definition: nmg_mod.c:3843
void * bu_realloc(void *ptr, size_t siz, const char *str)
struct bu_vls * ged_result_str
Definition: ged.h:357
#define ANALYZE_OK
Definition: analyze.h:48
struct ged * _ged_current_gedp
Definition: loadview.c:37
#define ANALYZE_ERROR
Definition: analyze.h:49
uint32_t magic
Definition: analyze.h:59
char * name
Definition: analyze.h:61
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
Definition: analyze.h:58
Definition: vls.h:56
#define bu_strdup(s)
Definition: str.h:71