Przeglądaj źródła

hcho: libgis: Added G_open/close_option_file
G_open_option_file(): multiple files not supported
(merge r 60514-5, https://trac.osgeo.org/grass/changeset/60517 from trunk)


git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@60739 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 11 lat temu
rodzic
commit
bcf7cbd0bb
2 zmienionych plików z 74 dodań i 3 usunięć
  1. 3 1
      include/defs/gis.h
  2. 71 2
      lib/gis/parser.c

+ 3 - 1
include/defs/gis.h

@@ -495,7 +495,9 @@ 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 *);
+char *G_option_to_separator(const struct Option *);
+FILE *G_open_option_file(const struct Option *);
+void G_close_option_file(FILE *);
 
 /* paths.c */
 int G_mkdir(const char *);

+ 71 - 2
lib/gis/parser.c

@@ -1459,12 +1459,81 @@ char* G_option_to_separator(const struct Option *option)
         sep = G_store("\t");
     else if (strcmp(option->answer, "newline") == 0)
         sep = G_store("\n");
-    else {
+    else
         sep = G_store(option->answer);
-    }
     
     G_debug(1, "G_option_to_separator(): key = %s -> sep = '%s'",
 	    option->key, sep);
     
     return sep;
 }
+
+/*!
+  \brief Get an input/output file pointer from the option. If the file name is
+  omitted or '-', it returns either stdin or stdout based on the gisprompt.
+
+  Calls G_fatal_error() on error. File pointer can be later closed by
+  G_close_option_file().
+  
+  \code
+  FILE *fp_input;
+  FILE *fp_output;
+  struct Option *opt_input;
+  struct Option *opt_output;
+  
+  opt_input = G_define_standard_option(G_OPT_F_INPUT);
+  opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
+
+  if (G_parser(argc, argv))
+      exit(EXIT_FAILURE);
+      
+  fp_input = G_open_option_file(opt_input);
+  fp_output = G_open_option_file(opt_output);
+  ...
+  G_close_option_file(fp_input);
+  G_close_option_file(fp_output);
+  \endcode
+
+  \param option pointer to a file option
+  
+  \return file pointer
+*/
+FILE *G_open_option_file(const struct Option *option)
+{
+    int stdinout;
+    FILE *fp;
+
+    stdinout = !option->answer || !*(option->answer) ||
+	    strcmp(option->answer, "-") == 0;
+
+    if (option->gisprompt == NULL)
+        G_fatal_error(_("Not a file option"));
+    else if (option->multiple)
+	G_fatal_error(_("Multiple files not supported"));
+    else if (strcmp(option->gisprompt, "old,file,file") == 0) {
+	if (stdinout)
+	    fp = stdin;
+	else if ((fp = fopen(option->answer, "r")) == NULL)
+	    G_fatal_error(_("Unable to read file [%s]"), option->answer);
+    } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
+	if (stdinout)
+	    fp = stdout;
+	else if ((fp = fopen(option->answer, "w")) == NULL)
+	    G_fatal_error(_("Unable to create file [%s]"), option->answer);
+    } else
+        G_fatal_error(_("Not a file option"));
+
+    return fp;
+}
+
+/*!
+  \brief Close an input/output file returned by G_open_option_file(). If the
+  file pointer is stdin, stdout, or stderr, nothing happens.
+
+  \param file pointer
+*/
+void G_close_option_file(FILE *fp)
+{
+    if (fp != stdin && fp != stdout && fp != stderr)
+	fclose(fp);
+}