BRL-CAD
density.c
Go to the documentation of this file.
1 /* T E S T _ D E N S I T Y . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-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 
23 #include <sys/stat.h>
24 #include "analyze.h"
25 
26 int
27 main(int argc, char **argv)
28 {
29  struct density_entry *densities = NULL;
30  static int num_densities = 1028;
31 
32  struct stat sb;
33  FILE *fp = (FILE *)NULL;
34  char *buf = NULL;
35  int ret = 0;
36  int i;
37 
38  if (argc < 2) {
39  bu_log("Error - please supply density file\n");
40  bu_exit(EXIT_FAILURE, NULL);
41  }
42 
43  fp = fopen(argv[1], "rb");
44  if (fp == (FILE *)NULL) {
45  bu_log("Error - file %s not opened\n", argv[1]);
46  bu_exit(EXIT_FAILURE, NULL);
47  }
48 
49  if (stat(argv[1], &sb)) {
50  bu_log("Error - file %s not stat successfully\n", argv[1]);
51  bu_exit(EXIT_FAILURE, NULL);
52  }
53 
54  buf = (char *)bu_malloc(sb.st_size+1, "density buffer");
55  ret = fread(buf, sb.st_size, 1, fp);
56  if (ret != 1) {
57  bu_log("Error reading file %s\n", argv[1]);
58  bu_exit(EXIT_FAILURE, NULL);
59  }
60 
61  densities = (struct density_entry *)bu_calloc(num_densities, sizeof(struct density_entry), "density entries");
62 
63  ret = parse_densities_buffer(buf, (unsigned long)sb.st_size, densities, NULL, &num_densities);
64 
65  for (i = 0; i < num_densities; i++) {
66  if (densities[i].name)
67  bu_log("densities[%i]: %s, %f\n", i, densities[i].name, densities[i].grams_per_cu_mm);
68  }
69 
70  return 0;
71 }
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int main(int argc, char **argv)
Definition: density.c:27
Definition: clone.c:90
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
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
struct density_entry * densities
Definition: gqa.c:167
Definition: analyze.h:58