GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_gets(char *buf)
Definition: gets.c:39
int I_get_group_title(const char *group, char *title, int n)
Definition: title.c:5
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:47
char * getenv()
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
int I_list_groups(int full)
Definition: ls_groups.c:16
int I_list_subgroups(const char *group, int full)
Definition: ls_groups.c:81
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
fclose(fd)
int I_get_group_ref(const char *group, struct Ref *ref)
read group REF file
Definition: group.c:114
int I_free_group_ref(struct Ref *ref)
free Ref structure
Definition: group.c:452
int G__make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:34
int I_get_subgroup_ref(const char *group, const char *subgroup, struct Ref *ref)
read subgroup REF file
Definition: group.c:134
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_spawn(const char *command,...)
Spawn new process based on command.
Definition: spawn.c:924