BRL-CAD
erase.c
Go to the documentation of this file.
1 /* E R A S E . 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/erase.c
21  *
22  * The erase command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 
32 
33 #include "./ged_private.h"
34 
35 /*
36  * Erase objects from the display.
37  *
38  */
39 int
40 ged_erase(struct ged *gedp, int argc, const char *argv[])
41 {
42  size_t i;
43  int flag_A_attr=0;
44  int flag_o_nonunique=1;
45  int last_opt=0;
46  struct bu_vls vls = BU_VLS_INIT_ZERO;
47  static const char *usage = "[[-r] | [[-o] -A attribute=value]] [object(s)]";
48  const char *cmdName = argv[0];
49 
52  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
53 
54  /* initialize result */
55  bu_vls_trunc(gedp->ged_result_str, 0);
56 
57  /* must be wanting help */
58  if (argc == 1) {
59  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmdName, usage);
60  return GED_HELP;
61  }
62 
63  /* skip past cmd */
64  --argc;
65  ++argv;
66 
67  /* check args for options */
68  for (i = 0; i < (size_t)argc; i++) {
69  /* Erase all and quit (ignore other options) */
70  char *ptr_A=NULL;
71  char *ptr_o=NULL;
72 
73  if (*argv[i] != '-')
74  break;
75 
76  if (strchr(argv[i], 'r')) {
77  for (i = 1; i < (size_t)argc; ++i)
79  return GED_OK;
80  }
81 
82  ptr_A=strchr(argv[i], 'A');
83  if (ptr_A)
84  flag_A_attr = 1;
85 
86  ptr_o=strchr(argv[i], 'o');
87  if (ptr_o)
88  flag_o_nonunique = 2;
89 
90  last_opt = i;
91 
92  if (!ptr_A && !ptr_o) {
93  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmdName, usage);
94  return GED_ERROR;
95  }
96 
97  if (strlen(argv[i]) == ((size_t)1 + (ptr_A != NULL) + (ptr_o != NULL))) {
98  /* argv[i] is just a "-A" or "-o" */
99  continue;
100  }
101 
102  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmdName, usage);
103  return GED_ERROR;
104  }
105 
106  if (flag_A_attr) {
107  /* args are attribute name/value pairs */
108  struct bu_attribute_value_set avs;
109  int max_count=0;
110  int remaining_args=0;
111  int new_argc=0;
112  char **new_argv=NULL;
113  struct bu_ptbl *tbl;
114 
115  remaining_args = argc - last_opt - 1;
116  if (remaining_args < 2 || remaining_args%2) {
117  bu_vls_printf(gedp->ged_result_str, "Error: must have even number of arguments (name/value pairs)\n");
118  bu_vls_free(&vls);
119  return GED_ERROR;
120  }
121 
122  bu_avs_init(&avs, (argc - last_opt)/2, "ged_erase avs");
123  i = 0;
124  while (i < (size_t)argc) {
125  if (*argv[i] == '-') {
126  i++;
127  continue;
128  }
129 
130  /* this is a name/value pair */
131  if (flag_o_nonunique == 2) {
132  bu_avs_add_nonunique(&avs, argv[i], argv[i+1]);
133  } else {
134  bu_avs_add(&avs, argv[i], argv[i+1]);
135  }
136  i += 2;
137  }
138 
139  tbl = db_lookup_by_attr(gedp->ged_wdbp->dbip, RT_DIR_REGION | RT_DIR_SOLID | RT_DIR_COMB, &avs, flag_o_nonunique);
140  bu_avs_free(&avs);
141  if (!tbl) {
142  bu_log("Error: db_lookup_by_attr() failed!!\n");
143  bu_vls_free(&vls);
144  return TCL_ERROR;
145  }
146  if (BU_PTBL_LEN(tbl) < 1) {
147  /* nothing matched, just return */
148  bu_vls_free(&vls);
149  return TCL_OK;
150  }
151  for (i = 0; i < BU_PTBL_LEN(tbl); i++) {
152  struct directory *dp;
153 
154  dp = (struct directory *)BU_PTBL_GET(tbl, i);
155  bu_vls_putc(&vls, ' ');
156  bu_vls_strcat(&vls, dp->d_namep);
157  }
158 
159  max_count = BU_PTBL_LEN(tbl) + last_opt + 1;
160  bu_ptbl_free(tbl);
161  bu_free((char *)tbl, "ged_erase ptbl");
162  new_argv = (char **)bu_calloc(max_count+1, sizeof(char *), "ged_erase new_argv");
163  new_argc = bu_argv_from_string(new_argv, max_count, bu_vls_addr(&vls));
164 
165  for (i = 0; i < (size_t)new_argc; ++i) {
166  /* Skip any options */
167  if (new_argv[i][0] == '-')
168  continue;
169 
170  dl_erasePathFromDisplay(gedp->ged_gdp->gd_headDisplay, gedp->ged_wdbp->dbip, gedp->ged_free_vlist_callback, new_argv[i], 1, gedp->freesolid);
171  }
172  } else {
173  for (i = 0; i < (size_t)argc; ++i)
175  }
176 
177  return GED_OK;
178 }
179 
180 
181 
182 /*
183  * Local Variables:
184  * tab-width: 8
185  * mode: C
186  * indent-tabs-mode: t
187  * c-file-style: "stroustrup"
188  * End:
189  * ex: shiftwidth=4 tabstop=8
190  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
void _dl_eraseAllPathsFromDisplay(struct bu_list *hdlp, struct db_i *dbip, void(*callback)(unsigned int, int), const char *path, const int skip_first, struct solid *freesolid)
Definition: display_list.c:437
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
int bu_avs_add(struct bu_attribute_value_set *avp, const char *attribute, const char *value)
Definition: avs.c:78
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
void(* ged_free_vlist_callback)(unsigned int, int)
function to call after freeing a vlist
Definition: ged.h:371
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#define GED_ERROR
Definition: ged.h:61
Definition: ptbl.h:62
struct solid * freesolid
Definition: ged.h:345
char * strchr(const char *sp, int c)
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define BU_PTBL_GET(ptbl, i)
Definition: ptbl.h:108
struct bu_ptbl * db_lookup_by_attr(struct db_i *dbip, int dir_flags, struct bu_attribute_value_set *avs, int op)
Definition: db_lookup.c:386
void bu_avs_add_nonunique(struct bu_attribute_value_set *avsp, const char *attribute, const char *value)
Definition: avs.c:287
void dl_erasePathFromDisplay(struct bu_list *hdlp, struct db_i *dbip, void(*callback)(unsigned int, int), const char *path, int allow_split, struct solid *freesolid)
Definition: display_list.c:204
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
void bu_avs_init(struct bu_attribute_value_set *avp, size_t len, const char *str)
Definition: avs.c:47
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
struct ged_drawable * ged_gdp
Definition: ged.h:360
#define BU_PTBL_LEN(ptbl)
Definition: ptbl.h:107
void bu_ptbl_free(struct bu_ptbl *b)
Definition: ptbl.c:226
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
size_t bu_argv_from_string(char *argv[], size_t lim, char *lp)
Definition: argv.c:32
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
int ged_erase(struct ged *gedp, int argc, const char *argv[])
Definition: erase.c:40
Definition: vls.h:56
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235