Browse Source

libgis: add G_option_to_separator()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@54536 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 12 years ago
parent
commit
559cd47f60
2 changed files with 49 additions and 0 deletions
  1. 1 0
      include/defs/gis.h
  2. 48 0
      lib/gis/parser.c

+ 1 - 0
include/defs/gis.h

@@ -484,6 +484,7 @@ char *G_recreate_command(void);
 void G_add_keyword(const char *);
 void G_set_keywords(const char *);
 int G_get_overwrite();
+char G_option_to_separator(const struct Option *);
 
 /* paths.c */
 int G_mkdir(const char *);

+ 48 - 0
lib/gis/parser.c

@@ -1393,3 +1393,51 @@ static void append_error(const char *msg)
     st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
     st->error[st->n_errors++] = G_store(msg);
 }
+
+/*!
+  \brief Get separator from the option.
+
+  Calls G_fatal_error() on error.
+  
+  \code
+  char fs;
+  struct Option *opt_fs;
+  
+  opt_fs = G_define_standard_option(G_OPT_F_SEP);
+  
+  if (G_parser(argc, argv))
+      exit(EXIT_FAILURE);
+      
+  fs = G_option_to_separator(opt_fs);
+  \endcode
+
+  \param option pointer to separator option
+  
+  \return character to be used as separator
+*/
+char G_option_to_separator(const struct Option *option)
+{
+    char sep;
+    
+    if (option->answer == NULL)
+        G_fatal_error(_("No separator given"));
+
+    if (strcmp(option->answer, "space") == 0)
+        sep = ' ';
+    else if (strcmp(option->answer, "tab") == 0 ||
+             strcmp(option->answer, "\\t") == 0)
+        sep = '\t';
+    else if (strcmp(option->answer, "newline") == 0)
+        sep = '\n';
+    else if (strcmp(option->answer, "comma") == 0)
+        sep = ',';
+    else {
+        if (strlen(option->answer) > 1)
+            G_warning(_("Option <%s>: '%s' is too long, using only first character '%c'"),
+                      option->key, option->answer, option->answer[0]);
+        
+        sep = option->answer[0];
+    }
+
+    return sep;
+}