[GRASS-dev] g.mlist: output (to ASCII file) parameter wish
Martin Landa
landa.martin at gmail.com
Mon May 26 04:56:39 PDT 2014
Hi,
2014-05-26 11:33 GMT+02:00 Markus Neteler <neteler at osgeo.org>:
>
> g.mlist rast pattern="precip*1951_1980_monthly_sums*" output=maplist.txt
> r.series file=maplist.txt output=precip_0_25deg.1951.1980.sum method=sum
>
> Makes sense?
>
yes, make sense, see patch in the attachment (it's not complete, -p/-f
ignores output option).
Martin
--
Martin Landa * http://geo.fsv.cvut.cz/gwiki/Landa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20140526/08ed2a8c/attachment.html>
-------------- next part --------------
Index: general/g.mlist/main.c
===================================================================
--- general/g.mlist/main.c (revision 60489)
+++ general/g.mlist/main.c (working copy)
@@ -9,7 +9,7 @@
* PURPOSE: Lists available GRASS data base files of the
* user-specified data type to standard output
*
- * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
+ * COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
@@ -27,7 +27,7 @@
static int any = 0;
-static void make_list(const struct list *,
+static void make_list(FILE *, const struct list *,
const char *, const char *,
int, int, int, int);
@@ -41,6 +41,7 @@
struct Option *exclude;
struct Option *separator;
struct Option *mapset;
+ struct Option *output;
} opt;
struct
{
@@ -53,6 +54,7 @@
} flag;
int i, n, all, num_types, nlist;
void *filter = NULL, *exclude = NULL;
+ FILE *fd;
const char *mapset;
char separator[2];
@@ -96,6 +98,9 @@
opt.separator = G_define_standard_option(G_OPT_F_SEP);
opt.separator->answer = "newline";
+ opt.output = G_define_standard_option(G_OPT_F_OUTPUT);
+ opt.output->required = NO;
+
flag.regex = G_define_flag();
flag.regex->key = 'r';
flag.regex->description =
@@ -134,6 +139,13 @@
if (flag.regex->answer && flag.extended->answer)
G_fatal_error(_("-r and -e are mutually exclusive"));
+ if (!opt.output->answer || strcmp(opt.output->answer, "-") == 0) {
+ fd = stdout;
+ }
+ else {
+ fd = fopen(opt.output->answer, "w");
+ }
+
if (opt.pattern->answer) {
if (flag.regex->answer || flag.extended->answer)
filter = G_ls_regex_filter(opt.pattern->answer, 0, (int) flag.extended->answer);
@@ -181,6 +193,9 @@
num_types = i;
}
+ if (fd != stdout)
+ G_set_verbose(0);
+
for (i = 0; i < num_types; i++) {
n = all ? i : M_get_element(opt.type->answers[i]);
@@ -198,14 +213,17 @@
}
}
else
- make_list(M_get_list(n), mapset, separator,
+ make_list(fd, M_get_list(n), mapset, separator,
flag.pretty->answer, flag.type->answer,
flag.mapset->answer, mapset && *mapset);
}
if (!flag.pretty->answer && any)
- fprintf(stdout, "\n");
+ fprintf(fd, "\n");
+ if (fd != stdout)
+ fclose(fd);
+
if (filter)
G_free_ls_filter(filter);
@@ -215,10 +233,10 @@
exit(EXIT_SUCCESS);
}
-static void make_list(
- const struct list *elem,
- const char *mapset, const char *separator,
- int pretty, int add_type, int add_mapset, int single_mapset)
+static void make_list(FILE *fd,
+ const struct list *elem,
+ const char *mapset, const char *separator,
+ int pretty, int add_type, int add_mapset, int single_mapset)
{
char path[GPATH_MAX];
const char *element = elem->element[0];
@@ -235,7 +253,7 @@
if (!mapset || !*mapset) {
int n;
for (n = 0; mapset = G__mapset_name(n), mapset; n++)
- make_list(elem, mapset, separator,
+ make_list(fd, elem, mapset, separator,
pretty, add_type, add_mapset, n == 0);
return;
}
@@ -251,9 +269,9 @@
if (count > 0) {
if (any) {
if (pretty)
- fprintf(stdout, "\n");
+ fprintf(fd, "\n");
else
- fprintf(stdout, "%s", separator);
+ fprintf(fd, "%s", separator);
}
G_message(_("%s available in mapset <%s>:"),
elem->text, mapset);
@@ -267,12 +285,12 @@
int need_mapset = 0;
if (any && i != 0)
- fprintf(stdout, "%s", separator);
+ fprintf(fd, "%s", separator);
if (add_type)
- fprintf(stdout, "%s/", alias);
+ fprintf(fd, "%s/", alias);
- fprintf(stdout, "%s", name);
+ fprintf(fd, "%s", name);
if (!add_mapset && !single_mapset) {
const char *mapset2 = G_find_file2(element, name, "");
@@ -280,7 +298,7 @@
need_mapset = strcmp(mapset, mapset2) != 0;
}
if (add_mapset || need_mapset)
- fprintf(stdout, "@%s", mapset);
+ fprintf(fd, "@%s", mapset);
G_free(name);
@@ -288,8 +306,9 @@
}
G_suppress_warnings(0);
- fflush(stdout);
-
+
+ fflush(fd);
+
G_free(list);
}
More information about the grass-dev
mailing list