[GRASS-SVN] r32578 - in grass/trunk: lib/gis raster/r.mapcalc
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Aug 6 13:48:43 EDT 2008
Author: glynn
Date: 2008-08-06 13:48:43 -0400 (Wed, 06 Aug 2008)
New Revision: 32578
Modified:
grass/trunk/lib/gis/parser.c
grass/trunk/raster/r.mapcalc/evaluate.c
grass/trunk/raster/r.mapcalc/globals.h
grass/trunk/raster/r.mapcalc/main.c
grass/trunk/raster/r.mapcalc/map.c
grass/trunk/raster/r.mapcalc/map3.c
grass/trunk/raster/r.mapcalc/mapcalc.h
Log:
Make r.mapcalc use G_parser()
Modified: grass/trunk/lib/gis/parser.c
===================================================================
--- grass/trunk/lib/gis/parser.c 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/lib/gis/parser.c 2008-08-06 17:48:43 UTC (rev 32578)
@@ -959,6 +959,9 @@
char element[KEYLENGTH];
char desc[KEYLENGTH];
+ if (module_info.overwrite)
+ return 1;
+
/* figure out if any of the options use a "new" gisprompt */
/* This is to see if we should spit out the --o flag */
if (n_opts) {
@@ -2498,6 +2501,8 @@
char *overstr;
int over;
+ module_info.overwrite = 0;
+
if (!n_opts)
return (0);
Modified: grass/trunk/raster/r.mapcalc/evaluate.c
===================================================================
--- grass/trunk/raster/r.mapcalc/evaluate.c 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/evaluate.c 2008-08-06 17:48:43 UTC (rev 32578)
@@ -269,6 +269,20 @@
for (l = ee; l; l = l->next) {
expression *e = l->exp;
const char *var;
+
+ if (e->type != expr_type_binding)
+ G_fatal_error("internal error: execute: invalid type: %d",
+ e->type);
+
+ var = e->data.bind.var;
+
+ if (!overwrite && check_output_map(var))
+ G_fatal_error(_("output map <%s> exists"), var);
+ }
+
+ for (l = ee; l; l = l->next) {
+ expression *e = l->exp;
+ const char *var;
expression *val;
if (e->type != expr_type_binding)
Modified: grass/trunk/raster/r.mapcalc/globals.h
===================================================================
--- grass/trunk/raster/r.mapcalc/globals.h 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/globals.h 2008-08-06 17:48:43 UTC (rev 32578)
@@ -5,6 +5,7 @@
extern volatile int floating_point_exception;
extern volatile int floating_point_exception_occurred;
extern int overflow_occurred;
+extern int overwrite;
extern int current_depth, current_row;
extern int depths, rows, columns;
Modified: grass/trunk/raster/r.mapcalc/main.c
===================================================================
--- grass/trunk/raster/r.mapcalc/main.c 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/main.c 2008-08-06 17:48:43 UTC (rev 32578)
@@ -26,25 +26,13 @@
/****************************************************************************/
int overflow_occurred;
+int overwrite;
volatile int floating_point_exception;
volatile int floating_point_exception_occurred;
/****************************************************************************/
-static const char help_text[] =
- "r.mapcalc - Raster map layer data calculator\n"
- "\n"
- "usage: r.mapcalc '<map>=<expression>'\n"
- "\n"
- "r.mapcalc performs arithmetic on raster map layers.\n"
- "\n"
- "New raster map layers can be created which are arithmetic expressions\n"
- "involving existing raster map layers, integer or floating point constants,\n"
- "and functions.\n" "\n" "For more information use 'g.manual r.mapcalc'\n";
-
-/****************************************************************************/
-
static expr_list *result;
/****************************************************************************/
@@ -90,50 +78,74 @@
/****************************************************************************/
-static const char *join(int argc, char **argv)
+static expr_list *parse_file(const char *filename)
{
- int size = 0;
- char *buf;
- int i;
+ expr_list *res;
+ FILE *fp;
- for (i = 0; i < argc; i++)
- size += strlen(argv[i]) + 1;
+ if (strcmp(filename, "-") == 0)
+ return parse_stream(stdin);
- buf = G_malloc(size);
- *buf = '\0';
- for (i = 0; i < argc; i++) {
- if (i)
- strcat(buf, " ");
- strcat(buf, argv[i]);
- }
+ fp = fopen(filename, "r");
+ if (!fp)
+ G_fatal_error(_("unable to open input file <%s>"), filename);
- return buf;
+ res = parse_stream(fp);
+
+ fclose(fp);
+
+ return res;
}
/****************************************************************************/
int main(int argc, char **argv)
{
+ struct GModule *module;
+ struct Option *expr, *file;
int all_ok;
- int overwrite;
G_gisinit(argv[0]);
- if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
- strcmp(argv[1], "--help") == 0)) {
- fputs(help_text, stderr);
- return EXIT_SUCCESS;
+ module = G_define_module();
+ module->keywords = _("raster");
+ module->description = _("Raster map calculator.");
+ module->overwrite = 1;
+
+ expr = G_define_option();
+ expr->key = "expression";
+ expr->type = TYPE_STRING;
+ expr->required = NO;
+ expr->description = _("Expression to evaluate");
+
+ file = G_define_standard_option(G_OPT_F_INPUT);
+ file->required = NO;
+ file->description = _("File containing expression to evaluate");
+
+ if (argc == 1)
+ {
+ char **p = G_malloc(3 * sizeof(char *));
+ p[0] = argv[0];
+ p[1] = G_store("input=-");
+ p[2] = NULL;
+ argv = p;
+ argc = 2;
}
- result = (argc >= 2)
- ? parse_string(join(argc - 1, argv + 1))
- : parse_stream(stdin);
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
- if (!result)
- return EXIT_FAILURE;
+ overwrite = module->overwrite;
- overwrite = G_check_overwrite(argc, argv);
+ if (expr->answer)
+ result = parse_string(expr->answer);
+ else if (file->answer)
+ result = parse_file(file->answer);
+ else
+ result = parse_stream(stdin);
+ if (!result)
+ G_fatal_error(_("parse error"));
pre_exec();
execute(result);
Modified: grass/trunk/raster/r.mapcalc/map.c
===================================================================
--- grass/trunk/raster/r.mapcalc/map.c 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/map.c 2008-08-06 17:48:43 UTC (rev 32578)
@@ -498,6 +498,11 @@
/****************************************************************************/
+int check_output_map(const char *name)
+{
+ return !!G_find_cell2(name, G_mapset());
+}
+
int open_output_map(const char *name, int res_type)
{
int fd;
Modified: grass/trunk/raster/r.mapcalc/map3.c
===================================================================
--- grass/trunk/raster/r.mapcalc/map3.c 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/map3.c 2008-08-06 17:48:43 UTC (rev 32578)
@@ -566,6 +566,11 @@
/****************************************************************************/
+int check_output_map(const char *name)
+{
+ return !!G_find_grid3(name, G_mapset());
+}
+
int open_output_map(const char *name, int res_type)
{
void *handle;
Modified: grass/trunk/raster/r.mapcalc/mapcalc.h
===================================================================
--- grass/trunk/raster/r.mapcalc/mapcalc.h 2008-08-06 17:06:40 UTC (rev 32577)
+++ grass/trunk/raster/r.mapcalc/mapcalc.h 2008-08-06 17:48:43 UTC (rev 32578)
@@ -49,6 +49,7 @@
void *buf, int res_type);
extern void close_maps(void);
+extern int check_output_map(const char *name);
extern int open_output_map(const char *name, int res_type);
extern void put_map_row(int fd, void *buf, int res_type);
extern void close_output_map(int fd);
More information about the grass-commit
mailing list