BRL-CAD
rmap.c
Go to the documentation of this file.
1 /* R M A P . 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/rmap.c
21  *
22  * The rmap command.
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_rmap(struct ged *gedp, int argc, const char *argv[])
37 {
38  int i;
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 
47  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
48 
49  /* initialize result */
50  bu_vls_trunc(gedp->ged_result_str, 0);
51 
52  if (argc != 1) {
53  bu_vls_printf(gedp->ged_result_str, "Usage: %s", argv[0]);
54  return GED_ERROR;
55  }
56 
57  if (db_version(gedp->ged_wdbp->dbip) < 5) {
58  bu_vls_printf(gedp->ged_result_str, "%s is not available prior to version 5 of the .g file format\n", argv[0]);
59  return GED_ERROR;
60  }
61 
62  BU_LIST_INIT(&headIdName.l);
63 
64  /* For all regions not hidden */
65  for (i = 0; i < RT_DBNHASH; i++) {
66  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
67  int found = 0;
68 
69  if (!(dp->d_flags & RT_DIR_REGION) ||
70  (dp->d_flags & RT_DIR_HIDDEN))
71  continue;
72 
73  if (rt_db_get_internal(&intern,
74  dp,
75  gedp->ged_wdbp->dbip,
76  (fastf_t *)NULL,
77  &rt_uniresource) < 0) {
78  bu_vls_printf(gedp->ged_result_str, "%s: Database read error, aborting", argv[0]);
79  return GED_ERROR;
80  }
81 
82  comb = (struct rt_comb_internal *)intern.idb_ptr;
83  /* check to see if the region id or air code matches one in our list */
84  for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) {
85  if ((comb->region_id == itnp->id) ||
86  (comb->aircode != 0 && -comb->aircode == itnp->id)) {
87  /* add region name to our name list for this region */
88  BU_GET(inp, struct _ged_id_names);
89  bu_vls_init(&inp->name);
90  bu_vls_strcpy(&inp->name, dp->d_namep);
91  BU_LIST_INSERT(&itnp->headName.l, &inp->l);
92  found = 1;
93  break;
94  }
95  }
96 
97  if (!found) {
98  /* create new id_to_names node */
99  BU_GET(itnp, struct _ged_id_to_names);
100  if (0 < comb->region_id)
101  itnp->id = comb->region_id;
102  else
103  itnp->id = -comb->aircode;
104  BU_LIST_INSERT(&headIdName.l, &itnp->l);
105  BU_LIST_INIT(&itnp->headName.l);
106 
107  /* add region name to our name list for this region */
108  BU_GET(inp, struct _ged_id_names);
109  bu_vls_init(&inp->name);
110  bu_vls_strcpy(&inp->name, dp->d_namep);
111  BU_LIST_INSERT(&itnp->headName.l, &inp->l);
112  }
113 
114  rt_db_free_internal(&intern);
115  }
116  }
117 
118  /* place data in the result string */
119  while (BU_LIST_WHILE(itnp, _ged_id_to_names, &headIdName.l)) {
120 
121  /* add this id to the list */
122  bu_vls_printf(gedp->ged_result_str, "%d {", itnp->id);
123 
124  /* start sublist of names associated with this id */
125  while (BU_LIST_WHILE(inp, _ged_id_names, &itnp->headName.l)) {
126  /* add the this name to this sublist */
127  bu_vls_printf(gedp->ged_result_str, " %s", bu_vls_addr(&inp->name));
128 
129  BU_LIST_DEQUEUE(&inp->l);
130  bu_vls_free(&inp->name);
131  BU_PUT(inp, struct _ged_id_names);
132  }
133  bu_vls_printf(gedp->ged_result_str, " } "); /* , itnp->id); */
134 
135  BU_LIST_DEQUEUE(&itnp->l);
136  BU_PUT(itnp, struct _ged_id_to_names);
137  }
138 
139  return GED_OK;
140 }
141 
142 
143 /*
144  * Local Variables:
145  * tab-width: 8
146  * mode: C
147  * indent-tabs-mode: t
148  * c-file-style: "stroustrup"
149  * End:
150  * ex: shiftwidth=4 tabstop=8
151  */
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
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.
#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 GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define RT_DIR_HIDDEN
object name is hidden
Definition: raytrace.h:886
int ged_rmap(struct ged *gedp, int argc, const char *argv[])
Definition: rmap.c:36
#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
#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
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