BRL-CAD
path.c
Go to the documentation of this file.
1 /* P A T H . 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/path.c
21  *
22  *
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include "vmath.h"
32 #include "db.h"
33 #include "raytrace.h"
34 #include "ged.h"
35 
36 
37 /*
38  * A recursive function that is only called by ged_path_validate()
39  */
40 HIDDEN int
41 path_validate_recurse(struct ged *gedp, struct db_full_path *path,
42  struct directory *roots_child)
43 {
44  struct rt_db_internal intern;
45  struct rt_comb_internal *comb;
46  struct directory *root = DB_FULL_PATH_ROOT_DIR(path);
47 
48  /* get comb object */
49  if (rt_db_get_internal(&intern, root, gedp->ged_wdbp->dbip,
50  (fastf_t *)NULL, &rt_uniresource) < 0) {
51  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
52  return GED_ERROR;
53  }
54  comb = (struct rt_comb_internal *)intern.idb_ptr;
55 
56  /* see if we really have a child of the root dir */
57  if (db_find_named_leaf(comb->tree, roots_child->d_namep) != TREE_NULL) {
58  rt_db_free_internal(&intern);
59  if (!(path->fp_len > 2))
60  return GED_OK; /* no more children */
61  else if (roots_child->d_flags & RT_DIR_COMB) {
62  /* remove root dir */
63  ++(path->fp_names);
64  --(path->fp_len);
65  path_validate_recurse(gedp, path, DB_FULL_PATH_GET(path, 1));
66  --(path->fp_names);
67  ++(path->fp_len);
68  } else
69  return GED_ERROR; /* non-combinations shouldn't have children */
70  } else {
71  rt_db_free_internal(&intern);
72  return GED_ERROR; /* that child doesn't exist under root */
73  }
74  return GED_OK; /* for compiler */
75 }
76 
77 
78 /**
79  * Checks that each directory in the supplied path actually has the
80  * subdirectories that are implied by the path. Returns GED_OK if
81  * true, or GED_ERROR if false.
82  */
83 int
84 ged_path_validate(struct ged *gedp, const struct db_full_path *const path)
85 {
86  /* Since this is a db_full_path, we already know that each
87  * directory exists at root, and just need to check the order */
88  struct directory *root;
89  struct db_full_path path_tmp;
90  int ret;
91 
92  db_full_path_init(&path_tmp);
93  db_dup_full_path(&path_tmp, path);
94 
95  if (path_tmp.fp_len <= 1)
96  return GED_OK; /* TRUE; no children */
97 
98  root = DB_FULL_PATH_ROOT_DIR(&path_tmp);
99  if (!(root->d_flags & RT_DIR_COMB))
100  return GED_ERROR; /* has children, but isn't a combination */
101 
102  ret = path_validate_recurse(gedp, &path_tmp,
103  DB_FULL_PATH_GET(&path_tmp, 1));
104  db_free_full_path(&path_tmp);
105  return ret;
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  */
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
int ged_path_validate(struct ged *gedp, const struct db_full_path *const path)
Definition: path.c:84
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
HIDDEN int path_validate_recurse(struct ged *gedp, struct db_full_path *path, struct directory *roots_child)
Definition: path.c:41
union tree * db_find_named_leaf(union tree *tp, const char *cp)
Definition: db_tree.c:393
size_t fp_len
Definition: db_fullpath.h:44
void db_dup_full_path(struct db_full_path *newp, const struct db_full_path *oldp)
Definition: db_fullpath.c:87
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
void db_full_path_init(struct db_full_path *pathp)
Definition: db_fullpath.c:40
#define GED_ERROR
Definition: ged.h:61
#define HIDDEN
Definition: common.h:86
if(share_geom)
Definition: nmg_mod.c:3829
#define DB_FULL_PATH_GET(_pp, _i)
Definition: db_fullpath.h:55
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define TREE_NULL
Definition: raytrace.h:1181
void db_free_full_path(struct db_full_path *pp)
Definition: db_fullpath.c:473
struct directory ** fp_names
array of dir pointers
Definition: db_fullpath.h:46
struct bu_vls * ged_result_str
Definition: ged.h:357
void * idb_ptr
Definition: raytrace.h:195
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define DB_FULL_PATH_ROOT_DIR(_pp)
Definition: db_fullpath.h:54
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216