[GRASS-SVN] r60739 - in grass/branches/releasebranch_7_0: include/defs lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jun 8 01:49:54 PDT 2014
Author: martinl
Date: 2014-06-08 01:49:54 -0700 (Sun, 08 Jun 2014)
New Revision: 60739
Modified:
grass/branches/releasebranch_7_0/include/defs/gis.h
grass/branches/releasebranch_7_0/lib/gis/parser.c
Log:
hcho: libgis: Added G_open/close_option_file
G_open_option_file(): multiple files not supported
(merge r 60514-5, r60517 from trunk)
Modified: grass/branches/releasebranch_7_0/include/defs/gis.h
===================================================================
--- grass/branches/releasebranch_7_0/include/defs/gis.h 2014-06-08 08:37:52 UTC (rev 60738)
+++ grass/branches/releasebranch_7_0/include/defs/gis.h 2014-06-08 08:49:54 UTC (rev 60739)
@@ -495,7 +495,9 @@
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 *);
Modified: grass/branches/releasebranch_7_0/lib/gis/parser.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/gis/parser.c 2014-06-08 08:37:52 UTC (rev 60738)
+++ grass/branches/releasebranch_7_0/lib/gis/parser.c 2014-06-08 08:49:54 UTC (rev 60739)
@@ -1459,12 +1459,81 @@
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);
+}
More information about the grass-commit
mailing list