-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
mask.c
102 lines (82 loc) · 2.27 KB
/
mask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <string.h>
#include "kappa.h"
#include "local_proto.h"
#include <grass/raster.h>
#include <grass/glocale.h>
/* function prototypes */
static int reclass_text(char *text, struct Reclass *reclass, int next);
static void do_text(char *text, long first, long last);
static char *append(char *results, char *text);
char *maskinfo(void)
{
struct Reclass reclass;
char *results, text[100];
int first, next;
results = NULL;
if (G_find_raster("MASK", G_mapset()) == NULL)
return "none";
if (Rast_get_reclass("MASK", G_mapset(), &reclass) <= 0) {
sprintf(text, "MASK in %s", G_mapset());
return append(results, text);
}
sprintf(text, "%s in %s", reclass.name, reclass.mapset);
results = append(results, text);
next = 0;
first = 1;
do {
next = reclass_text(text, &reclass, next);
if (*text == 0)
break;
if (first) {
first = 0;
results = append(results, ", categories");
}
results = append(results, " ");
results = append(results, text);
} while (next >= 0);
Rast_free_reclass(&reclass);
return results;
}
static int reclass_text(char *text, struct Reclass *reclass, int next)
{
int i, n, first;
*text = 0;
n = reclass->num;
first = -1;
for (i = next; i < n; i++) {
if (reclass->table[i]) {
if (first < 0)
first = i;
}
else if (first >= 0) {
do_text(text, (long)(first + reclass->min),
(long)(i - 1 + reclass->min));
first = -1;
if (strlen(text) > 60)
return i;
}
}
if (first >= 0)
do_text(text, (long)(first + reclass->min),
(long)(i - 1 + reclass->min));
return -1;
}
static void do_text(char *text, long first, long last)
{
char work[40];
if (*text)
strcat(text, " ");
if (first == last)
sprintf(work, "%ld", first);
else
sprintf(work, "%ld-%ld", first, last);
strcat(text, work);
}
static char *append(char *results, char *text)
{
if (results == NULL)
return G_store(text);
results = G_realloc(results, strlen(results) + strlen(text) + 1);
strcat(results, text);
return results;
}