BRL-CAD
rcodes.c
Go to the documentation of this file.
1 /* R 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/rcodes.c
21  *
22  * The rcodes 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 int
36 ged_rcodes(struct ged *gedp, int argc, const char *argv[])
37 {
38  int item, air, mat, los;
39  size_t g_changed = 0;
40  int found_a_match = 0;
41  char name[RT_MAXLINE];
42  char line[RT_MAXLINE];
43  char *cp;
44  FILE *fp;
45  struct directory *dp;
46  struct rt_db_internal intern;
47  struct rt_comb_internal *comb;
48 
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  /* must be wanting help */
57  if (argc == 1) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s filename", argv[0]);
59  return GED_HELP;
60  }
61 
62  if (argc != 2) {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s filename", argv[0]);
64  return GED_ERROR;
65  }
66 
67  if ((fp = fopen(argv[1], "r")) == NULL) {
68  bu_vls_printf(gedp->ged_result_str, "%s: Failed to read file - %s", argv[0], argv[1]);
69  return GED_ERROR;
70  }
71 
72  while (bu_fgets(line, RT_MAXLINE, fp) != NULL) {
73  int changed;
74 
75  /* character and/or whitespace delimited numbers */
76  if (sscanf(line, "%d%*c%d%*c%d%*c%d%s", &item, &air, &mat, &los, name) != 5)
77  continue; /* not useful */
78 
79  /* skip over the path */
80  if ((cp = strrchr(name, (int)'/')) == NULL)
81  cp = name;
82  else
83  ++cp;
84 
85  if (*cp == '\0')
86  continue;
87 
88  if ((dp = db_lookup(gedp->ged_wdbp->dbip, cp, LOOKUP_NOISY)) == RT_DIR_NULL) {
89  bu_vls_printf(gedp->ged_result_str, "%s: Warning - %s not found in database.\n", argv[1], cp);
90  continue;
91  }
92 
93  if (!(dp->d_flags & RT_DIR_REGION)) {
94  bu_vls_printf(gedp->ged_result_str, "%s: Warning - %s not a region\n", argv[1], cp);
95  continue;
96  }
97 
98  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (matp_t)NULL, &rt_uniresource) != ID_COMBINATION) {
99  bu_vls_printf(gedp->ged_result_str, "%s: Warning - %s not a region\n", argv[1], cp);
100  continue;
101  }
102 
103  /* By the time we make it here, we've got something */
104  found_a_match = 1;
105 
106  comb = (struct rt_comb_internal *)intern.idb_ptr;
107 
108  /* make the changes */
109  changed = 0;
110  if (comb->region_id != item) {
111  comb->region_id = item;
112  changed = 1;
113  }
114  if (comb->aircode != air) {
115  comb->aircode = air;
116  changed = 1;
117  }
118  if (comb->GIFTmater != mat) {
119  comb->GIFTmater = mat;
120  changed = 1;
121  }
122  if (comb->los != los) {
123  comb->los = los;
124  changed = 1;
125  }
126 
127  if (changed) {
128  /* write out all changes */
129  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource)) {
130  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting.\n");
132  "The in-memory table of contents may not match the status of the on-disk\ndatabase. The on-disk database should still be intact. For safety, \nyou should exit now, and resolve the I/O problem, before continuing.\n");
133 
134  rt_db_free_internal(&intern);
135  fclose(fp);
136  return GED_ERROR;
137  }
138  }
139  g_changed += (size_t)changed;
140 
141  }
142  fclose(fp);
143 
144  if (!found_a_match) {
145  bu_vls_printf(gedp->ged_result_str, "WARNING: rcodes file \"%s\" contained no matching lines. Geometry unchanged.\n", argv[1]);
146  }
147  if (g_changed) {
148  bu_vls_printf(gedp->ged_result_str, "NOTE: rcodes file \"%s\" applied. %d region%supdated.\n", argv[1], g_changed, (g_changed==1)?" ":"s ");
149  }
150 
151  return GED_OK;
152 }
153 
154 
155 /*
156  * Local Variables:
157  * tab-width: 8
158  * mode: C
159  * indent-tabs-mode: t
160  * c-file-style: "stroustrup"
161  * End:
162  * ex: shiftwidth=4 tabstop=8
163  */
#define GED_OK
Definition: ged.h:55
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
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
Definition: clone.c:90
#define RT_MAXLINE
Definition: raytrace.h:1255
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
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
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
#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
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_rcodes(struct ged *gedp, int argc, const char *argv[])
Definition: rcodes.c:36
void * idb_ptr
Definition: raytrace.h:195
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 * bu_fgets(char *s, int size, FILE *stream)
Definition: fgets.c:31
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216