GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
group.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: imagery library
5  * AUTHOR(S): Original author(s) name(s) unknown - written by CERL
6  * PURPOSE: Image processing library
7  * COPYRIGHT: (C) 1999, 2005 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public
10  * License (>=v2). Read the file COPYING that comes with GRASS
11  * for details.
12  *
13  *****************************************************************************/
14 
15 /**********************************************************
16 * I_get_group (group);
17 * I_put_group (group);
18 *
19 * I_get_group_ref (group, &Ref);
20 * I_put_group_ref (group, &Ref);
21 * I_get_subgroup_ref_file (group, subgroup, &Ref);
22 * I_put_subgroup_ref_file (group, subgroup, &Ref);
23 * I_add_file_to_group_ref (name, mapset, &Ref)
24 * I_transfer_group_ref_file (&Src_ref, n, &Dst_ref)
25 * I_init_group_ref (&Ref);
26 * I_free_group_ref (&Ref);
27 **********************************************************/
28 
29 #include <string.h>
30 #include <stdlib.h>
31 #include <grass/imagery.h>
32 
33 static int get_ref(const char *, const char *, struct Ref *);
34 static int set_color(const char *, const char *, const char *, struct Ref *);
35 static int put_ref(const char *, const char *, const struct Ref *);
36 
37 /* get current group name from file GROUPFILE in current mapset */
38 int I_get_group(char *group)
39 {
40  FILE *fd;
41  int stat;
42 
43  *group = 0;
45  fd = G_fopen_old("", GROUPFILE, G_mapset());
47  if (fd == NULL)
48  return 0;
49  stat = (fscanf(fd, "%s", group) == 1);
50  fclose(fd);
51  return stat;
52 }
53 
54 /* write group name to file GROUPFILE in current mapset */
55 int I_put_group(const char *group)
56 {
57  FILE *fd;
58 
59  fd = G_fopen_new("", GROUPFILE);
60  if (fd == NULL)
61  return 0;
62  fprintf(fd, "%s\n", group);
63  fclose(fd);
64  return 1;
65 }
66 
67 /* get current subgroup for group in current mapset */
68 int I_get_subgroup(const char *group, char *subgroup)
69 {
70  FILE *fd;
71  int stat;
72 
73  *subgroup = 0;
74  if (!I_find_group(group))
75  return 0;
77  fd = I_fopen_group_file_old(group, SUBGROUPFILE);
79  if (fd == NULL)
80  return 0;
81  stat = (fscanf(fd, "%s", subgroup) == 1);
82  fclose(fd);
83  return stat;
84 }
85 
86 /* write current subgroup to group in current mapset */
87 int I_put_subgroup(const char *group, const char *subgroup)
88 {
89  FILE *fd;
90 
91  if (!I_find_group(group))
92  return 0;
93  fd = I_fopen_group_file_new(group, SUBGROUPFILE);
94  if (fd == NULL)
95  return 0;
96  fprintf(fd, "%s\n", subgroup);
97  fclose(fd);
98  return 1;
99 }
100 
101 
114 int I_get_group_ref(const char *group, struct Ref *ref)
115 {
116  return get_ref(group, "", ref);
117 }
118 
119 
134 int I_get_subgroup_ref(const char *group,
135  const char *subgroup, struct Ref *ref)
136 {
137  return get_ref(group, subgroup, ref);
138 }
139 
140 static int get_ref(const char *group, const char *subgroup, struct Ref *ref)
141 {
142  int n;
143  char buf[1024];
144  char name[INAME_LEN], mapset[INAME_LEN];
145  char color[20];
146  FILE *fd;
147 
148  I_init_group_ref(ref);
149 
151  if (*subgroup == 0)
152  fd = I_fopen_group_ref_old(group);
153  else
154  fd = I_fopen_subgroup_ref_old(group, subgroup);
156  if (!fd)
157  return 0;
158 
159  while (G_getl2(buf, sizeof buf, fd)) {
160  n = sscanf(buf, "%255s %255s %15s", name, mapset, color); /* better use INAME_LEN */
161  if (n == 2 || n == 3) {
162  I_add_file_to_group_ref(name, mapset, ref);
163  if (n == 3)
164  set_color(name, mapset, color, ref);
165  }
166  }
167  /* make sure we have a color assignment */
169 
170  fclose(fd);
171  return 1;
172 }
173 
174 static int set_color(const char *name, const char *mapset, const char *color,
175  struct Ref *ref)
176 {
177  int n;
178 
179  for (n = 0; n < ref->nfiles; n++) {
180  if (strcmp(ref->file[n].name, name) == 0
181  && strcmp(ref->file[n].mapset, mapset) == 0)
182  break;
183  }
184 
185  if (n < ref->nfiles)
186  while (*color) {
187  switch (*color) {
188  case 'r':
189  case 'R':
190  if (ref->red.n < 0)
191  ref->red.n = n;
192  break;
193  case 'g':
194  case 'G':
195  if (ref->grn.n < 0)
196  ref->grn.n = n;
197  break;
198  case 'b':
199  case 'B':
200  if (ref->blu.n < 0)
201  ref->blu.n = n;
202  break;
203  }
204  color++;
205  }
206 
207  return 0;
208 }
209 
210 int I_init_ref_color_nums(struct Ref *ref)
211 {
212  ref->red.table = NULL;
213  ref->grn.table = NULL;
214  ref->blu.table = NULL;
215 
216  ref->red.index = NULL;
217  ref->grn.index = NULL;
218  ref->blu.index = NULL;
219 
220  if (ref->nfiles <= 0 || ref->red.n >= 0 || ref->blu.n >= 0 ||
221  ref->blu.n >= 0)
222  return 1;
223  switch (ref->nfiles) {
224  case 1:
225  ref->red.n = 0;
226  ref->grn.n = 0;
227  ref->blu.n = 0;
228  break;
229  case 2:
230  ref->blu.n = 0;
231  ref->grn.n = 1;
232  break;
233  case 3:
234  ref->blu.n = 0;
235  ref->grn.n = 1;
236  ref->red.n = 2;
237  break;
238  case 4:
239  ref->blu.n = 0;
240  ref->grn.n = 1;
241  ref->red.n = 3;
242  break;
243  default:
244  ref->blu.n = 1;
245  ref->grn.n = 2;
246  ref->red.n = 4;
247  break;
248  }
249 
250  return 0;
251 }
252 
253 
268 int I_put_group_ref(const char *group, const struct Ref *ref)
269 {
270  return put_ref(group, "", ref);
271 }
272 
273 
290 int I_put_subgroup_ref(const char *group, const char *subgroup,
291  const struct Ref *ref)
292 {
293  return put_ref(group, subgroup, ref);
294 }
295 
296 static int put_ref(const char *group, const char *subgroup,
297  const struct Ref *ref)
298 {
299  int n;
300  FILE *fd;
301 
302  if (*subgroup == 0)
303  fd = I_fopen_group_ref_new(group);
304  else
305  fd = I_fopen_subgroup_ref_new(group, subgroup);
306  if (!fd)
307  return 0;
308 
309  for (n = 0; n < ref->nfiles; n++) {
310  fprintf(fd, "%s %s", ref->file[n].name, ref->file[n].mapset);
311  if (n == ref->red.n || n == ref->grn.n || n == ref->blu.n) {
312  fprintf(fd, " ");
313  if (n == ref->red.n)
314  fprintf(fd, "r");
315  if (n == ref->grn.n)
316  fprintf(fd, "g");
317  if (n == ref->blu.n)
318  fprintf(fd, "b");
319  }
320  fprintf(fd, "\n");
321  }
322  fclose(fd);
323  return 1;
324 }
325 
326 
346 int I_add_file_to_group_ref(const char *name, const char *mapset,
347  struct Ref *ref)
348 {
349  int n;
350 
351  for (n = 0; n < ref->nfiles; n++) {
352  if (strcmp(ref->file[n].name, name) == 0
353  && strcmp(ref->file[n].mapset, mapset) == 0)
354  return n;
355  }
356 
357  if ((n = ref->nfiles++))
358  ref->file =
359  (struct Ref_Files *)G_realloc(ref->file,
360  ref->nfiles *
361  sizeof(struct Ref_Files));
362  else
363  ref->file =
364  (struct Ref_Files *)G_malloc(ref->nfiles *
365  sizeof(struct Ref_Files));
366  strcpy(ref->file[n].name, name);
367  strcpy(ref->file[n].mapset, mapset);
368  return n;
369 }
370 
371 
398 int I_transfer_group_ref_file(const struct Ref *ref2, int n, struct Ref *ref1)
399 {
400  int k;
401 
402  /* insert old name into new ref */
403  k = I_add_file_to_group_ref(ref2->file[n].name, ref2->file[n].mapset,
404  ref1);
405 
406  /* preserve color assignment */
407  if (n == ref2->red.n)
408  ref1->red.n = k;
409  if (n == ref2->grn.n)
410  ref1->grn.n = k;
411  if (n == ref2->blu.n)
412  ref1->blu.n = k;
413 
414  return 0;
415 }
416 
417 
418 
433 int I_init_group_ref(struct Ref *ref)
434 {
435  ref->nfiles = 0;
436  ref->red.n = ref->grn.n = ref->blu.n = -1;
437  ref->red.table = ref->grn.table = ref->blu.table = NULL;
438 
439  return 0;
440 }
441 
442 
452 int I_free_group_ref(struct Ref *ref)
453 {
454  if (ref->nfiles > 0)
455  free(ref->file);
456  ref->nfiles = 0;
457 
458  return 0;
459 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int I_add_file_to_group_ref(const char *name, const char *mapset, struct Ref *ref)
add file name to Ref structure
Definition: group.c:346
int I_put_subgroup(const char *group, const char *subgroup)
Definition: group.c:87
int I_put_group_ref(const char *group, const struct Ref *ref)
write group REF file
Definition: group.c:268
int I_init_group_ref(struct Ref *ref)
initialize Ref structure
Definition: group.c:433
int I_get_subgroup(const char *group, char *subgroup)
Definition: group.c:68
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
int I_init_ref_color_nums(struct Ref *ref)
Definition: group.c:210
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
int I_get_group(char *group)
Definition: group.c:38
int I_find_group(const char *group)
does group exist?
Definition: imagery/find.c:21
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
int stat
Definition: g3dcolor.c:369
tuple color
Definition: tools.py:1703
int I_put_subgroup_ref(const char *group, const char *subgroup, const struct Ref *ref)
write subgroup REF file
Definition: group.c:290
FILE * I_fopen_subgroup_ref_new(const char *group, const char *subgroup)
Definition: ref.c:44
FILE * I_fopen_group_file_old(const char *group, const char *file)
Definition: fopen.c:43
FILE * I_fopen_group_file_new(const char *group, const char *file)
Definition: fopen.c:17
FILE * I_fopen_group_ref_old(const char *group)
Definition: ref.c:30
int I_transfer_group_ref_file(const struct Ref *ref2, int n, struct Ref *ref1)
copy Ref lists
Definition: group.c:398
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:197
return NULL
Definition: dbfopen.c:1394
int I_put_group(const char *group)
Definition: group.c:55
fclose(fd)
int I_get_group_ref(const char *group, struct Ref *ref)
read group REF file
Definition: group.c:114
void free(void *)
int I_free_group_ref(struct Ref *ref)
free Ref structure
Definition: group.c:452
int I_get_subgroup_ref(const char *group, const char *subgroup, struct Ref *ref)
read subgroup REF file
Definition: group.c:134
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
FILE * I_fopen_group_ref_new(const char *group)
Definition: ref.c:25
FILE * I_fopen_subgroup_ref_old(const char *group, const char *subgroup)
Definition: ref.c:49
int n
Definition: dataquad.c:291