BRL-CAD
rmater.c
Go to the documentation of this file.
1 /* R M A T E R . 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/rmater.c
21  *
22  * The rmater command.
23  *
24  */
25 
26 #include "ged.h"
27 
28 
29 static int
30 extract_mater_from_line(char *line,
31  char *name,
32  char *shader,
33  int *r,
34  int *g,
35  int *b,
36  int *override,
37  int *inherit)
38 {
39  int i, j, k;
40  char *str[3];
41 
42  str[0] = name;
43  str[1] = shader;
44 
45  /* Extract first 2 strings. */
46  for (i = j =0; i < 2; ++i) {
47 
48  /* skip white space */
49  while (line[j] == ' ' || line[j] == '\t')
50  ++j;
51 
52  if (line[j] == '\0')
53  return TCL_ERROR;
54 
55  /* We found a double quote, so use everything between the quotes */
56  if (line[j] == '"') {
57  for (k = 0, ++j; line[j] != '"' && line[j] != '\0'; ++j, ++k)
58  str[i][k] = line[j];
59  } else {
60  for (k = 0; line[j] != ' ' && line[j] != '\t' && line[j] != '\0'; ++j, ++k)
61  str[i][k] = line[j];
62  }
63 
64  if (line[j] == '\0')
65  return TCL_ERROR;
66 
67  str[i][k] = '\0';
68  ++j;
69  }
70 
71  /* character and/or whitespace delimited numbers */
72  if ((sscanf(line + j, "%d%*c%d%*c%d%*c%d%*c%d", r, g, b, override, inherit)) != 5)
73  return TCL_ERROR;
74 
75  return TCL_OK;
76 }
77 
78 
79 int
80 ged_rmater(struct ged *gedp, int argc, const char *argv[])
81 {
82 #ifndef LINELEN
83 #define LINELEN 256
84 #endif
85  int status = GED_OK;
86  FILE *fp;
87  struct directory *dp;
88  struct rt_db_internal intern;
89  struct rt_comb_internal *comb;
90  char line[LINELEN];
91  char name[128];
92  char shader[256];
93  int r, g, b;
94  int override;
95  int inherit;
96  static const char *usage = "filename";
97 
100  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
101 
102  /* initialize result */
103  bu_vls_trunc(gedp->ged_result_str, 0);
104 
105  /* must be wanting help */
106  if (argc == 1) {
107  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
108  return GED_HELP;
109  }
110 
111  if (argc != 2) {
112  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
113  return GED_ERROR;
114  }
115 
116  if ((fp = fopen(argv[1], "r")) == NULL) {
117  bu_vls_printf(gedp->ged_result_str, "ged_rmater: Failed to read file - %s", argv[1]);
118  return GED_ERROR;
119  }
120 
121  while (bu_fgets(line, LINELEN, fp) != NULL) {
122  if ((extract_mater_from_line(line, name, shader,
123  &r, &g, &b, &override, &inherit)) == GED_ERROR)
124  continue;
125 
126  if ((dp = db_lookup(gedp->ged_wdbp->dbip, name, LOOKUP_NOISY)) == RT_DIR_NULL) {
127  bu_vls_printf(gedp->ged_result_str, "ged_rmater: Failed to find %s\n", name);
128  status = GED_ERROR;
129  continue;
130  }
131 
132  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
133  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting\n");
134  status = GED_ERROR;
135  }
136  comb = (struct rt_comb_internal *)intern.idb_ptr;
137  RT_CK_COMB(comb);
138 
139  /* Assign new values */
140  if (shader[0] == '-')
141  bu_vls_free(&comb->shader);
142  else
143  bu_vls_strcpy(&comb->shader, shader);
144 
145  comb->rgb[0] = (unsigned char)r;
146  comb->rgb[1] = (unsigned char)g;
147  comb->rgb[2] = (unsigned char)b;
148  comb->rgb_valid = override;
149  comb->inherit = inherit;
150 
151  /* Write new values to database */
152  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource) < 0) {
153  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting\n");
154  status = GED_ERROR;
155  }
156  }
157 
158  (void)fclose(fp);
159  return status;
160 }
161 
162 
163 /*
164  * Local Variables:
165  * tab-width: 8
166  * mode: C
167  * indent-tabs-mode: t
168  * c-file-style: "stroustrup"
169  * End:
170  * ex: shiftwidth=4 tabstop=8
171  */
void usage(struct ged *gedp)
Definition: coil.c:315
#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
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
unsigned char rgb[3]
Definition: raytrace.h:948
#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
struct bu_vls shader
Definition: raytrace.h:950
#define LINELEN
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_rmater(struct ged *gedp, int argc, const char *argv[])
Definition: rmater.c:80
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
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
char rgb_valid
!0 ==> rgb[] has valid color
Definition: raytrace.h:947
ustring shader
char * bu_fgets(char *s, int size, FILE *stream)
Definition: fgets.c:31
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
double fastf_t
Definition: defines.h:300