BRL-CAD
ged_util.c
Go to the documentation of this file.
1 /* G E D _ U T I L . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2000-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 /** @addtogroup libged */
21 /** @{ */
22 /** @file libged/ged_util.c
23  *
24  * Utility routines for common operations in libged.
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 
32 #include "ged.h"
33 #include "./ged_private.h"
34 
35 int
37 {
38  if (UNLIKELY(!results))
39  return GED_ERROR;
40  BU_ALLOC(results->results_tbl, struct bu_ptbl);
41  BU_PTBL_INIT(results->results_tbl);
42  return GED_OK;
43 }
44 
45 
46 int
47 _ged_results_add(struct ged_results *results, const char *result_string)
48 {
49  /* If there isn't a string, we can live with that */
50  if (UNLIKELY(!result_string))
51  return GED_OK;
52 
53  /* If we have nowhere to insert into and we *do* have a string, trouble */
54  if (UNLIKELY(!results))
55  return GED_ERROR;
56  if (UNLIKELY(!(results->results_tbl)))
57  return GED_ERROR;
59  return GED_ERROR;
60 
61  /* We're good to go - copy the string and stuff it in. */
62  bu_ptbl_ins(results->results_tbl, (long *)bu_strdup(result_string));
63 
64  return GED_OK;
65 }
66 
67 size_t
69 {
70  if (UNLIKELY(!results)) return 0;
71  if (UNLIKELY(!(results->results_tbl))) return 0;
72  return (size_t)BU_PTBL_LEN(results->results_tbl);
73 }
74 
75 const char *
76 ged_results_get(struct ged_results *results, size_t index)
77 {
78  return (const char *)BU_PTBL_GET(results->results_tbl, index);
79 }
80 
81 void
83 {
84  int i = 0;
85  if (UNLIKELY(!results)) return;
86  if (UNLIKELY(!(results->results_tbl))) return;
87 
88  /* we clean up everything except the ged_results structure itself */
89  for (i = (int)BU_PTBL_LEN(results->results_tbl) - 1; i >= 0; i--) {
90  char *rstring = (char *)BU_PTBL_GET(results->results_tbl, i);
91  if (rstring)
92  bu_free(rstring, "free results string");
93  }
94  bu_ptbl_reset(results->results_tbl);
95 }
96 
97 void
98 ged_results_free(struct ged_results *results) {
99  if (UNLIKELY(!results)) return;
100  if (UNLIKELY(!(results->results_tbl))) return;
101 
102  ged_results_clear(results);
103  bu_ptbl_free(results->results_tbl);
104  bu_free(results->results_tbl, "done with results ptbl");
105 }
106 
107 /*
108  * Local Variables:
109  * mode: C
110  * tab-width: 8
111  * indent-tabs-mode: t
112  * c-file-style: "stroustrup"
113  * End:
114  * ex: shiftwidth=4 tabstop=8
115  */
#define GED_OK
Definition: ged.h:55
struct bu_ptbl * results_tbl
Definition: ged_private.h:510
Header file for the BRL-CAD common definitions.
int bu_ptbl_ins(struct bu_ptbl *b, long *p)
void bu_ptbl_reset(struct bu_ptbl *b)
Definition: ptbl.c:49
#define GED_ERROR
Definition: ged.h:61
Definition: ptbl.h:62
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define BU_PTBL_IS_INITIALIZED(_p)
Definition: ptbl.h:98
#define BU_PTBL_GET(ptbl, i)
Definition: ptbl.h:108
int _ged_results_add(struct ged_results *results, const char *result_string)
Definition: ged_util.c:47
int _ged_results_init(struct ged_results *results)
Definition: ged_util.c:36
#define BU_PTBL_INIT(_p)
Definition: ptbl.h:80
void ged_results_clear(struct ged_results *results)
Definition: ged_util.c:82
void ged_results_free(struct ged_results *results)
Definition: ged_util.c:98
#define BU_PTBL_LEN(ptbl)
Definition: ptbl.h:107
void bu_ptbl_free(struct bu_ptbl *b)
Definition: ptbl.c:226
size_t ged_results_count(struct ged_results *results)
Definition: ged_util.c:68
const char * ged_results_get(struct ged_results *results, size_t index)
Definition: ged_util.c:76
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define bu_strdup(s)
Definition: str.h:71
#define UNLIKELY(expression)
Definition: common.h:282