[GRASS-SVN] r69094 - grass-addons/grass7/display/d.legend.vect

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Aug 6 03:37:57 PDT 2016


Author: lazaa
Date: 2016-08-06 03:37:56 -0700 (Sat, 06 Aug 2016)
New Revision: 69094

Modified:
   grass-addons/grass7/display/d.legend.vect/draw.c
   grass-addons/grass7/display/d.legend.vect/local_proto.h
   grass-addons/grass7/display/d.legend.vect/main.c
Log:
Separator, group labels, g_tokenize

Modified: grass-addons/grass7/display/d.legend.vect/draw.c
===================================================================
--- grass-addons/grass7/display/d.legend.vect/draw.c	2016-08-05 14:49:57 UTC (rev 69093)
+++ grass-addons/grass7/display/d.legend.vect/draw.c	2016-08-06 10:37:56 UTC (rev 69094)
@@ -17,7 +17,7 @@
 
 void draw(char *file_name, double LL, double LT, char *title, int cols, int bgcolor,
           int bcolor, int bg_width, int do_bg, char* tit_font, int tit_size, char *sub_font,
-          int sub_size, char *font, int fontsize, int fontcolor, int symb_size)
+          int sub_size, char *font, int fontsize, int fontcolor, int symb_size, char *sep)
 {
     double db, dt, dl, dr;
     double bb, bt, bl, br;
@@ -25,7 +25,7 @@
     double offs_y, row_ind, offs_x;
     double x, y;
     FILE *file_in;
-    char buf[512];
+    char buf[BUFFSIZE];
     int got_new;
     SYMBOL *Symb;
     char *symb_name, *line_color_str, *fill_color_str, *label, *type_str;
@@ -39,8 +39,8 @@
     int item_count, item;
     double it_per_col;
     double margin, bg_h, bg_w;
-    char *sep;
     int type_count;
+    char **tokens;
 
 
     D_get_src(&dt, &db, &dl, &dr);
@@ -66,7 +66,8 @@
     }
 
     file_in = fopen(file_name, "r");
-    sep = "|";
+    sub_delim = G_malloc(GNAME_MAX);
+    snprintf(sub_delim, sizeof(GNAME_MAX), "%s%s%s%s%s", sep, sep, sep, sep, sep);
     if (!file_in)
         G_fatal_error(_("Unable to open input file <%s>"), file_name);
 
@@ -77,47 +78,42 @@
     got_new = G_getl2(buf, sizeof(buf), file_in);
     G_strip(buf);
     while (got_new) {
-        /* Get the maximum symbol size */
-        symb_name = G_malloc(strlen(buf) + 1);
-        type_str = G_malloc(strlen(buf) + 1);
-        line_color_str = G_malloc(strlen(buf) + 1);
-        fill_color_str = G_malloc(strlen(buf) + 1);
-        label = G_malloc(strlen(buf)+1);
-        line_color = G_malloc(sizeof(RGBA_Color));
-        fill_color = G_malloc(sizeof(RGBA_Color));
+        if (strstr(buf, sub_delim) == NULL) {
+            /* Get the maximum symbol size */
+            symb_name = G_malloc(GNAME_MAX);
+            type_str = G_malloc(GNAME_MAX);
+            line_color_str = G_malloc(GNAME_MAX);
+            fill_color_str = G_malloc(GNAME_MAX);
+            label = G_malloc(GNAME_MAX);
+            line_color = G_malloc(sizeof(RGBA_Color));
+            fill_color = G_malloc(sizeof(RGBA_Color));
 
-        part = strtok(buf, sep);
-        sscanf(part, "%s", label);
-        part = strtok(NULL, sep);
-        sscanf(part, "%s", symb_name);
-        part = strtok(NULL, sep);
-        sscanf(part, "%lf", &size);
-        part = strtok(NULL, sep);
-        sscanf(part, "%s", line_color_str);
-        part = strtok(NULL, sep);
-        sscanf(part, "%s", fill_color_str);
-        part = strtok(NULL, sep);
-        sscanf(part, "%lf", &line_width);
-        part = strtok(NULL, sep);
-        sscanf(part, "%s", type_str);
-        part = strtok(NULL, sep);
-        sscanf(part, "%d", &type_count);
+            tokens = G_tokenize(buf, sep);
+            sscanf(tokens[0], "%s", label);
+            sscanf(tokens[1], "%s", symb_name);
+            sscanf(tokens[2], "%lf", &size);
+            sscanf(tokens[3], "%s", line_color_str);
+            sscanf(tokens[4], "%s", fill_color_str);
+            sscanf(tokens[5], "%lf", &line_width);
+            sscanf(tokens[6], "%s", type_str);
+            sscanf(tokens[6], "%d", &type_count);
+            G_free_tokens(tokens);
 
-        /* Symbol */
-        if (strcmp(type_str,"point")!=0) {
-            size = symb_size;
-        }
-        D_line_width(line_width);
-        Symb = S_read(symb_name);
-        if (Symb == NULL)
-            G_warning(_("Cannot read symbol"));
-        else
-            S_stroke(Symb, size, 0, 0);
-        symb_w = size;
+            /* Symbol */
+            if (strcmp(type_str,"point")!=0) {
+                size = symb_size;
+            }
+            D_line_width(line_width);
+            Symb = S_read(symb_name);
+            if (Symb == NULL)
+                G_warning(_("Cannot read symbol"));
+            else
+                S_stroke(Symb, size, 0, 0);
+            symb_w = size;
 
-        if (symb_w > def_symb_w)
-            def_symb_w = symb_w;
-
+            if (symb_w > def_symb_w)
+                def_symb_w = symb_w;
+        }
         item_count++;
         got_new = G_getl2(buf, sizeof(buf), file_in);
         G_strip(buf);
@@ -133,8 +129,6 @@
     item = 0;
     offs_x = 0;
     margin = 10;
-    sub_delim = G_malloc(strlen(buf)+1);
-    snprintf(sub_delim, sizeof(strlen(buf)+1), "%s%s%s%s%s", sep, sep, sep, sep, sep);
 
     got_new = G_getl2(buf, sizeof(buf), file_in);
     G_strip(buf);
@@ -155,7 +149,7 @@
         }
         if (strstr(buf, sub_delim) != NULL) {
             /* Group subtitle */
-            label = G_malloc(strlen(buf)+1);
+            label = G_malloc(GNAME_MAX);
             part = strtok(buf, sep);
             sscanf(part, "%s", label);
 
@@ -180,33 +174,27 @@
         }
         else {
             /* Map layers */
-            symb_name = G_malloc(strlen(buf) + 1);
-            type_str = G_malloc(strlen(buf) + 1);
-            line_color_str = G_malloc(strlen(buf) + 1);
-            fill_color_str = G_malloc(strlen(buf) + 1);
-            label = G_malloc(strlen(buf)+1);
+            symb_name = G_malloc(GNAME_MAX);
+            type_str = G_malloc(GNAME_MAX);
+            line_color_str = G_malloc(GNAME_MAX);
+            fill_color_str = G_malloc(GNAME_MAX);
+            label = G_malloc(GNAME_MAX);
             line_color = G_malloc(sizeof(RGBA_Color));
             fill_color = G_malloc(sizeof(RGBA_Color));
 
-            part = strtok(buf, sep);
-            sscanf(part, "%s", label);
-            part = strtok(NULL, sep);
-            sscanf(part, "%s", symb_name);
-            part = strtok(NULL, sep);
-            sscanf(part, "%lf", &size);
-            part = strtok(NULL, sep);
-            sscanf(part, "%s", line_color_str);
-            part = strtok(NULL, sep);
-            sscanf(part, "%s", fill_color_str);
-            part = strtok(NULL, sep);
-            sscanf(part, "%lf", &line_width);
-            part = strtok(NULL, sep);
-            sscanf(part, "%s", type_str);
-            part = strtok(NULL, sep);
-            sscanf(part, "%d", &type_count);
+            tokens = G_tokenize(buf, sep);
+            sscanf(tokens[0], "%s", label);
+            sscanf(tokens[1], "%s", symb_name);
+            sscanf(tokens[2], "%lf", &size);
+            sscanf(tokens[3], "%s", line_color_str);
+            sscanf(tokens[4], "%s", fill_color_str);
+            sscanf(tokens[5], "%lf", &line_width);
+            sscanf(tokens[6], "%s", type_str);
+            sscanf(tokens[6], "%d", &type_count);
+            G_free_tokens(tokens);
 
             /* Symbol */
-            if (strcmp(type_str,"point") != 0){
+            if ((strcmp(type_str,"point") != 0) && (strcmp(type_str, "centroid") != 0)) {
                 size = symb_size;
             }
             Symb = S_read(symb_name);

Modified: grass-addons/grass7/display/d.legend.vect/local_proto.h
===================================================================
--- grass-addons/grass7/display/d.legend.vect/local_proto.h	2016-08-05 14:49:57 UTC (rev 69093)
+++ grass-addons/grass7/display/d.legend.vect/local_proto.h	2016-08-06 10:37:56 UTC (rev 69094)
@@ -1,3 +1,5 @@
+#define BUFFSIZE 512
+
 /* draw.c */
 void draw(char *, double, double, char *, int, int, int, int,
-          int, char *, int, char *, int, char *, int, int, int);
+          int, char *, int, char *, int, char *, int, int, int, char *);

Modified: grass-addons/grass7/display/d.legend.vect/main.c
===================================================================
--- grass-addons/grass7/display/d.legend.vect/main.c	2016-08-05 14:49:57 UTC (rev 69093)
+++ grass-addons/grass7/display/d.legend.vect/main.c	2016-08-06 10:37:56 UTC (rev 69094)
@@ -27,7 +27,7 @@
     struct Option *opt_at, *opt_cols, *opt_font, *opt_fontsize,
             *opt_fontcolor, *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font,
             *opt_sub_fontsize, *opt_bcolor, *opt_bgcolor, *opt_symb_size,
-            *opt_bg_width, *opt_output, *opt_input;
+            *opt_bg_width, *opt_output, *opt_input, *opt_sep;
     struct Flag *fl_bg;
 
     double LL, LT;
@@ -38,7 +38,8 @@
     int cols, symb_size, bg_width;
     char *out_file;
     FILE *source, *target;
-    char buf[512];
+    char buf[BUFFSIZE];
+    char *sep;
 
 
     /* Initialize the GIS calls */
@@ -178,6 +179,9 @@
     opt_input->required = NO;
     opt_input->guisection = _("In/Out");
 
+    opt_sep = G_define_standard_option(G_OPT_F_SEP);
+    opt_sep->guisection = _("Input");
+
     /* Check command line */
     if (G_parser(argc, argv)) {
         exit(EXIT_FAILURE);
@@ -245,6 +249,8 @@
     fontcolor = D_parse_color(opt_fontcolor->answer, FALSE); /*default color: black */
 
     /* I/O */
+    sep = G_option_to_separator(opt_sep);
+
     if (opt_input->answer) {
         file_name = opt_input->answer;
         if (!file_name)
@@ -263,7 +269,7 @@
             G_fatal_error(_("Unable to open input file <%s>"), file_name);
 
         if (strcmp(opt_output->answer,"-") == 0)
-            while (fgets (buf, sizeof(buf), source) != NULL)
+            while (fgets (buf, sizeof(GNAME_MAX), source) != NULL)
                 puts (buf);
         else {
             out_file = opt_output->answer;
@@ -272,7 +278,7 @@
                 fclose(source);
                 G_fatal_error(_("Unable to create output file <%s>"), out_file);
             }
-            while (fgets (buf, sizeof(buf), source) != NULL)
+            while (fgets (buf, sizeof(GNAME_MAX), source) != NULL)
                 fputs (buf, target);
             fclose(target);
         }
@@ -281,10 +287,10 @@
 
     /* Pre-calculate the layout */
     if (do_bg)
-        draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size);
+        draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size, sep);
 
     /* Draw legend */
-    draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size);
+    draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size, sep);
 
     D_close_driver();
 



More information about the grass-commit mailing list