BRL-CAD
make_name.c
Go to the documentation of this file.
1 /* M A K E _ N A M 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/make_name.c
21  *
22  * The make_name command.
23  *
24  */
25 
26 #include "common.h"
27 #include <string.h>
28 #include "ged.h"
29 
30 int
31 ged_make_name(struct ged *gedp, int argc, const char *argv[])
32 {
33  struct bu_vls obj_name = BU_VLS_INIT_ZERO;
34  char *cp, *tp;
35  static int i = 0;
36  int new_i;
37  int len;
38  static const char *usage = "template | -s [num]";
39 
42  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
43 
44  /* initialize result */
45  bu_vls_trunc(gedp->ged_result_str, 0);
46 
47  /* must be wanting help */
48  if (argc == 1) {
49  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
50  return GED_HELP;
51  }
52 
53  switch (argc) {
54  case 2:
55  if (!BU_STR_EQUAL(argv[1], "-s"))
56  break;
57 
58  i = 0;
59  return GED_OK;
60 
61  case 3:
62 
63  if ((BU_STR_EQUAL(argv[1], "-s"))
64  && (sscanf(argv[2], "%d", &new_i) == 1)) {
65  i = new_i;
66  return GED_OK;
67  }
68  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
69  return GED_ERROR;
70 
71  default:
72  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
73  return GED_ERROR;
74  }
75 
76  for (cp = (char *)argv[1], len = 0; *cp != '\0'; ++cp, ++len) {
77  if (*cp == '@') {
78  if (*(cp + 1) == '@')
79  ++cp;
80  else
81  break;
82  }
83  bu_vls_putc(&obj_name, *cp);
84  }
85  bu_vls_putc(&obj_name, '\0');
86  tp = (*cp == '\0') ? "" : cp + 1;
87 
88  do {
89  bu_vls_trunc(&obj_name, len);
90  bu_vls_printf(&obj_name, "%d", i++);
91  bu_vls_strcat(&obj_name, tp);
92  }
93  while (db_lookup(gedp->ged_wdbp->dbip, bu_vls_addr(&obj_name), LOOKUP_QUIET) != RT_DIR_NULL);
94 
95  bu_vls_printf(gedp->ged_result_str, "%s", bu_vls_addr(&obj_name));
96  bu_vls_free(&obj_name);
97 
98  return GED_OK;
99 }
100 
101 
102 /*
103  * Local Variables:
104  * tab-width: 8
105  * mode: C
106  * indent-tabs-mode: t
107  * c-file-style: "stroustrup"
108  * End:
109  * ex: shiftwidth=4 tabstop=8
110  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int ged_make_name(struct ged *gedp, int argc, const char *argv[])
Definition: make_name.c:31
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
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define LOOKUP_QUIET
Definition: raytrace.h:893
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
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 GED_HELP
Definition: ged.h:62
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126