BRL-CAD
open.c
Go to the documentation of this file.
1 /* O P E N . 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/open.c
21  *
22  * The open command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_reopen(struct ged *gedp, int argc, const char *argv[])
37 {
38  struct db_i *new_dbip;
39  static const char *usage = "[filename]";
40 
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  /* get database filename */
48  if (argc == 1) {
50  return GED_OK;
51  }
52 
53  /* set database filename */
54  if (argc == 2) {
55  char *av[2];
56  struct db_i *old_dbip = gedp->ged_wdbp->dbip;
57  struct mater *old_materp = rt_material_head();
58  struct mater *new_materp;
59 
60  rt_new_material_head(MATER_NULL);
61 
62  if ((new_dbip = _ged_open_dbip(argv[1], 0)) == DBI_NULL) {
63  /* Restore RT's material head */
64  rt_new_material_head(old_materp);
65 
66  bu_vls_printf(gedp->ged_result_str, "ged_reopen: failed to open %s\n", argv[1]);
67  return GED_ERROR;
68  }
69 
70  new_materp = rt_material_head();
71 
72  gedp->ged_wdbp->dbip = old_dbip;
73  rt_new_material_head(old_materp);
74 
75  av[0] = "zap";
76  av[1] = (char *)0;
77  ged_zap(gedp, 1, (const char **)av);
78 
79  /* close current database */
80  db_close(gedp->ged_wdbp->dbip);
81 
82  gedp->ged_wdbp->dbip = new_dbip;
83  rt_new_material_head(new_materp);
84 
86  return GED_OK;
87  }
88 
89  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
90  return GED_ERROR;
91 }
92 
93 
94 /*
95  * Local Variables:
96  * tab-width: 8
97  * mode: C
98  * indent-tabs-mode: t
99  * c-file-style: "stroustrup"
100  * End:
101  * ex: shiftwidth=4 tabstop=8
102  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: raytrace.h:800
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
struct db_i * _ged_open_dbip(const char *filename, int existing_only)
Open/Create the database and build the in memory directory.
Definition: ged.c:420
#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 GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
void rt_new_material_head(struct mater *newmat)
Definition: mater.c:253
void db_close(struct db_i *dbip)
int ged_zap(struct ged *gedp, int argc, const char *argv[])
Definition: zap.c:42
struct bu_vls * ged_result_str
Definition: ged.h:357
struct mater * rt_material_head(void)
Definition: mater.c:243
#define DBI_NULL
Definition: raytrace.h:827
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
int ged_reopen(struct ged *gedp, int argc, const char *argv[])
Definition: open.c:36
char * dbi_filename
file name
Definition: raytrace.h:805