GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
ls_groups.c
Go to the documentation of this file.
1 
2 /*************************************************************
3 * I_list_groups (full)
4 * I_list_subgroups (group, full)
5 *************************************************************/
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <grass/imagery.h>
11 #include <grass/spawn.h>
12 
13 static char *tempfile = NULL;
14 
15 
16 int I_list_groups(int full)
17 {
18  char *element;
19  int i;
20 
21  char buf[GPATH_MAX];
22  char title[50];
23  FILE *ls, *temp;
24  struct Ref ref;
25  int any;
26 
27  if (tempfile == NULL)
28  tempfile = G_tempfile();
29 
30  element = "group";
31  G__make_mapset_element(element);
32 
33  temp = fopen(tempfile, "w");
34  if (temp == NULL)
35  G_fatal_error("can't open any temp files");
36  fprintf(temp, "Available groups\n");
37  fprintf(temp, "---------------------------------\n");
38 
39  any = 0;
40  strcpy(buf, "cd ");
41  G__file_name(buf + strlen(buf), element, "", G_mapset());
42  strcat(buf, ";ls");
43  if (!full)
44  strcat(buf, " -C");
45  /* FIXME: use G__ls() */
46  if ((ls = popen(buf, "r"))) {
47  while (G_getl2(buf, sizeof(buf), ls)) {
48  any = 1;
49  fprintf(temp, "%s", buf);
50  if (full) {
51  I_get_group_title(buf, title, sizeof(title));
52  if (*title)
53  fprintf(temp, " (%s)", title);
54  fprintf(temp, "\n");
55  I_get_group_ref(buf, &ref);
56  for (i = 0; i < ref.nfiles; i++)
57  fprintf(temp, "\t%s in %s\n", ref.file[i].name,
58  ref.file[i].mapset);
59  if (ref.nfiles <= 0)
60  fprintf(temp, "\t** empty **\n");
61  I_free_group_ref(&ref);
62  }
63  else
64  fprintf(temp, "\n");
65  }
66  pclose(ls);
67  }
68  if (!any)
69  fprintf(temp, "no group files available\n");
70  fprintf(temp, "---------------------------------\n");
71  fclose(temp);
72  G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
73  remove(tempfile);
74  fprintf(stdout, "hit RETURN to continue -->");
75  fflush(stdout);
76  G_gets(buf);
77 
78  return 0;
79 }
80 
81 int I_list_subgroups(const char *group, int full)
82 {
83  char element[GNAME_MAX + 15];
84  int i;
85 
86  char buf[GPATH_MAX];
87  FILE *ls, *temp;
88  struct Ref ref;
89  int any;
90 
91  if (tempfile == NULL)
92  tempfile = G_tempfile();
93 
94  sprintf(element, "group/%s/subgroup", group);
95  G__make_mapset_element(element);
96 
97  temp = fopen(tempfile, "w");
98  if (temp == NULL)
99  G_fatal_error("Unable to open any temporary file");
100  fprintf(temp, "Available subgroups in group %s\n", group);
101  fprintf(temp, "---------------------------------\n");
102 
103  any = 0;
104  strcpy(buf, "cd ");
105  G__file_name(buf + strlen(buf), element, "", G_mapset());
106  strcat(buf, ";ls");
107  if (!full)
108  strcat(buf, " -C");
109  /* FIXME: use G__ls() */
110  if ((ls = popen(buf, "r"))) {
111  while (G_getl2(buf, sizeof(buf), ls)) {
112  any = 1;
113  fprintf(temp, "%s\n", buf);
114  if (full) {
115  I_get_subgroup_ref(group, buf, &ref);
116  for (i = 0; i < ref.nfiles; i++)
117  fprintf(temp, "\t%s in %s\n", ref.file[i].name,
118  ref.file[i].mapset);
119  if (ref.nfiles <= 0)
120  fprintf(temp, "\t** empty **\n");
121  I_free_group_ref(&ref);
122  }
123  }
124  pclose(ls);
125  }
126  if (!any)
127  fprintf(temp, "no subgroup files available\n");
128  fprintf(temp, "---------------------------------\n");
129  fclose(temp);
130  G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
131  remove(tempfile);
132  fprintf(stdout, "hit RETURN to continue -->");
133  fflush(stdout);
134  G_gets(buf);
135 
136  return 0;
137 }