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 char *tmp_name = rname, *tmp_mapset = rmapset;
148 fd = fopen_cellhd_old(
name, mapset);
151 char *error_message =
NULL;
152 reclass->
type = reclass_type(fd, &tmp_name, &tmp_mapset, &error_message);
155 if (reclass->
type == 0) {
158 return reclass->
type;
160 if (reclass->
type < 0) {
163 G_warning(
_(
"Error reading beginning of header file for <%s@%s>: %s"),
164 name, mapset, error_message);
165 if (error_message !=
NULL)
167 return reclass->
type;
170 switch (reclass->
type) {
172 stat = get_reclass_table(fd, reclass, &error_message);
185 _(
"Illegal reclass format in header file for <%s@%s>: %s"),
186 name, mapset, error_message);
189 if (error_message !=
NULL)
201 switch (reclass->
type) {
203 if (reclass->
num > 0)
228 static int reclass_type(FILE *fd,
char **rname,
char **rmapset,
229 char **error_message)
238 if (fgets(buf,
sizeof(buf), fd) ==
NULL)
240 if (strncmp(buf,
"reclas", 6))
250 for (i = 0; i < 2; i++) {
251 if (fgets(buf,
sizeof buf, fd) ==
NULL) {
252 if (error_message !=
NULL) {
253 G_asprintf(error_message,
_(
"File too short, reading line %d"),
258 if (buf[strlen(buf) - 1] !=
'\n') {
259 if (error_message !=
NULL) {
260 G_asprintf(error_message,
_(
"Line too long: %s..."), buf);
264 if (sscanf(buf,
"%[^:]:%s", label, arg) != 2) {
265 if (error_message !=
NULL) {
266 G_asprintf(error_message,
_(
"Format is not key:value: %s"),
271 if (strncmp(label,
"maps", 4) == 0 && *rmapset) {
274 else if (strncmp(label,
"name", 4) == 0 && *rname) {
278 if (error_message !=
NULL) {
279 G_asprintf(error_message,
_(
"Unknown key at line: %s"), buf);
284 if ((*rmapset && **rmapset) || (*rname && **rname))
288 if (**rname && error_message !=
NULL) {
290 _(
"Mapset not read, only raster name: %s"), *rname);
292 else if (**rmapset && error_message !=
NULL) {
294 _(
"Raster name not read, only mapset: %s"), *rmapset);
296 else if (error_message !=
NULL) {
297 *error_message =
G_store(
_(
"Raster name and mapset not read"));
303 static FILE *fopen_cellhd_old(
const char *
name,
const char *mapset)
325 switch (reclass->
type) {
327 if (reclass->
min > reclass->
max || reclass->
num <= 0) {
337 fd = fopen_cellhd_new(
name);
344 fprintf(fd,
"reclass\n");
345 fprintf(fd,
"name: %s\n", reclass->
name);
346 fprintf(fd,
"mapset: %s\n", reclass->
mapset);
366 fprintf(fd,
"#%ld\n", (
long)reclass->
min +
min);
369 fprintf(fd,
"%s\n", NULL_STRING);
371 fprintf(fd,
"%ld\n", (
long)reclass->
table[
min]);
378 if ((p = strchr(buf2,
'@')))
384 fd = fopen(buf1,
"a+");
387 G_warning(
_(
"Unable to create dependency file in <%s@%s>"),
400 if (!
G_getl2(buf,
sizeof(buf), fd))
402 if (strcmp(xname, buf) == 0) {
409 fprintf(fd,
"%s\n", xname);
417 static FILE *fopen_cellhd_new(
const char *
name)
436 static int get_reclass_table(FILE *fd,
struct Reclass *reclass,
437 char **error_message)
441 int first, null_str_size;
452 null_str_size = strlen(NULL_STRING);
455 bool min_set =
false;
456 while (fgets(buf,
sizeof buf, fd)) {
459 if (sscanf(buf,
"#%d", &cat) == 1) {
465 if (strncmp(buf, NULL_STRING, null_str_size) == 0)
468 if (sscanf(buf,
"%d", &cat) != 1) {
471 if (error_message !=
NULL) {
474 _(
"Reading integer failed on line: %s "
475 "(after reading min: %d)"),
479 _(
"First entry (min) not read yet and "
480 "reading integer failed on line: %s"),
487 len = (long)n *
sizeof(
CELL);
489 if (len != (
int)len) {
495 reclass->
table[n - 1] = cat;
497 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.
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
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_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.
int Rast_is_reclass(const char *name, const char *mapset, char rname[256], char rmapset[256])
Check if raster map is reclassified.