BRL-CAD
put.c
Go to the documentation of this file.
1 /* P U T . 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/put.c
21  *
22  * The put 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.h"
33 
34 
35 int
36 ged_put(struct ged *gedp, int argc, const char *argv[])
37 {
38  struct rt_db_internal intern;
39  const struct rt_functab *ftp;
40  int i;
41  char *name;
42  char type[16];
43  static const char *usage = "object type attrs";
44 
47  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
48 
49  /* initialize result */
50  bu_vls_trunc(gedp->ged_result_str, 0);
51 
52  /* must be wanting help */
53  if (argc == 1) {
54  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
55  return GED_HELP;
56  }
57 
58  if (argc < 3) {
59  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
60  return GED_ERROR;
61  }
62 
63  name = (char *)argv[1];
64 
65  if (db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET) != RT_DIR_NULL) {
66  bu_vls_printf(gedp->ged_result_str, "%s already exists", argv[1]);
67  return GED_ERROR;
68  }
69 
70  RT_DB_INTERNAL_INIT(&intern);
71 
72  for (i = 0; argv[2][i] != 0 && i < 15; i++) {
73  type[i] = isupper((int)argv[2][i]) ? tolower((int)argv[2][i]) :
74  argv[2][i];
75  }
76  type[i] = 0;
77 
78  ftp = rt_get_functab_by_label(type);
79  if (ftp == NULL) {
80  bu_vls_printf(gedp->ged_result_str, "%s is an unknown object type.", type);
81  return GED_ERROR;
82  }
83 
84  RT_CK_FUNCTAB(ftp);
85 
86  if (ftp->ft_make) {
87  ftp->ft_make(ftp, &intern);
88  } else {
89  rt_generic_make(ftp, &intern);
90  }
91 
92  if (!ftp->ft_adjust || ftp->ft_adjust(gedp->ged_result_str, &intern, argc-3, argv+3) == GED_ERROR) {
93  rt_db_free_internal(&intern);
94  return GED_ERROR;
95  }
96 
97  if (wdb_put_internal(gedp->ged_wdbp, name, &intern, 1.0) < 0) {
98  bu_vls_printf(gedp->ged_result_str, "wdb_put_internal(%s)", argv[1]);
99  rt_db_free_internal(&intern);
100  return GED_ERROR;
101  }
102 
103  rt_db_free_internal(&intern);
104 
105  return GED_OK;
106 }
107 
108 
109 /*
110  * Local Variables:
111  * tab-width: 8
112  * mode: C
113  * indent-tabs-mode: t
114  * c-file-style: "stroustrup"
115  * End:
116  * ex: shiftwidth=4 tabstop=8
117  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define RT_CK_FUNCTAB(_p)
Definition: raytrace.h:2242
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
Definition: clone.c:90
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
int wdb_put_internal(struct rt_wdb *wdbp, const char *name, struct rt_db_internal *ip, double local2mm)
Definition: wdb.c:218
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
int ged_put(struct ged *gedp, int argc, const char *argv[])
Definition: put.c:36
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
struct bu_vls * ged_result_str
Definition: ged.h:357
void rt_generic_make(const struct rt_functab *ftp, struct rt_db_internal *intern)
Definition: generic.c:200
void(* ft_make)(const struct rt_functab *, struct rt_db_internal *)
Definition: raytrace.h:2189
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 GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int(* ft_adjust)(struct bu_vls *, struct rt_db_internal *, int, const char **)
Definition: raytrace.h:2183
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
const struct rt_functab * rt_get_functab_by_label(const char *label)
Definition: table.c:2410