~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.mapcalc/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
/****************************************************************************/
27
27
 
28
28
int overflow_occurred;
 
29
int overwrite_flag;
29
30
 
30
31
volatile int floating_point_exception;
31
32
volatile int floating_point_exception_occurred;
32
33
 
 
34
long seed_value;
 
35
long seeded;
 
36
 
33
37
/****************************************************************************/
34
38
 
35
39
static expr_list *result;
77
81
 
78
82
/****************************************************************************/
79
83
 
80
 
static const char *join(int argc, char **argv)
 
84
static expr_list *parse_file(const char *filename)
81
85
{
82
 
    int size = 0;
83
 
    char *buf;
84
 
    int i;
85
 
 
86
 
    for (i = 0; i < argc; i++)
87
 
        size += strlen(argv[i]) + 1;
88
 
 
89
 
    buf = G_malloc(size);
90
 
    *buf = '\0';
91
 
    for (i = 0; i < argc; i++) {
92
 
        if (i)
93
 
            strcat(buf, " ");
94
 
        strcat(buf, argv[i]);
95
 
    }
96
 
 
97
 
    return buf;
 
86
    expr_list *res;
 
87
    FILE *fp;
 
88
 
 
89
    if (strcmp(filename, "-") == 0)
 
90
        return parse_stream(stdin);
 
91
 
 
92
    fp = fopen(filename, "r");
 
93
    if (!fp)
 
94
        G_fatal_error(_("Unable to open input file <%s>"), filename);
 
95
 
 
96
    res = parse_stream(fp);
 
97
 
 
98
    fclose(fp);
 
99
 
 
100
    return res;
98
101
}
99
102
 
100
103
/****************************************************************************/
101
104
 
102
105
int main(int argc, char **argv)
103
106
{
 
107
    struct GModule *module;
 
108
    struct Option *expr, *file, *seed;
 
109
    struct Flag *random;
104
110
    int all_ok;
105
 
    int overwrite;
106
111
 
107
112
    G_gisinit(argv[0]);
108
113
 
109
 
    if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
110
 
                     strcmp(argv[1], "--help") == 0)) {
111
 
        fputs(
112
 
            _("r.mapcalc - Raster map layer data calculator\n"
113
 
            "\n"
114
 
            "usage: r.mapcalc '<map>=<expression>'\n"
115
 
            "\n"
116
 
            "r.mapcalc performs arithmetic on raster map layers.\n"
117
 
            "\n"
118
 
            "New raster map layers can be created which are arithmetic expressions\n"
119
 
            "involving existing raster map layers, integer or floating point constants,\n"
120
 
            "and functions.\n \nFor more information use 'g.manual r.mapcalc'\n"), 
121
 
       stderr);
122
 
        return EXIT_SUCCESS;
 
114
    module = G_define_module();
 
115
    G_add_keyword(_("raster"));
 
116
    G_add_keyword(_("algebra"));
 
117
    module->description = _("Raster map calculator.");
 
118
    module->overwrite = 1;
 
119
 
 
120
    expr = G_define_option();
 
121
    expr->key = "expression";
 
122
    expr->type = TYPE_STRING;
 
123
    expr->required = NO;
 
124
    expr->description = _("Expression to evaluate");
 
125
    expr->guisection = _("Expression");
 
126
    
 
127
    file = G_define_standard_option(G_OPT_F_INPUT);
 
128
    file->key = "file";
 
129
    file->required = NO;
 
130
    file->description = _("File containing expression(s) to evaluate");
 
131
    file->guisection = _("Expression");
 
132
 
 
133
    seed = G_define_option();
 
134
    seed->key = "seed";
 
135
    seed->type = TYPE_INTEGER;
 
136
    seed->required = NO;
 
137
    seed->description = _("Seed for rand() function");
 
138
 
 
139
    random = G_define_flag();
 
140
    random->key = 's';
 
141
    random->description = _("Generate random seed (result is non-deterministic)");
 
142
 
 
143
    if (argc == 1)
 
144
    {
 
145
        char **p = G_malloc(3 * sizeof(char *));
 
146
        p[0] = argv[0];
 
147
        p[1] = G_store("file=-");
 
148
        p[2] = NULL;
 
149
        argv = p;
 
150
        argc = 2;
123
151
    }
124
152
 
125
 
    result = (argc >= 2)
126
 
        ? parse_string(join(argc - 1, argv + 1))
127
 
        : parse_stream(stdin);
 
153
    if (G_parser(argc, argv))
 
154
        exit(EXIT_FAILURE);
 
155
 
 
156
    overwrite_flag = module->overwrite;
 
157
 
 
158
    if (expr->answer && file->answer)
 
159
        G_fatal_error(_("%s= and %s= are mutually exclusive"),
 
160
                        expr->key, file->key);
 
161
 
 
162
    if (seed->answer && random->answer)
 
163
        G_fatal_error(_("%s= and -%c are mutually exclusive"),
 
164
                      seed->key, random->key);
 
165
 
 
166
    if (expr->answer)
 
167
        result = parse_string(expr->answer);
 
168
    else if (file->answer)
 
169
        result = parse_file(file->answer);
 
170
    else
 
171
        result = parse_stream(stdin);
128
172
 
129
173
    if (!result)
130
 
        return EXIT_FAILURE;
131
 
 
132
 
    overwrite = G_check_overwrite(argc, argv);
133
 
 
 
174
        G_fatal_error(_("parse error"));
 
175
 
 
176
    if (seed->answer) {
 
177
        seed_value = atol(seed->answer);
 
178
        G_srand48(seed_value);
 
179
        seeded = 1;
 
180
        G_debug(3, "Read random seed from seed=: %ld", seed_value);
 
181
    }
 
182
 
 
183
    if (random->answer) {
 
184
        seed_value = G_srand48_auto();
 
185
        seeded = 1;
 
186
        G_debug(3, "Generated random seed (-s): %ld", seed_value);
 
187
    }
134
188
 
135
189
    pre_exec();
136
190
    execute(result);
139
193
    all_ok = 1;
140
194
 
141
195
    if (floating_point_exception_occurred) {
142
 
        G_warning(_("Floating point error(s) occured in the calculation"));
 
196
        G_warning(_("Floating point error(s) occurred in the calculation"));
143
197
        all_ok = 0;
144
198
    }
145
199
 
146
200
    if (overflow_occurred) {
147
 
        G_warning(_("Overflow occured in the calculation"));
 
201
        G_warning(_("Overflow occurred in the calculation"));
148
202
        all_ok = 0;
149
203
    }
150
204