BRL-CAD
prefix.c
Go to the documentation of this file.
1 /* P R E F I X . 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/prefix.c
21  *
22  * The prefix 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 static void
36 prefix_do(struct db_i *dbip, struct rt_comb_internal *UNUSED(comb), union tree *comb_leaf, void *prefix_ptr, void *obj_ptr, void *UNUSED(user_ptr3), void *UNUSED(user_ptr4))
37 {
38  char *prefix, *obj;
39  char tempstring_v4[NAMESIZE+1];
40  size_t len = NAMESIZE+1;
41 
42  RT_CK_DBI(dbip);
43  RT_CK_TREE(comb_leaf);
44 
45  prefix = (char *)prefix_ptr;
46  obj = (char *)obj_ptr;
47 
48  if (!BU_STR_EQUAL(comb_leaf->tr_l.tl_name, obj))
49  return;
50 
51  bu_free(comb_leaf->tr_l.tl_name, "comb_leaf->tr_l.tl_name");
52  if (db_version(dbip) < 5) {
53  bu_strlcpy(tempstring_v4, prefix, len);
54  bu_strlcat(tempstring_v4, obj, len);
55  comb_leaf->tr_l.tl_name = bu_strdup(tempstring_v4);
56  } else {
57  len = strlen(prefix)+strlen(obj)+1;
58  comb_leaf->tr_l.tl_name = (char *)bu_malloc(len, "Adding prefix");
59 
60  bu_strlcpy(comb_leaf->tr_l.tl_name , prefix, len);
61  bu_strlcat(comb_leaf->tr_l.tl_name , obj, len);
62  }
63 }
64 
65 
66 int
67 ged_prefix(struct ged *gedp, int argc, const char *argv[])
68 {
69  int i, k;
70  struct directory *dp;
71  struct rt_db_internal intern;
72  struct rt_comb_internal *comb;
73  char tempstring_v4[NAMESIZE+1];
74  struct bu_vls tempstring_v5 = BU_VLS_INIT_ZERO;
75  char *tempstring;
76  int len = NAMESIZE+1;
77  static const char *usage = "new_prefix object(s)";
78 
81  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
82 
83  /* initialize result */
84  bu_vls_trunc(gedp->ged_result_str, 0);
85 
86  /* must be wanting help */
87  if (argc == 1) {
88  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
89  return GED_HELP;
90  }
91 
92  if (argc < 3) {
93  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
94  return GED_ERROR;
95  }
96 
97  bu_log("!!! ged_prefix: step 1\n");
98 
99  /* First, check validity, and change node names */
100  for (i = 2; i < argc; i++) {
101  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) == RT_DIR_NULL) {
102  argv[i] = "";
103  continue;
104  }
105 
106  if (db_version(gedp->ged_wdbp->dbip) < 5 && (int)(strlen(argv[1]) + strlen(argv[i])) > NAMESIZE) {
107  bu_vls_printf(gedp->ged_result_str, "'%s%s' too long, must be %d characters or less.\n",
108  argv[1], argv[i], NAMESIZE);
109 
110  argv[i] = "";
111  continue;
112  }
113 
114  if (db_version(gedp->ged_wdbp->dbip) < 5) {
115  bu_strlcpy(tempstring_v4, argv[1], len);
116  bu_strlcat(tempstring_v4, argv[i], len);
117  tempstring = tempstring_v4;
118  } else {
119  bu_vls_trunc(&tempstring_v5, 0);
120  bu_vls_strcpy(&tempstring_v5, argv[1]);
121  bu_vls_strcat(&tempstring_v5, argv[i]);
122  tempstring = bu_vls_addr(&tempstring_v5);
123  }
124 
125  if (db_lookup(gedp->ged_wdbp->dbip, tempstring, LOOKUP_QUIET) != RT_DIR_NULL) {
126  bu_vls_printf(gedp->ged_result_str, "%s: already exists\n", tempstring);
127  argv[i] = "";
128  continue;
129  }
130 
131  /* Change object name in the directory. */
132  if (db_rename(gedp->ged_wdbp->dbip, dp, tempstring) < 0) {
133  bu_vls_free(&tempstring_v5);
134  bu_vls_printf(gedp->ged_result_str, "error in rename to %s, aborting\n", tempstring);
135  return GED_ERROR;
136  }
137 
138  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
139  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
140  return GED_ERROR;
141  }
142 
143  /* Change object name on disk. */
144  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource)) {
145  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting");
146  return GED_ERROR;
147  }
148  bu_log("XXXged_prefix: changed name from %s to %s\n", argv[i], tempstring);
149  }
150 
151  bu_vls_free(&tempstring_v5);
152 
153  /* Examine all COMB nodes */
155  if (!(dp->d_flags & RT_DIR_COMB))
156  continue;
157 
158  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
159  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
160  return GED_ERROR;
161  }
162  comb = (struct rt_comb_internal *)intern.idb_ptr;
163 
164  for (k = 2; k < argc; k++)
165  db_tree_funcleaf(gedp->ged_wdbp->dbip, comb, comb->tree, prefix_do,
166  (void *)argv[1], (void *)argv[k], (void *)NULL, (void *)NULL);
167  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource)) {
168  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting");
169  return GED_ERROR;
170  }
172 
173  return GED_OK;
174 }
175 
176 
177 /*
178  * Local Variables:
179  * tab-width: 8
180  * mode: C
181  * indent-tabs-mode: t
182  * c-file-style: "stroustrup"
183  * End:
184  * ex: shiftwidth=4 tabstop=8
185  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: raytrace.h:800
#define FOR_ALL_DIRECTORY_START(_dp, _dbip)
Definition: raytrace.h:895
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
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
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
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
int db_rename(struct db_i *, struct directory *, const char *newname)
Definition: db_lookup.c:302
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
#define FOR_ALL_DIRECTORY_END
Definition: raytrace.h:899
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int ged_prefix(struct ged *gedp, int argc, const char *argv[])
Definition: prefix.c:67
#define GED_ERROR
Definition: ged.h:61
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
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 LOOKUP_QUIET
Definition: raytrace.h:893
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
#define UNUSED(parameter)
Definition: common.h:239
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
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_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define RT_CK_TREE(_p)
Definition: raytrace.h:1182
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
Definition: vls.h:56
double fastf_t
Definition: defines.h:300
#define bu_strlcat(dst, src, size)
Definition: str.h:50
#define bu_strdup(s)
Definition: str.h:71
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126