78
82
/****************************************************************************/
80
static const char *join(int argc, char **argv)
84
static expr_list *parse_file(const char *filename)
86
for (i = 0; i < argc; i++)
87
size += strlen(argv[i]) + 1;
91
for (i = 0; i < argc; i++) {
89
if (strcmp(filename, "-") == 0)
90
return parse_stream(stdin);
92
fp = fopen(filename, "r");
94
G_fatal_error(_("Unable to open input file <%s>"), filename);
96
res = parse_stream(fp);
100
103
/****************************************************************************/
102
105
int main(int argc, char **argv)
107
struct GModule *module;
108
struct Option *expr, *file, *seed;
107
112
G_gisinit(argv[0]);
109
if (argc > 1 && (strcmp(argv[1], "help") == 0 ||
110
strcmp(argv[1], "--help") == 0)) {
112
_("r.mapcalc - Raster map layer data calculator\n"
114
"usage: r.mapcalc '<map>=<expression>'\n"
116
"r.mapcalc performs arithmetic on raster map layers.\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"),
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;
120
expr = G_define_option();
121
expr->key = "expression";
122
expr->type = TYPE_STRING;
124
expr->description = _("Expression to evaluate");
125
expr->guisection = _("Expression");
127
file = G_define_standard_option(G_OPT_F_INPUT);
130
file->description = _("File containing expression(s) to evaluate");
131
file->guisection = _("Expression");
133
seed = G_define_option();
135
seed->type = TYPE_INTEGER;
137
seed->description = _("Seed for rand() function");
139
random = G_define_flag();
141
random->description = _("Generate random seed (result is non-deterministic)");
145
char **p = G_malloc(3 * sizeof(char *));
147
p[1] = G_store("file=-");
126
? parse_string(join(argc - 1, argv + 1))
127
: parse_stream(stdin);
153
if (G_parser(argc, argv))
156
overwrite_flag = module->overwrite;
158
if (expr->answer && file->answer)
159
G_fatal_error(_("%s= and %s= are mutually exclusive"),
160
expr->key, file->key);
162
if (seed->answer && random->answer)
163
G_fatal_error(_("%s= and -%c are mutually exclusive"),
164
seed->key, random->key);
167
result = parse_string(expr->answer);
168
else if (file->answer)
169
result = parse_file(file->answer);
171
result = parse_stream(stdin);
132
overwrite = G_check_overwrite(argc, argv);
174
G_fatal_error(_("parse error"));
177
seed_value = atol(seed->answer);
178
G_srand48(seed_value);
180
G_debug(3, "Read random seed from seed=: %ld", seed_value);
183
if (random->answer) {
184
seed_value = G_srand48_auto();
186
G_debug(3, "Generated random seed (-s): %ld", seed_value);