BRL-CAD
bo.c
Go to the documentation of this file.
1 /* B O . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-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 /** @file libged/bo.c
21  *
22  * The 'bo' binary object command, used for importing and exporting
23  * between files and binary objects stored in a geometry database.
24  *
25  */
26 
27 #include "common.h"
28 
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <string.h>
32 
33 #include "bu/getopt.h"
34 #include "ged.h"
35 
36 
37 int
38 ged_bo(struct ged *gedp, int argc, const char *argv[])
39 {
40  int c;
41  unsigned int minor_type=0;
42  char *obj_name;
43  char *file_name;
44  int input_mode=0;
45  int output_mode=0;
46  struct rt_binunif_internal *bip;
47  struct rt_db_internal intern;
48  struct directory *dp;
49  const char *argv0;
50  static const char *usage = "{-i major_type minor_type | -o} dest source";
51 
54  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
55 
56  /* initialize result */
57  bu_vls_trunc(gedp->ged_result_str, 0);
58 
59  argv0 = argv[0];
60 
61  /* must be wanting help */
62  if (argc == 1) {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv0, usage);
64  return GED_HELP;
65  }
66 
67  /* check that we are using a version 5 database */
68  if (db_version(gedp->ged_wdbp->dbip) < 5) {
69  bu_vls_printf(gedp->ged_result_str, "This is an older database version.\nIt does not support binary objects.Use \"dbupgrade\" to upgrade this database to the current version.\n");
70  return GED_ERROR;
71  }
72 
73  bu_optind = 1; /* re-init bu_getopt() */
74  bu_opterr = 0; /* suppress bu_getopt()'s error message */
75  while ((c=bu_getopt(argc, (char * const *)argv, "iou:")) != -1) {
76  switch (c) {
77  case 'i':
78  input_mode = 1;
79  break;
80  case 'o':
81  output_mode = 1;
82  break;
83  default:
84  bu_vls_printf(gedp->ged_result_str, "Unrecognized option - %c", c);
85  return GED_ERROR;
86 
87  }
88  }
89 
90  if (input_mode + output_mode != 1) {
91  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv0, usage);
92  return GED_ERROR;
93  }
94 
95  argc -= bu_optind;
96  argv += bu_optind;
97 
98  if ((input_mode && argc != 4) || (output_mode && argc != 2)) {
99  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv0, usage);
100  return GED_ERROR;
101  }
102 
103 
104  if (input_mode) {
105  if (argv[0][0] == 'u') {
106 
107  if (argv[1][1] != '\0') {
108  bu_vls_printf(gedp->ged_result_str, "Unrecognized minor type: %s", argv[1]);
109  return GED_ERROR;
110  }
111 
112  switch ((int)argv[1][0]) {
113  case 'f':
114  minor_type = DB5_MINORTYPE_BINU_FLOAT;
115  break;
116  case 'd':
117  minor_type = DB5_MINORTYPE_BINU_DOUBLE;
118  break;
119  case 'c':
120  minor_type = DB5_MINORTYPE_BINU_8BITINT;
121  break;
122  case 's':
123  minor_type = DB5_MINORTYPE_BINU_16BITINT;
124  break;
125  case 'i':
126  minor_type = DB5_MINORTYPE_BINU_32BITINT;
127  break;
128  case 'l':
129  minor_type = DB5_MINORTYPE_BINU_64BITINT;
130  break;
131  case 'C':
132  minor_type = DB5_MINORTYPE_BINU_8BITINT_U;
133  break;
134  case 'S':
135  minor_type = DB5_MINORTYPE_BINU_16BITINT_U;
136  break;
137  case 'I':
138  minor_type = DB5_MINORTYPE_BINU_32BITINT_U;
139  break;
140  case 'L':
141  minor_type = DB5_MINORTYPE_BINU_64BITINT_U;
142  break;
143  default:
144  bu_vls_printf(gedp->ged_result_str, "Unrecognized minor type: %s", argv[1]);
145  return GED_ERROR;
146  }
147  } else {
148  bu_vls_printf(gedp->ged_result_str, "Unrecognized major type: %s", argv[0]);
149  return GED_ERROR;
150  }
151 
152  /* skip past major_type and minor_type */
153  argc -= 2;
154  argv += 2;
155 
156  if (minor_type == 0) {
157  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv0, usage);
158  return GED_ERROR;
159  }
160 
161  obj_name = (char *)*argv;
162  GED_CHECK_EXISTS(gedp, obj_name, LOOKUP_QUIET, GED_ERROR);
163 
164  argc--;
165  argv++;
166 
167  file_name = (char *)*argv;
168 
169  /* make a binunif of the entire file */
170  if (rt_mk_binunif (gedp->ged_wdbp, obj_name, file_name, minor_type, 0)) {
171  bu_vls_printf(gedp->ged_result_str, "Error creating %s", obj_name);
172  return GED_ERROR;
173  }
174 
175  } else if (output_mode) {
176  FILE *fp;
177 
178  file_name = (char *)*argv;
179 
180  argc--;
181  argv++;
182 
183  obj_name = (char *)*argv;
184 
185  if ((dp=db_lookup(gedp->ged_wdbp->dbip, obj_name, LOOKUP_NOISY)) == RT_DIR_NULL) {
186  return GED_ERROR;
187  }
188  if (!(dp->d_major_type & DB5_MAJORTYPE_BINARY_MASK)) {
189  bu_vls_printf(gedp->ged_result_str, "%s is not a binary object", obj_name);
190  return GED_ERROR;
191  }
192 
193  if (dp->d_major_type != DB5_MAJORTYPE_BINARY_UNIF) {
194  bu_vls_printf(gedp->ged_result_str, "source must be a uniform binary object");
195  return GED_ERROR;
196  }
197 
198  fp = fopen(file_name, "w+b");
199  if (fp == NULL) {
200  bu_vls_printf(gedp->ged_result_str, "Error: cannot open file %s for writing", file_name);
201  return GED_ERROR;
202  }
203 
204  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, NULL,
205  &rt_uniresource) < 0) {
206  bu_vls_printf(gedp->ged_result_str, "Error reading %s from database", dp->d_namep);
207  fclose(fp);
208  return GED_ERROR;
209  }
210 
211  RT_CK_DB_INTERNAL(&intern);
212 
213  bip = (struct rt_binunif_internal *)intern.idb_ptr;
214  if (bip->count < 1) {
215  bu_vls_printf(gedp->ged_result_str, "%s has no contents", obj_name);
216  fclose(fp);
217  rt_db_free_internal(&intern);
218  return GED_ERROR;
219  }
220 
221  if (fwrite(bip->u.int8, bip->count * db5_type_sizeof_h_binu(bip->type),
222  1, fp) != 1) {
223  bu_vls_printf(gedp->ged_result_str, "Error writing contents to file");
224  fclose(fp);
225  rt_db_free_internal(&intern);
226  return GED_ERROR;
227  }
228 
229  fclose(fp);
230  rt_db_free_internal(&intern);
231 
232  } else {
233  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv0, usage);
234  return GED_ERROR;
235  }
236 
237  return GED_OK;
238 }
239 
240 
241 /*
242  * Local Variables:
243  * tab-width: 8
244  * mode: C
245  * indent-tabs-mode: t
246  * c-file-style: "stroustrup"
247  * End:
248  * ex: shiftwidth=4 tabstop=8
249  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
unsigned char d_major_type
object major type
Definition: raytrace.h:870
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
union rt_binunif_internal::@9 u
#define GED_ERROR
Definition: ged.h:61
if(share_geom)
Definition: nmg_mod.c:3829
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define GED_CHECK_EXISTS(_gedp, _name, _noisy, _flags)
Definition: ged.h:171
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_bo(struct ged *gedp, int argc, const char *argv[])
Definition: bo.c:38
void * idb_ptr
Definition: raytrace.h:195
size_t db5_type_sizeof_h_binu(const int minor)
Definition: db5_types.c:228
int bu_opterr
Definition: globals.c:88
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define GED_HELP
Definition: ged.h:62
char * file_name
Definition: fb2pix.c:40
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int rt_mk_binunif(struct rt_wdb *wdbp, const char *obj_name, const char *file_name, unsigned int minor_type, size_t max_count)
Definition: binunif.c:55
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216