GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gp.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 
21 #include <grass/gis.h>
22 #include <grass/gstypes.h>
23 
24 #define FIRST_SITE_ID 21720
25 
26 static geosite *Site_top = NULL;
27 
36 geosite *gp_get_site(int id)
37 {
38  geosite *gp;
39 
40  G_debug(5, "gp_get_site");
41 
42  for (gp = Site_top; gp; gp = gp->next) {
43  if (gp->gsite_id == id) {
44  return (gp);
45  }
46  }
47 
48  return (NULL);
49 }
50 
59 geosite *gp_get_prev_site(int id)
60 {
61  geosite *pp;
62 
63  G_debug(5, "gp_get_prev_site");
64 
65  for (pp = Site_top; pp; pp = pp->next) {
66  if (pp->gsite_id == id - 1) {
67  return (pp);
68  }
69  }
70 
71  return (NULL);
72 }
73 
79 int gp_num_sites(void)
80 {
81  geosite *gp;
82  int i;
83 
84  for (i = 0, gp = Site_top; gp; gp = gp->next, i++) ;
85 
86  G_debug(5, "gp_num_sites(): n=%d", i);
87 
88  return (i);
89 }
90 
97 geosite *gp_get_last_site(void)
98 {
99  geosite *lp;
100 
101  G_debug(5, "gp_get_last_site");
102 
103  if (!Site_top) {
104  return (NULL);
105  }
106 
107  for (lp = Site_top; lp->next; lp = lp->next) ;
108 
109  G_debug(5, " last site id: %d", lp->gsite_id);
110 
111  return (lp);
112 }
113 
120 geosite *gp_get_new_site(void)
121 {
122  geosite *np, *lp;
123 
124  G_debug(5, "gp_get_new_site");
125 
126  np = (geosite *) G_malloc(sizeof(geosite)); /* G_fatal_error */
127  if (!np) {
128  return (NULL);
129  }
130 
131  lp = gp_get_last_site();
132  if (lp) {
133  lp->next = np;
134  np->gsite_id = lp->gsite_id + 1;
135  }
136  else {
137  Site_top = np;
138  np->gsite_id = FIRST_SITE_ID;
139  }
140 
141  np->next = NULL;
142 
143  return (np);
144 }
145 
152 {
153  geosite *gp;
154  int i, j;
155 
156  for (gp = Site_top; gp; gp = gp->next) {
157  if (gp->n_surfs) {
158  for (i = 0; i < gp->n_surfs; i++) {
159  if (gp->drape_surf_id[i]) {
160  if (NULL == gs_get_surf(gp->drape_surf_id[i])) {
161  for (j = i; j < gp->n_surfs - 1; j++) {
162  gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
163  }
164 
165  gp->n_surfs = gp->n_surfs - 1;
166  }
167  }
168  }
169  }
170  }
171 
172  return;
173 }
174 
183 int gp_set_defaults(geosite * gp)
184 {
185  int i;
186  float dim;
187 
188  G_debug(5, "gp_set_defaults");
189 
190  if (!gp) {
191  return (-1);
192  }
193 
194  GS_get_longdim(&dim);
195 
196  gp->filename = NULL;
197  gp->n_sites = gp->use_z = gp->n_surfs = gp->use_mem = 0;
198  gp->x_trans = gp->y_trans = gp->z_trans = 0.0;
199  gp->size = dim / 100.;
200  gp->points = NULL;
201  gp->width = 1;
202  gp->color = 0xFFFFFF;
203  gp->marker = ST_X;
204  gp->has_z = gp->has_att = 0;
205  gp->attr_mode = ST_ATT_NONE;
206  gp->next = NULL;
207  for (i = 0; i < MAX_SURFS; i++) {
208  gp->drape_surf_id[i] = 0;
209  }
210 
211  return (1);
212 }
213 
219 void print_site_fields(geosite * gp)
220 {
221  int i;
222 
223  fprintf(stderr, "n_sites=%d use_z=%d n_surfs=%d use_mem=%d\n",
224  gp->n_sites, gp->use_z, gp->n_surfs, gp->use_mem);
225  fprintf(stderr, "x_trans=%.2f x_trans=%.2f x_trans=%.2f\n",
226  gp->x_trans, gp->y_trans, gp->z_trans);
227  fprintf(stderr, "size = %.2f\n", gp->size);
228  fprintf(stderr, "points = %lx\n", (unsigned long)gp->points);
229  fprintf(stderr, "width = %d\n", gp->width);
230  fprintf(stderr, "color = %x\n", gp->color);
231  fprintf(stderr, "marker = %d\n", gp->marker);
232  fprintf(stderr, "has_z = %d, has_att = %d\n", gp->has_z, gp->has_att);
233  fprintf(stderr, "attr_mode = %d\n", gp->attr_mode);
234 
235  for (i = 0; i < MAX_SURFS; i++) {
236  fprintf(stderr, "drape_surf_id[%d] = %d\n", i, gp->drape_surf_id[i]);
237  }
238 
239  return;
240 }
241 
250 int gp_init_site(geosite * gp)
251 {
252  G_debug(5, "gp_init_site");
253 
254  if (!gp) {
255  return (-1);
256  }
257 
258  return (0);
259 }
260 
266 void gp_delete_site(int id)
267 {
268  geosite *fp;
269 
270  G_debug(5, "gp_delete_site");
271 
272  fp = gp_get_site(id);
273 
274  if (fp) {
275  gp_free_site(fp);
276  }
277 
278  return;
279 }
280 
289 int gp_free_site(geosite * fp)
290 {
291  geosite *gp;
292  int found = 0;
293 
294  G_debug(5, "gp_free_site");
295 
296  if (Site_top) {
297  if (fp == Site_top) {
298  if (Site_top->next) {
299  /* can't free top if last */
300  found = 1;
301  Site_top = fp->next;
302  }
303  else {
304  gp_free_sitemem(fp);
305  G_free(fp);
306  Site_top = NULL;
307  }
308  }
309  else {
310  for (gp = Site_top; gp && !found; gp = gp->next) {
311  /* can't free top */
312  if (gp->next) {
313  if (gp->next == fp) {
314  found = 1;
315  gp->next = fp->next;
316  }
317  }
318  }
319  }
320 
321  if (found) {
322  gp_free_sitemem(fp);
323  G_free(fp);
324  fp = NULL;
325  }
326 
327  return (1);
328  }
329 
330  return (-1);
331 }
332 
338 void gp_free_sitemem(geosite * fp)
339 {
340  geopoint *gpt, *tmp;
341 
342  G_free((void *)fp->filename);
343  fp->filename = NULL;
344  if (fp->points) {
345  for (gpt = fp->points; gpt;) {
346  if (gpt->cattr) {
347  G_free(gpt->cattr);
348  }
349 
350  tmp = gpt;
351  gpt = gpt->next;
352  G_free(tmp);
353  }
354 
355  fp->n_sites = 0;
356  fp->points = NULL;
357  }
358 
359  return;
360 }
361 
369 void gp_set_drapesurfs(geosite * gp, int hsurfs[], int nsurfs)
370 {
371  int i;
372 
373  for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
374  gp->drape_surf_id[i] = hsurfs[i];
375  }
376 
377  return;
378 }
int gp_free_site(geosite *fp)
Free geosite struct.
Definition: gp.c:289
int gp_set_defaults(geosite *gp)
Set default value for geosite struct.
Definition: gp.c:183
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
geosite * gp_get_last_site(void)
Get last point set.
Definition: gp.c:97
int gp_num_sites(void)
Get number of loaded point sets.
Definition: gp.c:79
void gp_set_drapesurfs(geosite *gp, int hsurfs[], int nsurfs)
Set drape surfaces.
Definition: gp.c:369
geosite * gp_get_site(int id)
Get geosite struct.
Definition: gp.c:36
void gp_update_drapesurfs(void)
Update drape surfaces.
Definition: gp.c:151
geosite * gp_get_new_site(void)
Create new geosite instance and add it to list.
Definition: gp.c:120
geosite * gp_get_prev_site(int id)
Get previous geosite struct from list.
Definition: gp.c:59
void print_site_fields(geosite *gp)
Print point set fields, debugging.
Definition: gp.c:219
#define FIRST_SITE_ID
Definition: gp.c:24
geosurf * gs_get_surf(int id)
Get geosurf struct.
Definition: gs.c:62
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void gp_free_sitemem(geosite *fp)
Free geosite.
Definition: gp.c:338
int gp_init_site(geosite *gp)
Initialize geosite struct.
Definition: gp.c:250
int GS_get_longdim(float *dim)
Get largest dimension.
Definition: GS2.c:140
void gp_delete_site(int id)
Delete point set and remove from list.
Definition: gp.c:266