BRL-CAD
wcodes.c
Go to the documentation of this file.
1 /* W C O D E S . 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/wcodes.c
21  *
22  * The wcodes command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "./ged_private.h"
33 
34 
35 #define ABORTED -99
36 #define OLDSOLID 0
37 #define NEWSOLID 1
38 #define SOL_TABLE 1
39 #define REG_TABLE 2
40 #define ID_TABLE 3
41 
42 
43 #define PATH_STEP 256
44 static struct directory **path = NULL;
45 static size_t path_capacity = 0;
46 
47 
48 HIDDEN int wcodes_printcodes(struct ged *gedp, FILE *fp, struct directory *dp, size_t pathpos);
49 
50 
51 HIDDEN void
52 wcodes_printnode(struct db_i *dbip, struct rt_comb_internal *UNUSED(comb), union tree *comb_leaf, void *user_ptr1, void *user_ptr2, void *user_ptr3, void *UNUSED(user_ptr4))
53 {
54  FILE *fp;
55  size_t *pathpos;
56  struct directory *nextdp;
57  struct ged *gedp;
58 
59  RT_CK_DBI(dbip);
60  RT_CK_TREE(comb_leaf);
61 
62  if ((nextdp=db_lookup(dbip, comb_leaf->tr_l.tl_name, LOOKUP_NOISY)) == RT_DIR_NULL)
63  return;
64 
65  fp = (FILE *)user_ptr1;
66  pathpos = (size_t *)user_ptr2;
67  gedp = (struct ged *)user_ptr3;
68 
69  /* recurse on combinations */
70  if (nextdp->d_flags & RT_DIR_COMB)
71  (void)wcodes_printcodes(gedp, fp, nextdp, (*pathpos)+1);
72 }
73 
74 
75 HIDDEN int
76 wcodes_printcodes(struct ged *gedp, FILE *fp, struct directory *dp, size_t pathpos)
77 {
78  size_t i;
79  struct rt_db_internal intern;
80  struct rt_comb_internal *comb;
81  int id;
82 
83  if (!(dp->d_flags & RT_DIR_COMB))
84  return GED_OK;
85 
86  if ((id=rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (matp_t)NULL, &rt_uniresource)) < 0) {
87  bu_vls_printf(gedp->ged_result_str, "Cannot get records for %s\n", dp->d_namep);
88  return GED_ERROR;
89  }
90 
91  if (id != ID_COMBINATION) {
92  intern.idb_meth->ft_ifree(&intern);
93  return GED_OK;
94  }
95 
96  comb = (struct rt_comb_internal *)intern.idb_ptr;
97  RT_CK_COMB(comb);
98 
99  if (comb->region_flag) {
100  fprintf(fp, "%-6ld %-3ld %-3ld %-4ld ",
101  comb->region_id,
102  comb->aircode,
103  comb->GIFTmater,
104  comb->los);
105  for (i =0 ; i < pathpos; i++)
106  fprintf(fp, "/%s", path[i]->d_namep);
107  fprintf(fp, "/%s\n", dp->d_namep);
108  intern.idb_meth->ft_ifree(&intern);
109  return GED_OK;
110  }
111 
112  if (comb->tree) {
113  if (pathpos >= path_capacity) {
114  path_capacity += PATH_STEP;
115  path = (struct directory **)bu_realloc(path, sizeof(struct directory *) * path_capacity, "realloc path bigger");
116  }
117  path[pathpos] = dp;
118  db_tree_funcleaf(gedp->ged_wdbp->dbip, comb, comb->tree, wcodes_printnode,
119  (void *)fp, (void *)&pathpos, (void *)gedp, (void *)gedp);
120  }
121 
122  intern.idb_meth->ft_ifree(&intern);
123  return GED_OK;
124 }
125 
126 
127 int
128 ged_wcodes(struct ged *gedp, int argc, const char *argv[])
129 {
130  int i;
131  int status;
132  FILE *fp;
133  struct directory *dp;
134  static const char *usage = "filename object(s)";
135 
137  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
138 
139  /* initialize result */
140  bu_vls_trunc(gedp->ged_result_str, 0);
141 
142  /* must be wanting help */
143  if (argc == 1) {
144  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
145  return GED_HELP;
146  }
147 
148  if (argc == 2) {
149  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
150  return GED_ERROR;
151  }
152 
153  if ((fp = fopen(argv[1], "w")) == NULL) {
154  bu_vls_printf(gedp->ged_result_str, "%s: Failed to open file - %s",
155  argv[0], argv[1]);
156  return GED_ERROR;
157  }
158 
159  path = (struct directory **)bu_calloc(PATH_STEP, sizeof(struct directory *), "alloc initial path");
160  path_capacity = PATH_STEP;
161 
162  for (i = 2; i < argc; ++i) {
163  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) != RT_DIR_NULL) {
164  status = wcodes_printcodes(gedp, fp, dp, 0);
165 
166  if (status == GED_ERROR) {
167  (void)fclose(fp);
168  return GED_ERROR;
169  }
170  }
171  }
172 
173  (void)fclose(fp);
174  bu_free(path, "dealloc path");
175  path = NULL;
176  path_capacity = 0;
177 
178  return GED_OK;
179 }
180 
181 
182 /*
183  * Local Variables:
184  * tab-width: 8
185  * mode: C
186  * indent-tabs-mode: t
187  * c-file-style: "stroustrup"
188  * End:
189  * ex: shiftwidth=4 tabstop=8
190  */
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
Definition: raytrace.h:800
int ged_wcodes(struct ged *gedp, int argc, const char *argv[])
Definition: wcodes.c:128
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
char region_flag
!0 ==> this COMB is a REGION
Definition: raytrace.h:939
Definition: ged.h:338
void(* ft_ifree)(struct rt_db_internal *)
Definition: raytrace.h:2159
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
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
#define GED_ERROR
Definition: ged.h:61
#define HIDDEN
Definition: common.h:86
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
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
void * bu_realloc(void *ptr, size_t siz, const char *str)
#define UNUSED(parameter)
Definition: common.h:239
HIDDEN void wcodes_printnode(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_leaf, void *user_ptr1, void *user_ptr2, void *user_ptr3, void *user_ptr4)
Definition: wcodes.c:52
struct bu_vls * ged_result_str
Definition: ged.h:357
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
struct tree::tree_db_leaf tr_l
void * idb_ptr
Definition: raytrace.h:195
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
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
void db_tree_funcleaf(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_tree, void(*leaf_func)(struct db_i *, struct rt_comb_internal *, union tree *, void *, void *, void *, void *), void *user_ptr1, void *user_ptr2, void *user_ptr3, void *user_ptr4)
Definition: db_tree.c:640
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define RT_CK_TREE(_p)
Definition: raytrace.h:1182
int d_flags
flags
Definition: raytrace.h:869
HIDDEN int wcodes_printcodes(struct ged *gedp, FILE *fp, struct directory *dp, size_t pathpos)
Definition: wcodes.c:76
#define PATH_STEP
Definition: wcodes.c:43