BRL-CAD
pathsum.c
Go to the documentation of this file.
1 /* P A T H S U M . 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/pathsum.c
21  *
22  * The paths and listeval 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_pathsum(struct ged *gedp, int argc, const char *argv[])
37 {
38  int i, pos_in;
39  int verbose;
40  struct _ged_trace_data gtd;
41 
42  /* listeval */
43  static const char *usage1 =
44  "[-t] {path}\n{path} may be specified by '/' or space separated components, but not both";
45 
46  /* paths */
47  static const char *usage2 =
48  "{path_start}\n{path_start} may be specified by '/' or space separated components, but not both";
49 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /*
54  * paths are matched up to last input member
55  * ANY path the same up to this point is considered as matching
56  */
57 
58  /* initialize gtd */
59  memset((char *)(&gtd), 0, sizeof(struct _ged_trace_data));
60  gtd.gtd_gedp = gedp;
61  gtd.gtd_flag = _GED_CPEVAL;
62  gtd.gtd_prflag = 0;
63 
64  /* find out which command was entered */
65  if (BU_STR_EQUAL(argv[0], "listeval")) {
66  /* want to list evaluated solid[s] */
67  gtd.gtd_flag = _GED_LISTEVAL;
68  }
69  if (BU_STR_EQUAL(argv[0], "paths")) {
70  /* want to list all matching paths */
71  gtd.gtd_flag = _GED_LISTPATH;
72  }
73 
74  /* must be wanting help */
75  if (argc == 1) {
76  if (gtd.gtd_flag == _GED_LISTEVAL) {
77  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage1); /* listeval */
78  } else {
79  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage2); /* paths */
80  }
81  return GED_HELP;
82  }
83 
84  if (BU_STR_EQUAL(argv[1], "-t") && gtd.gtd_flag == _GED_LISTEVAL) {
85  pos_in = 2;
86  verbose = 0;
87  } else {
88  pos_in = 1;
89  verbose = 1;
90  }
91 
92  gtd.gtd_objpos = 0;
93  if (argc == (pos_in + 1) && strchr(argv[pos_in], '/')) {
94  char *tok;
95  tok = strtok((char *)argv[pos_in], "/");
96  while (tok) {
97  if ((gtd.gtd_obj[gtd.gtd_objpos++] = db_lookup(gedp->ged_wdbp->dbip, tok, LOOKUP_NOISY)) == RT_DIR_NULL) {
98  return GED_ERROR;
99  }
100  tok = strtok((char *)NULL, "/");
101  }
102  } else {
103  gtd.gtd_objpos = argc - pos_in;
104  /* build directory pointer array for desired path */
105  for (i = 0; i < gtd.gtd_objpos; i++) {
106  if ((gtd.gtd_obj[i] = db_lookup(gedp->ged_wdbp->dbip, argv[pos_in+i], LOOKUP_NOISY)) == RT_DIR_NULL) {
107  return GED_ERROR;
108  }
109  }
110  }
111 
112  if (!gtd.gtd_objpos) {
113  bu_vls_printf(gedp->ged_result_str, "Invalid path\n");
114  return GED_ERROR;
115  }
116 
117  MAT_IDN(gtd.gtd_xform);
118 
119  ged_trace(gtd.gtd_obj[0], 0, bn_mat_identity, &gtd, verbose);
120 
121  if (gtd.gtd_prflag == 0) {
122  /* path not found */
123  bu_vls_printf(gedp->ged_result_str, "PATH: ");
124  for (i = 0; i < gtd.gtd_objpos; i++) {
125  bu_vls_printf(gedp->ged_result_str, "/%s", gtd.gtd_obj[i]->d_namep);
126  }
127  bu_vls_printf(gedp->ged_result_str, " NOT FOUND\n");
128  }
129 
130  return GED_OK;
131 }
132 
133 
134 /*
135  * Local Variables:
136  * tab-width: 8
137  * mode: C
138  * indent-tabs-mode: t
139  * c-file-style: "stroustrup"
140  * End:
141  * ex: shiftwidth=4 tabstop=8
142  */
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
struct db_i * dbip
Definition: raytrace.h:1266
#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
char * strchr(const char *sp, int c)
void * memset(void *s, int c, size_t n)
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define _GED_CPEVAL
Definition: ged_private.h:52
char * strtok(char *s, const char *delim)
void ged_trace(struct directory *dp, int pathpos, const mat_t old_xlate, struct _ged_trace_data *gtdp, int verbose)
Definition: trace.c:82
struct ged * gtd_gedp
Definition: ged.h:1914
struct bu_vls * ged_result_str
Definition: ged.h:357
mat_t gtd_xform
Definition: ged.h:1917
#define _GED_LISTEVAL
Definition: ged_private.h:54
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
#define _GED_LISTPATH
Definition: ged_private.h:53
int gtd_flag
Definition: ged.h:1920
int ged_pathsum(struct ged *gedp, int argc, const char *argv[])
Definition: pathsum.c:36
HIDDEN void verbose(struct human_data_t *dude)
Definition: human.c:2008
int gtd_prflag
Definition: ged.h:1919
struct directory * gtd_obj[_GED_MAX_LEVELS]
Definition: ged.h:1916
int gtd_objpos
Definition: ged.h:1918
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126