Ignore:
Timestamp:
Aug 6, 2008, 10:48:43 AM (16 years ago)
Author:
glynn
Message:

Make r.mapcalc use G_parser()

Location:
grass/trunk/raster/r.mapcalc
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/raster/r.mapcalc/evaluate.c

    r32526 r32578  
    270270        expression *e = l->exp;
    271271        const char *var;
     272
     273        if (e->type != expr_type_binding)
     274            G_fatal_error("internal error: execute: invalid type: %d",
     275                          e->type);
     276
     277        var = e->data.bind.var;
     278
     279        if (!overwrite && check_output_map(var))
     280            G_fatal_error(_("output map <%s> exists"), var);
     281    }
     282
     283    for (l = ee; l; l = l->next) {
     284        expression *e = l->exp;
     285        const char *var;
    272286        expression *val;
    273287
  • grass/trunk/raster/r.mapcalc/globals.h

    r32526 r32578  
    66extern volatile int floating_point_exception_occurred;
    77extern int overflow_occurred;
     8extern int overwrite;
    89
    910extern int current_depth, current_row;
  • grass/trunk/raster/r.mapcalc/main.c

    r32526 r32578  
    2727
    2828int overflow_occurred;
     29int overwrite;
    2930
    3031volatile int floating_point_exception;
    3132volatile int floating_point_exception_occurred;
    32 
    33 /****************************************************************************/
    34 
    35 static const char help_text[] =
    36     "r.mapcalc - Raster map layer data calculator\n"
    37     "\n"
    38     "usage: r.mapcalc '<map>=<expression>'\n"
    39     "\n"
    40     "r.mapcalc performs arithmetic on raster map layers.\n"
    41     "\n"
    42     "New raster map layers can be created which are arithmetic expressions\n"
    43     "involving existing raster map layers, integer or floating point constants,\n"
    44     "and functions.\n" "\n" "For more information use 'g.manual r.mapcalc'\n";
    4533
    4634/****************************************************************************/
     
    9179/****************************************************************************/
    9280
    93 static const char *join(int argc, char **argv)
     81static expr_list *parse_file(const char *filename)
    9482{
    95     int size = 0;
    96     char *buf;
    97     int i;
     83    expr_list *res;
     84    FILE *fp;
    9885
    99     for (i = 0; i < argc; i++)
    100         size += strlen(argv[i]) + 1;
     86    if (strcmp(filename, "-") == 0)
     87        return parse_stream(stdin);
    10188
    102     buf = G_malloc(size);
    103     *buf = '\0';
    104     for (i = 0; i < argc; i++) {
    105         if (i)
    106             strcat(buf, " ");
    107         strcat(buf, argv[i]);
    108     }
     89    fp = fopen(filename, "r");
     90    if (!fp)
     91        G_fatal_error(_("unable to open input file <%s>"), filename);
    10992
    110     return buf;
     93    res = parse_stream(fp);
     94
     95    fclose(fp);
     96
     97    return res;
    11198}
    11299
     
    115102int main(int argc, char **argv)
    116103{
     104    struct GModule *module;
     105    struct Option *expr, *file;
    117106    int all_ok;
    118     int overwrite;
    119107
    120108    G_gisinit(argv[0]);
    121109
    122     if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
    123                      strcmp(argv[1], "--help") == 0)) {
    124         fputs(help_text, stderr);
    125         return EXIT_SUCCESS;
     110    module = G_define_module();
     111    module->keywords = _("raster");
     112    module->description = _("Raster map calculator.");
     113    module->overwrite = 1;
     114
     115    expr = G_define_option();
     116    expr->key = "expression";
     117    expr->type = TYPE_STRING;
     118    expr->required = NO;
     119    expr->description = _("Expression to evaluate");
     120
     121    file = G_define_standard_option(G_OPT_F_INPUT);
     122    file->required = NO;
     123    file->description = _("File containing expression to evaluate");
     124
     125    if (argc == 1)
     126    {
     127        char **p = G_malloc(3 * sizeof(char *));
     128        p[0] = argv[0];
     129        p[1] = G_store("input=-");
     130        p[2] = NULL;
     131        argv = p;
     132        argc = 2;
    126133    }
    127134
    128     result = (argc >= 2)
    129         ? parse_string(join(argc - 1, argv + 1))
    130         : parse_stream(stdin);
     135    if (G_parser(argc, argv))
     136        exit(EXIT_FAILURE);
     137
     138    overwrite = module->overwrite;
     139
     140    if (expr->answer)
     141        result = parse_string(expr->answer);
     142    else if (file->answer)
     143        result = parse_file(file->answer);
     144    else
     145        result = parse_stream(stdin);
    131146
    132147    if (!result)
    133         return EXIT_FAILURE;
    134 
    135     overwrite = G_check_overwrite(argc, argv);
    136 
     148        G_fatal_error(_("parse error"));
    137149
    138150    pre_exec();
  • grass/trunk/raster/r.mapcalc/map.c

    r32526 r32578  
    499499/****************************************************************************/
    500500
     501int check_output_map(const char *name)
     502{
     503    return !!G_find_cell2(name, G_mapset());
     504}
     505
    501506int open_output_map(const char *name, int res_type)
    502507{
  • grass/trunk/raster/r.mapcalc/map3.c

    r32526 r32578  
    567567/****************************************************************************/
    568568
     569int check_output_map(const char *name)
     570{
     571    return !!G_find_grid3(name, G_mapset());
     572}
     573
    569574int open_output_map(const char *name, int res_type)
    570575{
  • grass/trunk/raster/r.mapcalc/mapcalc.h

    r32526 r32578  
    5050extern void close_maps(void);
    5151
     52extern int check_output_map(const char *name);
    5253extern int open_output_map(const char *name, int res_type);
    5354extern void put_map_row(int fd, void *buf, int res_type);
Note: See TracChangeset for help on using the changeset viewer.