BRL-CAD
move.c
Go to the documentation of this file.
1 /* M O V 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/move.c
21  *
22  * The move command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "bu/cmd.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_move(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct display_list *gdlp;
40  struct directory *dp;
41  struct rt_db_internal intern;
42  static const char *usage = "from to";
43 
46  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
47 
48  /* initialize result */
49  bu_vls_trunc(gedp->ged_result_str, 0);
50 
51  /* must be wanting help */
52  if (argc == 1) {
53  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
54  return GED_HELP;
55  }
56 
57  if (argc != 3) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_ERROR;
60  }
61 
62  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL)
63  return GED_ERROR;
64 
65  if (db_lookup(gedp->ged_wdbp->dbip, argv[2], LOOKUP_QUIET) != RT_DIR_NULL) {
66  bu_vls_printf(gedp->ged_result_str, "%s: already exists", argv[2]);
67  return GED_ERROR;
68  }
69 
70  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
71  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
72  return GED_ERROR;
73  }
74 
75  /* Change object name in the in-memory directory. */
76  if (db_rename(gedp->ged_wdbp->dbip, dp, argv[2]) < 0) {
77  rt_db_free_internal(&intern);
78  bu_vls_printf(gedp->ged_result_str, "error in db_rename to %s, aborting", argv[2]);
79  return GED_ERROR;
80  }
81 
82  /* Re-write to the database. New name is applied on the way out. */
83  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource) < 0) {
84  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting");
85  return GED_ERROR;
86  }
87 
88  /* Change object name if it matches the first element in the display list path. */
89  for (BU_LIST_FOR(gdlp, display_list, gedp->ged_gdp->gd_headDisplay)) {
90  int first = 1;
91  int found = 0;
92  struct bu_vls new_path = BU_VLS_INIT_ZERO;
93  char *dupstr = strdup(bu_vls_addr(&gdlp->dl_path));
94  char *tok = strtok(dupstr, "/");
95 
96  while (tok) {
97  if (first) {
98  first = 0;
99 
100  if (BU_STR_EQUAL(tok, argv[1])) {
101  found = 1;
102  bu_vls_printf(&new_path, "%s", argv[2]);
103  } else
104  break; /* no need to go further */
105  } else
106  bu_vls_printf(&new_path, "/%s", tok);
107 
108  tok = strtok((char *)NULL, "/");
109  }
110 
111  if (found) {
112  bu_vls_free(&gdlp->dl_path);
113  bu_vls_printf(&gdlp->dl_path, "%s", bu_vls_addr(&new_path));
114  }
115 
116  free((void *)dupstr);
117  bu_vls_free(&new_path);
118  }
119 
120  return GED_OK;
121 }
122 
123 
124 /*
125  * Local Variables:
126  * tab-width: 8
127  * mode: C
128  * indent-tabs-mode: t
129  * c-file-style: "stroustrup"
130  * End:
131  * ex: shiftwidth=4 tabstop=8
132  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
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_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
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int ged_move(struct ged *gedp, int argc, const char *argv[])
Definition: move.c:37
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#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
#define LOOKUP_QUIET
Definition: raytrace.h:893
char * strtok(char *s, const char *delim)
struct bu_vls dl_path
Definition: bview.h:48
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
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 * strdup(const char *cp)
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126