GRASS Programmer's Manual  6.4.2(2012)
gp.c
Go to the documentation of this file.
00001 
00019 #include <stdlib.h>
00020 
00021 #include <grass/gis.h>
00022 #include <grass/gstypes.h>
00023 
00024 #define FIRST_SITE_ID 21720
00025 
00026 static geosite *Site_top = NULL;
00027 
00036 geosite *gp_get_site(int id)
00037 {
00038     geosite *gp;
00039 
00040     G_debug(5, "gp_get_site");
00041 
00042     for (gp = Site_top; gp; gp = gp->next) {
00043         if (gp->gsite_id == id) {
00044             return (gp);
00045         }
00046     }
00047 
00048     return (NULL);
00049 }
00050 
00059 geosite *gp_get_prev_site(int id)
00060 {
00061     geosite *pp;
00062 
00063     G_debug(5, "gp_get_prev_site");
00064 
00065     for (pp = Site_top; pp; pp = pp->next) {
00066         if (pp->gsite_id == id - 1) {
00067             return (pp);
00068         }
00069     }
00070 
00071     return (NULL);
00072 }
00073 
00079 int gp_num_sites(void)
00080 {
00081     geosite *gp;
00082     int i;
00083 
00084     for (i = 0, gp = Site_top; gp; gp = gp->next, i++) ;
00085 
00086     G_debug(5, "gp_num_sites(): n=%d", i);
00087 
00088     return (i);
00089 }
00090 
00097 geosite *gp_get_last_site(void)
00098 {
00099     geosite *lp;
00100 
00101     G_debug(5, "gp_get_last_site");
00102 
00103     if (!Site_top) {
00104         return (NULL);
00105     }
00106 
00107     for (lp = Site_top; lp->next; lp = lp->next) ;
00108 
00109     G_debug(5, " last site id: %d", lp->gsite_id);
00110 
00111     return (lp);
00112 }
00113 
00120 geosite *gp_get_new_site(void)
00121 {
00122     geosite *np, *lp;
00123 
00124     G_debug(5, "gp_get_new_site");
00125 
00126     np = (geosite *) G_malloc(sizeof(geosite)); /* G_fatal_error */
00127     if (!np) {
00128         return (NULL);
00129     }
00130 
00131     lp = gp_get_last_site();
00132     if (lp) {
00133         lp->next = np;
00134         np->gsite_id = lp->gsite_id + 1;
00135     }
00136     else {
00137         Site_top = np;
00138         np->gsite_id = FIRST_SITE_ID;
00139     }
00140 
00141     np->next = NULL;
00142 
00143     return (np);
00144 }
00145 
00151 void gp_update_drapesurfs(void)
00152 {
00153     geosite *gp;
00154     int i, j;
00155 
00156     for (gp = Site_top; gp; gp = gp->next) {
00157         if (gp->n_surfs) {
00158             for (i = 0; i < gp->n_surfs; i++) {
00159                 if (gp->drape_surf_id[i]) {
00160                     if (NULL == gs_get_surf(gp->drape_surf_id[i])) {
00161                         for (j = i; j < gp->n_surfs - 1; j++) {
00162                             gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
00163                         }
00164 
00165                         gp->n_surfs = gp->n_surfs - 1;
00166                     }
00167                 }
00168             }
00169         }
00170     }
00171 
00172     return;
00173 }
00174 
00183 int gp_set_defaults(geosite * gp)
00184 {
00185     int i;
00186     float dim;
00187 
00188     G_debug(5, "gp_set_defaults");
00189 
00190     if (!gp) {
00191         return (-1);
00192     }
00193 
00194     GS_get_longdim(&dim);
00195 
00196     gp->filename = NULL;
00197     gp->n_sites = gp->use_z = gp->n_surfs = gp->use_mem = 0;
00198     gp->x_trans = gp->y_trans = gp->z_trans = 0.0;
00199     gp->size = dim / 100.;
00200     gp->points = NULL;
00201     gp->width = 1;
00202     gp->color = 0xFFFFFF;
00203     gp->marker = ST_X;
00204     gp->has_z = gp->has_att = 0;
00205     gp->attr_mode = ST_ATT_NONE;
00206     gp->next = NULL;
00207     for (i = 0; i < MAX_SURFS; i++) {
00208         gp->drape_surf_id[i] = 0;
00209     }
00210 
00211     return (1);
00212 }
00213 
00219 void print_site_fields(geosite * gp)
00220 {
00221     int i;
00222 
00223     fprintf(stderr, "n_sites=%d use_z=%d n_surfs=%d use_mem=%d\n",
00224             gp->n_sites, gp->use_z, gp->n_surfs, gp->use_mem);
00225     fprintf(stderr, "x_trans=%.2f x_trans=%.2f x_trans=%.2f\n",
00226             gp->x_trans, gp->y_trans, gp->z_trans);
00227     fprintf(stderr, "size = %.2f\n", gp->size);
00228     fprintf(stderr, "points = %lx\n", (unsigned long)gp->points);
00229     fprintf(stderr, "width = %d\n", gp->width);
00230     fprintf(stderr, "color = %x\n", gp->color);
00231     fprintf(stderr, "marker = %d\n", gp->marker);
00232     fprintf(stderr, "has_z = %d, has_att = %d\n", gp->has_z, gp->has_att);
00233     fprintf(stderr, "attr_mode = %d\n", gp->attr_mode);
00234 
00235     for (i = 0; i < MAX_SURFS; i++) {
00236         fprintf(stderr, "drape_surf_id[%d] = %d\n", i, gp->drape_surf_id[i]);
00237     }
00238 
00239     return;
00240 }
00241 
00250 int gp_init_site(geosite * gp)
00251 {
00252     G_debug(5, "gp_init_site");
00253 
00254     if (!gp) {
00255         return (-1);
00256     }
00257 
00258     return (0);
00259 }
00260 
00266 void gp_delete_site(int id)
00267 {
00268     geosite *fp;
00269 
00270     G_debug(5, "gp_delete_site");
00271 
00272     fp = gp_get_site(id);
00273 
00274     if (fp) {
00275         gp_free_site(fp);
00276     }
00277 
00278     return;
00279 }
00280 
00289 int gp_free_site(geosite * fp)
00290 {
00291     geosite *gp;
00292     int found = 0;
00293 
00294     G_debug(5, "gp_free_site");
00295 
00296     if (Site_top) {
00297         if (fp == Site_top) {
00298             if (Site_top->next) {
00299                 /* can't free top if last */
00300                 found = 1;
00301                 Site_top = fp->next;
00302             }
00303             else {
00304                 gp_free_sitemem(fp);
00305                 G_free(fp);
00306                 Site_top = NULL;
00307             }
00308         }
00309         else {
00310             for (gp = Site_top; gp && !found; gp = gp->next) {
00311                 /* can't free top */
00312                 if (gp->next) {
00313                     if (gp->next == fp) {
00314                         found = 1;
00315                         gp->next = fp->next;
00316                     }
00317                 }
00318             }
00319         }
00320 
00321         if (found) {
00322             gp_free_sitemem(fp);
00323             G_free(fp);
00324             fp = NULL;
00325         }
00326 
00327         return (1);
00328     }
00329 
00330     return (-1);
00331 }
00332 
00338 void gp_free_sitemem(geosite * fp)
00339 {
00340     geopoint *gpt, *tmp;
00341 
00342     G_free((void *)fp->filename);
00343     fp->filename = NULL;
00344     if (fp->points) {
00345         for (gpt = fp->points; gpt;) {
00346             if (gpt->cattr) {
00347                 G_free(gpt->cattr);
00348             }
00349 
00350             tmp = gpt;
00351             gpt = gpt->next;
00352             G_free(tmp);
00353         }
00354 
00355         fp->n_sites = 0;
00356         fp->points = NULL;
00357     }
00358 
00359     return;
00360 }
00361 
00369 void gp_set_drapesurfs(geosite * gp, int hsurfs[], int nsurfs)
00370 {
00371     int i;
00372 
00373     for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
00374         gp->drape_surf_id[i] = hsurfs[i];
00375     }
00376 
00377     return;
00378 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines