21 static const char NULL_STRING[] =
"null";
22 static int reclass_type(FILE *,
char **,
char **,
char **);
23 static FILE *fopen_cellhd_old(
const char *,
const char *);
24 static FILE *fopen_cellhd_new(
const char *);
25 static int get_reclass_table(FILE *,
struct Reclass *,
char **);
50 fd = fopen_cellhd_old(
name, mapset);
54 type = reclass_type(fd, &rname, &rmapset,
NULL);
85 char buf2[256], buf3[256];
95 for (i = 0; !feof(fd) && fgets(buf2, 255, fd);) {
97 for (j = 0, k = 0; j <
l; j++) {
99 ((buf2[j] ==
' ' || buf2[j] ==
'\t' || buf2[j] ==
'\n') && k))
101 else if (buf2[j] !=
' ' && buf2[j] !=
'\t')
109 *rmaps = (
char **)
G_realloc(*rmaps, i *
sizeof(
char *));
110 (*rmaps)[i - 1] = (
char *)
G_malloc(k + 1);
111 strncpy((*rmaps)[i - 1], buf3, k);
112 (*rmaps)[i - 1][k] = 0;
122 *rmaps = (
char **)
G_realloc(*rmaps, i *
sizeof(
char *));
123 (*rmaps)[i - 1] =
NULL;
146 fd = fopen_cellhd_old(
name, mapset);
151 char *error_message =
NULL;
153 reclass_type(fd, &reclass->
name, &reclass->
mapset, &error_message);
154 if (reclass->
type == 0) {
157 return reclass->
type;
159 if (reclass->
type < 0) {
162 G_warning(
_(
"Error reading beginning of header file for <%s@%s>: %s"),
163 name, mapset, error_message);
164 if (error_message !=
NULL)
166 return reclass->
type;
169 switch (reclass->
type) {
171 stat = get_reclass_table(fd, reclass, &error_message);
184 _(
"Illegal reclass format in header file for <%s@%s>: %s"),
185 name, mapset, error_message);
188 if (error_message !=
NULL)
200 switch (reclass->
type) {
202 if (reclass->
num > 0)
227 static int reclass_type(FILE *fd,
char **rname,
char **rmapset,
228 char **error_message)
237 if (fgets(buf,
sizeof(buf), fd) ==
NULL)
239 if (strncmp(buf,
"reclas", 6))
249 for (i = 0; i < 2; i++) {
250 if (fgets(buf,
sizeof buf, fd) ==
NULL) {
251 if (error_message !=
NULL) {
252 G_asprintf(error_message,
_(
"File too short, reading line %d"),
257 if (buf[strlen(buf) - 1] !=
'\n') {
258 if (error_message !=
NULL) {
259 G_asprintf(error_message,
_(
"Line too long: %s..."), buf);
263 if (sscanf(buf,
"%[^:]:%s", label, arg) != 2) {
264 if (error_message !=
NULL) {
265 G_asprintf(error_message,
_(
"Format is not key:value: %s"),
270 if (strncmp(label,
"maps", 4) == 0) {
276 else if (strncmp(label,
"name", 4) == 0) {
283 if (error_message !=
NULL) {
284 G_asprintf(error_message,
_(
"Unknown key at line: %s"), buf);
289 if (**rmapset && **rname)
293 if (**rname && error_message !=
NULL) {
295 _(
"Mapset not read, only raster name: %s"), *rname);
297 else if (**rmapset && error_message !=
NULL) {
299 _(
"Raster name not read, only mapset: %s"), *rmapset);
301 else if (error_message !=
NULL) {
302 *error_message =
G_store(
_(
"Raster name and mapset not read"));
308 static FILE *fopen_cellhd_old(
const char *
name,
const char *mapset)
330 switch (reclass->
type) {
332 if (reclass->
min > reclass->
max || reclass->
num <= 0) {
342 fd = fopen_cellhd_new(
name);
349 fprintf(fd,
"reclass\n");
350 fprintf(fd,
"name: %s\n", reclass->
name);
351 fprintf(fd,
"mapset: %s\n", reclass->
mapset);
371 fprintf(fd,
"#%ld\n", (
long)reclass->
min +
min);
374 fprintf(fd,
"%s\n", NULL_STRING);
376 fprintf(fd,
"%ld\n", (
long)reclass->
table[
min]);
383 if ((p = strchr(buf2,
'@')))
389 fd = fopen(buf1,
"a+");
392 G_warning(
_(
"Unable to create dependency file in <%s@%s>"),
405 if (!
G_getl2(buf,
sizeof(buf), fd))
407 if (strcmp(xname, buf) == 0) {
414 fprintf(fd,
"%s\n", xname);
422 static FILE *fopen_cellhd_new(
const char *
name)
441 static int get_reclass_table(FILE *fd,
struct Reclass *reclass,
442 char **error_message)
446 int first, null_str_size;
457 null_str_size = strlen(NULL_STRING);
460 bool min_set =
false;
461 while (fgets(buf,
sizeof buf, fd)) {
464 if (sscanf(buf,
"#%d", &cat) == 1) {
470 if (strncmp(buf, NULL_STRING, null_str_size) == 0)
473 if (sscanf(buf,
"%d", &cat) != 1) {
476 if (error_message !=
NULL) {
479 _(
"Reading integer failed on line: %s "
480 "(after reading min: %d)"),
484 _(
"First entry (min) not read yet and "
485 "reading integer failed on line: %s"),
492 len = (long)n *
sizeof(
CELL);
494 if (len != (
int)len) {
500 reclass->
table[n - 1] = cat;
502 reclass->
max = reclass->
min + n - 1;
char * G_file_name_misc(char *, const char *, const char *, const char *, const char *)
Builds full path names to GIS misc data files.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
void G_free(void *)
Free allocated memory.
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database misc file for reading
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
const char * G_mapset(void)
Get current mapset name.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
char * G_store(const char *)
Copy string to allocated memory.
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
#define Rast_is_c_null_value(cellVal)
int Rast_is_reclass(const char *name, const char *mapset, char *rname, char *rmapset)
Check if raster map is reclassified.
int Rast_get_reclass(const char *name, const char *mapset, struct Reclass *reclass)
Get reclass.
int Rast_put_reclass(const char *name, const struct Reclass *reclass)
Put reclass.
int Rast_is_reclassed_to(const char *name, const char *mapset, int *nrmaps, char ***rmaps)
Get child reclass maps list.
void Rast_free_reclass(struct Reclass *reclass)
Free Reclass structure.