GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pad.c
Go to the documentation of this file.
1 
2 /* required for NULL */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <grass/gis.h>
7 #include "pad.h"
8 
9 static PAD *padlist;
10 
11 static int free_item(ITEM * item)
12 {
13  LIST *list, *next;
14 
15  if (item->name != NULL)
16  G_free(item->name);
17  for (list = item->list; list != NULL; list = next) {
18  next = list->next;
19  if (list->value)
20  G_free(list->value);
21  G_free(list);
22  }
23  G_free(item);
24 
25  return 0;
26 }
27 
28 static ITEM *new_item(PAD * pad, const char *name)
29 {
30  ITEM *item;
31 
32  item = (ITEM *) G_malloc((size_t) sizeof(ITEM));
33  if (item == NULL)
34  return (ITEM *) NULL;
35 
36  item->name = G_store(name);
37  if (item->name == NULL) {
38  G_free(item);
39  return (ITEM *) NULL;
40  }
41  item->list = NULL;
42  item->next = pad->items;
43  if (item->next != NULL)
44  item->next->prev = item;
45  item->prev = NULL;
46  pad->items = item;
47 
48  return item;
49 }
50 
51 static int remove_value(ITEM * item, const char *value)
52 {
53  LIST **p = &item->list;
54  LIST *l = *p;
55 
56  for (l = *p; l; l = *p) {
57  if (value && l->value && !strcmp(value, l->value)) {
58  *p = l->next;
59  if (l->value)
60  G_free(l->value);
61  G_free(l);
62  }
63  else
64  p = &l->next;
65  }
66 
67  return 0;
68 }
69 
70 int append_item(PAD * pad, const char *name, const char *value, int replace)
71 {
72  ITEM *item;
73  LIST *cur, *prev;
74  LIST *list;
75 
76  if (pad == NULL)
77  return 0;
78 
79  /* allocate a list struct and put value into it */
80  list = (LIST *) G_malloc((size_t) sizeof(LIST));
81  if (list == NULL)
82  return 0;
83  list->next = NULL;
84  list->value = G_store(value);
85  if (list->value == NULL) {
86  G_free(list);
87  return 0;
88  }
89  /* find the named item for the current pad */
90  item = find_item(pad, name);
91  if (item == NULL)
92  item = new_item(pad, name);
93  if (item == NULL)
94  return 0;
95 
96  /* remove any existing occurences of the value */
97  if (replace)
98  remove_value(item, value);
99 
100  /* add the LIST at the end of the item LIST */
101  prev = NULL;
102  for (cur = item->list; cur != NULL; cur = cur->next)
103  prev = cur;
104 
105  if (prev == NULL)
106  item->list = list;
107  else
108  prev->next = list;
109 
110  return 1;
111 }
112 
113 int delete_item(PAD * pad, const char *name)
114 {
115  ITEM *item;
116 
117  item = find_item(pad, name);
118  if (item == NULL)
119  return 0;
120 
121  if (item->prev == NULL)
122  pad->items = item->next;
123  else
124  item->prev->next = item->next;
125 
126  if (item->next != NULL)
127  item->next->prev = item->prev;
128 
129  /* free the item */
130  free_item(item);
131 
132  return 1;
133 }
134 
135 ITEM *find_item(PAD * pad, const char *name)
136 {
137  ITEM *item;
138 
139  if (pad != NULL)
140  for (item = pad->items; item != NULL; item = item->next)
141  if (strcmp(name, item->name) == 0)
142  return item;
143  return (ITEM *) NULL;
144 }
145 
146 PAD *pad_list(void)
147 {
148  return padlist;
149 }
150 
151 static int delink_pad(PAD * pad)
152 {
153  if (pad == NULL)
154  return 1;
155 
156  if (pad->prev == NULL)
157  padlist = pad->next;
158  else
159  pad->prev->next = pad->next;
160 
161  if (pad->next != NULL)
162  pad->next->prev = pad->prev;
163 
164  return 0;
165 }
166 
167 int create_pad(const char *name)
168 {
169  PAD *pad;
170 
171  pad = (PAD *) G_malloc((size_t) sizeof(PAD));
172  if (pad == NULL)
173  return 0;
174  pad->name = G_store(name);
175  if (pad->name == NULL) {
176  G_free(pad);
177  return 0;
178  }
179  pad->items = NULL;
180  pad->next = padlist;
181  if (pad->next != NULL)
182  pad->next->prev = pad;
183  pad->prev = NULL;
184  padlist = pad;
185  return 1;
186 }
187 
188 int delete_pad(PAD * pad)
189 {
190  ITEM *item, *next;
191 
192  if (pad == NULL)
193  return 0;
194 
195  delink_pad(pad);
196 
197  /* free the items */
198  for (item = pad->items; item != NULL; item = next) {
199  next = item->next;
200  free_item(item);
201  }
202  G_free(pad);
203 
204  return 1;
205 }
206 
207 PAD *find_pad(const char *name)
208 {
209  PAD *pad;
210 
211  for (pad = padlist; pad != NULL; pad = pad->next)
212  if (strcmp(name, pad->name) == 0)
213  return pad;
214  return (PAD *) NULL;
215 }
216 
217 int invent_pad(char *name)
218 {
219  static int i = 0;
220 
221  do
222  sprintf(name, "%d", ++i);
223  while (find_pad(name) != NULL);
224 
225  return 0;
226 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
struct _pad_ * prev
Definition: pad.h:22
int l
Definition: dataquad.c:292
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
struct _item_ * prev
Definition: pad.h:15
int new_item(char *item, char *menu, char **tcl_lines, int line_no)
Definition: reg_entries.c:173
string name
Definition: render.py:1314
Definition: pad.h:5
struct _pad_ * next
Definition: pad.h:22
struct _list LIST
ITEM * items
Definition: pad.h:21
struct _pad_ PAD
char * value
Definition: pad.h:7
LIST * list
Definition: pad.h:14
struct _list * next
Definition: pad.h:8
int invent_pad(char *name)
Definition: pad.c:217
ITEM * find_item(PAD *pad, const char *name)
Definition: pad.c:135
struct _item_ ITEM
char * name
Definition: pad.h:20
Definition: pad.h:11
Definition: pad.h:18
char * value
Definition: env.c:30
PAD * find_pad(const char *name)
Definition: pad.c:207
return NULL
Definition: dbfopen.c:1394
PAD * pad_list(void)
Definition: pad.c:146
struct _item_ * next
Definition: pad.h:15
int delete_item(PAD *pad, const char *name)
Definition: pad.c:113
char * name
Definition: pad.h:13
int delete_pad(PAD *pad)
Definition: pad.c:188
int create_pad(const char *name)
Definition: pad.c:167
int append_item(PAD *pad, const char *name, const char *value, int replace)
Definition: pad.c:70