3 #include <grass/glocale.h>
5 static char *NULL_STRING =
"null";
6 static int reclass_type(FILE *,
char **,
char **);
7 static FILE *fopen_cellhd_old(
const char *,
const char *);
8 static FILE *fopen_cellhd_new(
const char *);
9 static int get_reclass_table(FILE *,
struct Reclass *);
36 fd = fopen_cellhd_old(name, mapset);
40 type = reclass_type(fd, &rname, &rmapset);
71 char buf2[256], buf3[256];
81 for (i = 0; !feof(fd) && fgets(buf2, 255, fd);) {
83 for (j = 0, k = 0; j <
l; j++) {
85 ((buf2[j] ==
' ' || buf2[j] ==
'\t' || buf2[j] ==
'\n') && k))
87 else if (buf2[j] !=
' ' && buf2[j] !=
'\t')
95 *rmaps = (
char **)G_realloc(*rmaps, i *
sizeof(
char *));
96 (*rmaps)[i - 1] = (
char *)G_malloc(k + 1);
97 strncpy((*rmaps)[i - 1], buf3, k);
98 (*rmaps)[i - 1][k] = 0;
108 *rmaps = (
char **)G_realloc(*rmaps, i *
sizeof(
char *));
109 (*rmaps)[i - 1] =
NULL;
118 struct Reclass *reclass)
123 fd = fopen_cellhd_old(name, mapset);
126 reclass->name =
NULL;
127 reclass->mapset =
NULL;
128 reclass->type = reclass_type(fd, &reclass->name, &reclass->mapset);
129 if (reclass->type <= 0) {
131 return reclass->type;
134 switch (reclass->type) {
136 stat = get_reclass_table(fd, reclass);
145 G_warning(_(
"Too many reclass categories for [%s in %s]"),
148 G_warning(_(
"Illegal reclass format in header file for [%s in %s]"),
157 switch (reclass->type) {
159 if (reclass->num > 0)
166 reclass->name =
NULL;
167 reclass->mapset =
NULL;
176 static int reclass_type(FILE *
fd,
char **rname,
char **rmapset)
179 char label[128], arg[128];
184 if (fgets(buf,
sizeof(buf), fd) ==
NULL)
186 if (strncmp(buf,
"reclas", 6))
189 type = RECLASS_TABLE;
196 for (i = 0; i < 2; i++) {
197 if (fgets(buf,
sizeof buf, fd) ==
NULL)
199 if (sscanf(buf,
"%[^:]:%s", label, arg) != 2)
201 if (strncmp(label,
"maps", 4) == 0) {
203 strcpy(*rmapset, arg);
207 else if (strncmp(label,
"name", 4) == 0) {
216 if (**rmapset && **rname)
222 static FILE *fopen_cellhd_old(
const char *
name,
const char *mapset)
232 char buf1[GPATH_MAX],
buf2[GNAME_MAX], *p;
235 switch (reclass->type) {
237 if (reclass->min > reclass->max || reclass->num <= 0) {
247 fd = fopen_cellhd_new(name);
249 G_warning(_(
"Unable to create header file for [%s in %s]"),
254 fprintf(fd,
"reclass\n");
255 fprintf(fd,
"name: %s\n", reclass->name);
256 fprintf(fd,
"mapset: %s\n", reclass->mapset);
259 for (min = 0; min < reclass->num; min++)
263 for (max = reclass->num - 1; max >= 0; max--)
276 fprintf(fd,
"#%ld\n", (
long)reclass->min + min);
279 fprintf(fd,
"%s\n", NULL_STRING);
281 fprintf(fd,
"%ld\n", (
long)reclass->table[min]);
287 strcpy(buf2, reclass->name);
288 if ((p = strchr(buf2,
'@')))
294 fd = fopen(buf1,
"a+");
297 G_warning(_(
"Unable to create dependency file in [%s in %s]"),
298 buf2, reclass->mapset);
303 fseek(fd, 0L, SEEK_SET);
308 char buf[GNAME_MAX + GMAPSET_MAX];
309 if (!
G_getl2(buf,
sizeof(buf), fd))
311 if (strcmp(xname, buf) == 0) {
318 fprintf(fd,
"%s\n", xname);
326 static FILE *fopen_cellhd_new(
const char *
name)
331 static int get_reclass_table(FILE * fd,
struct Reclass *reclass)
335 int first, null_str_size;
345 reclass->table =
NULL;
346 null_str_size = strlen(NULL_STRING);
349 while (fgets(buf,
sizeof buf, fd)) {
352 if (sscanf(buf,
"#%d", &cat) == 1) {
357 if (strncmp(buf, NULL_STRING, null_str_size) == 0)
360 if (sscanf(buf,
"%d", &cat) != 1)
364 len = (long)n *
sizeof(CELL);
366 if (len != (
int)len) {
367 if (reclass->table !=
NULL)
371 reclass->table = (CELL *) G_realloc((
char *)reclass->table, (
int)len);
372 reclass->table[n - 1] =
cat;
374 reclass->max = reclass->min + n - 1;
char * G_mapset(void)
current mapset name
int G_is_c_null_value(const CELL *cellVal)
Returns 1 if cell is NULL, 0 otherwise. This will test if the value cell is the largest int...
void G_free(void *buf)
Free allocated memory.
char * G_store(const char *s)
Copy string to allocated memory.
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
int G_is_reclassed_to(const char *name, const char *mapset, int *nrmaps, char ***rmaps)
get child reclass maps list
int G_put_reclass(const char *name, const struct Reclass *reclass)
int G_is_reclass(const char *name, const char *mapset, char *rname, char *rmapset)
reclass file?
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
int G_free_reclass(struct Reclass *reclass)
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
char * G_fully_qualified_name(const char *name, const char *mapset)
fully qualified file name
void G_set_c_null_value(CELL *cellVals, int numVals)
int G_get_reclass(const char *name, const char *mapset, struct Reclass *reclass)
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.