BRL-CAD
which.c
Go to the documentation of this file.
1 /* W H I C H . 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/which.c
21  *
22  * The whichair and whichid commands.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_which(struct ged *gedp, int argc, const char *argv[])
37 {
38  int i, j;
39  struct directory *dp;
40  struct rt_db_internal intern;
41  struct rt_comb_internal *comb;
42  struct _ged_id_to_names headIdName;
43  struct _ged_id_to_names *itnp;
44  struct _ged_id_names *inp;
45  int isAir;
46  int sflag;
47  static const char *usageAir = "code(s)";
48  static const char *usageIds = "region_id(s)";
49 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /* initialize result */
54  bu_vls_trunc(gedp->ged_result_str, 0);
55 
56  if (BU_STR_EQUAL(argv[0], "whichair"))
57  isAir = 1;
58  else
59  isAir = 0;
60 
61  /* must be wanting help */
62  if (argc == 1) {
63  if (isAir)
64  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageAir);
65  else
66  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageIds);
67  return GED_HELP;
68  }
69 
70  if (BU_STR_EQUAL(argv[1], "-s")) {
71  --argc;
72  ++argv;
73 
74  if (argc < 2) {
75  if (isAir)
76  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageAir);
77  else
78  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageIds);
79 
80  return GED_ERROR;
81  }
82 
83  sflag = 1;
84  } else {
85  sflag = 0;
86  }
87 
88  BU_LIST_INIT(&headIdName.l);
89 
90  /* Build list of id_to_names */
91  for (j = 1; j < argc; j++) {
92  int n;
93  int start, end;
94  int range;
95  int k;
96 
97  n = sscanf(argv[j], "%d%*[:-]%d", &start, &end);
98  switch (n) {
99  case 1:
100  for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l))
101  if (itnp->id == start)
102  break;
103 
104  /* id not found */
105  if (BU_LIST_IS_HEAD(itnp, &headIdName.l)) {
106  BU_GET(itnp, struct _ged_id_to_names);
107  itnp->id = start;
108  BU_LIST_INSERT(&headIdName.l, &itnp->l);
109  BU_LIST_INIT(&itnp->headName.l);
110  }
111 
112  break;
113  case 2:
114  if (start < end)
115  range = end - start + 1;
116  else if (end < start) {
117  range = start - end + 1;
118  start = end;
119  } else
120  range = 1;
121 
122  for (k = 0; k < range; ++k) {
123  int id = start + k;
124 
125  for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l))
126  if (itnp->id == id)
127  break;
128 
129  /* id not found */
130  if (BU_LIST_IS_HEAD(itnp, &headIdName.l)) {
131  BU_GET(itnp, struct _ged_id_to_names);
132  itnp->id = id;
133  BU_LIST_INSERT(&headIdName.l, &itnp->l);
134  BU_LIST_INIT(&itnp->headName.l);
135  }
136  }
137 
138  break;
139  }
140  }
141 
142  /* Examine all COMB nodes */
143  for (i = 0; i < RT_DBNHASH; i++) {
144  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
145  if (!(dp->d_flags & RT_DIR_REGION))
146  continue;
147 
148  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
149  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
150  return GED_ERROR;
151  }
152  comb = (struct rt_comb_internal *)intern.idb_ptr;
153  /* check to see if the region id or air code matches one in our list */
154  for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) {
155  if ((!isAir && comb->region_id == itnp->id) ||
156  (isAir && comb->aircode == itnp->id)) {
157  /* add region name to our name list for this region */
158  BU_GET(inp, struct _ged_id_names);
159  bu_vls_init(&inp->name);
160  bu_vls_strcpy(&inp->name, dp->d_namep);
161  BU_LIST_INSERT(&itnp->headName.l, &inp->l);
162  break;
163  }
164  }
165 
166  rt_db_free_internal(&intern);
167  }
168  }
169 
170  /* place data in interp and free memory */
171  while (BU_LIST_WHILE(itnp, _ged_id_to_names, &headIdName.l)) {
172  if (!sflag) {
173  bu_vls_printf(gedp->ged_result_str, "Region[s] with %s %d:\n",
174  isAir ? "air code" : "ident", itnp->id);
175  }
176 
177  while (BU_LIST_WHILE(inp, _ged_id_names, &itnp->headName.l)) {
178  if (sflag)
179  bu_vls_printf(gedp->ged_result_str, " %s", bu_vls_addr(&inp->name));
180  else
181  bu_vls_printf(gedp->ged_result_str, " %s\n", bu_vls_addr(&inp->name));
182 
183  BU_LIST_DEQUEUE(&inp->l);
184  bu_vls_free(&inp->name);
185  BU_PUT(inp, struct _ged_id_names);
186  }
187 
188  BU_LIST_DEQUEUE(&itnp->l);
189  BU_PUT(itnp, struct _ged_id_to_names);
190  }
191 
192  return GED_OK;
193 }
194 
195 
196 /*
197  * Local Variables:
198  * tab-width: 8
199  * mode: C
200  * indent-tabs-mode: t
201  * c-file-style: "stroustrup"
202  * End:
203  * ex: shiftwidth=4 tabstop=8
204  */
void bu_vls_init(struct bu_vls *vp)
Definition: vls.c:56
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
#define BU_LIST_INSERT(old, new)
Definition: list.h:183
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
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition: raytrace.h:755
struct bu_list l
Definition: ged_private.h:89
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 rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define RT_DIR_REGION
region
Definition: raytrace.h:885
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
struct bu_list l
Definition: ged_private.h:95
struct bu_vls name
Definition: ged_private.h:90
#define GED_ERROR
Definition: ged.h:61
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define BU_LIST_IS_HEAD(p, hp)
Definition: list.h:322
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
#define BU_LIST_WHILE(p, structure, hp)
Definition: list.h:410
int ged_which(struct ged *gedp, int argc, const char *argv[])
Definition: which.c:36
#define BU_LIST_INIT(_hp)
Definition: list.h:148
void * idb_ptr
Definition: raytrace.h:195
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
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 GED_HELP
Definition: ged.h:62
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
struct _ged_id_names headName
Definition: ged_private.h:97
#define BU_LIST_DEQUEUE(cur)
Definition: list.h:209
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126