GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/list.c
Go to the documentation of this file.
1 
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <signal.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <dirent.h>
22 
23 #include <grass/gis.h>
24 #include <grass/glocale.h>
25 
26 static int broken_pipe;
27 static int hit_return = 0;
28 static int list_element(FILE *, const char *, const char *, const char *,
29  int (*)(const char *, const char *, const char *));
30 static RETSIGTYPE sigpipe_catch(int);
31 
33 {
34  hit_return = flag;
35  return 0;
36 }
37 
60 int G_list_element(const char *element,
61  const char *desc,
62  const char *mapset,
63  int (*lister) (const char *, const char *, const char *))
64 {
65  int n;
66  FILE *more;
67  int count;
68 
69 #ifdef SIGPIPE
70  RETSIGTYPE (*sigpipe)(int);
71 #endif
72 
73  /* must catch broken pipe in case "more" quits */
74  broken_pipe = 0;
75 #ifdef SIGPIPE
76  sigpipe = signal(SIGPIPE, sigpipe_catch);
77 #endif
78 
79  count = 0;
80  if (desc == 0 || *desc == 0)
81  desc = element;
82 
83  /*
84  * popen() the more command to page the output
85  */
86  if (isatty(1)) {
87 #ifdef __MINGW32__
88  more = popen("%GRASS_PAGER%", "w");
89 #else
90  more = popen("$GRASS_PAGER", "w");
91 #endif
92  if (!more)
93  more = stdout;
94  }
95  else
96  more = stdout;
97 
98  fprintf(more, "----------------------------------------------\n");
99 
100  /*
101  * if no specific mapset is requested, list the mapsets
102  * from the mapset search list
103  * otherwise just list the specified mapset
104  */
105  if (mapset == 0 || *mapset == 0)
106  for (n = 0; !broken_pipe && (mapset = G__mapset_name(n)); n++)
107  count += list_element(more, element, desc, mapset, lister);
108  else
109  count += list_element(more, element, desc, mapset, lister);
110 
111  if (!broken_pipe) {
112  if (count == 0) {
113  if (mapset == 0 || *mapset == 0)
114  fprintf(more, _("no %s files available in current mapset\n"),
115  desc);
116  else
117  fprintf(more, _("no %s files available in mapset <%s>\n"),
118  desc, mapset);
119  }
120 
121  fprintf(more, "----------------------------------------------\n");
122  }
123  /*
124  * close the more
125  */
126  if (more != stdout)
127  pclose(more);
128 #ifdef SIGPIPE
129  signal(SIGPIPE, sigpipe);
130 #endif
131  if (hit_return && isatty(1)) {
132  fprintf(stderr, _("hit RETURN to continue -->"));
133  while (getchar() != '\n') ;
134  }
135 
136  return 0;
137 }
138 
139 static RETSIGTYPE sigpipe_catch(int n)
140 {
141  broken_pipe = 1;
142  signal(n, sigpipe_catch);
143 }
144 
145 static int list_element(FILE * out,
146  const char *element, const char *desc,
147  const char *mapset, int (*lister) (const char *,
148  const char *,
149  const char *))
150 {
151  char path[GPATH_MAX];
152  int count = 0;
153  char **list;
154  int i;
155 
156  /*
157  * convert . to current mapset
158  */
159  if (strcmp(mapset, ".") == 0)
160  mapset = G_mapset();
161 
162 
163  /*
164  * get the full name of the GIS directory within the mapset
165  * and list its contents (if it exists)
166  *
167  * if lister() routine is given, the ls command must give 1 name
168  */
169  G__file_name(path, element, "", mapset);
170  if (access(path, 0) != 0) {
171  fprintf(out, "\n");
172  return count;
173  }
174 
175  /*
176  * if a title so that we can call lister() with the names
177  * otherwise the ls must be forced into columnar form.
178  */
179 
180  list = G__ls(path, &count);
181 
182  if (count > 0) {
183  fprintf(out, _("%s files available in mapset <%s>:\n"), desc, mapset);
184  if (lister) {
185  char title[400];
186  char name[GNAME_MAX];
187 
188  *name = *title = 0;
189  lister(name, mapset, title);
190  if (*title)
191  fprintf(out, "\n%-18s %-.60s\n", name, title);
192  }
193  }
194 
195  if (lister) {
196  for (i = 0; i < count; i++) {
197  char title[400];
198 
199  lister(list[i], mapset, title);
200  fprintf(out, "%-18s %-.60s\n", list[i], title);
201  }
202  }
203  else
204  G_ls_format(list, count, 0, out);
205 
206  fprintf(out, "\n");
207 
208  for (i = 0; i < count; i++)
209  G_free((char *)list[i]);
210  if (list)
211  G_free(list);
212 
213  return count;
214 }
215 
226 char **G_list(int element, const char *gisbase, const char *location,
227  const char *mapset)
228 {
229  char *el;
230  char *buf;
231  DIR *dirp;
232  struct dirent *dp;
233  int count;
234  char **list;
235 
236  switch (element) {
237  case G_ELEMENT_RASTER:
238  el = "cell";
239  break;
240 
241  case G_ELEMENT_GROUP:
242  el = "group";
243  break;
244 
245  case G_ELEMENT_VECTOR:
246  el = "vector";
247  break;
248 
249  case G_ELEMENT_REGION:
250  el = "windows";
251  break;
252 
253  default:
254  G_fatal_error(_("G_list: Unknown element type"));
255  }
256 
257  buf = (char *)G_malloc(strlen(gisbase) + strlen(location)
258  + strlen(mapset) + strlen(el) + 4);
259 
260  sprintf(buf, "%s/%s/%s/%s", gisbase, location, mapset, el);
261 
262  dirp = opendir(buf);
263  G_free(buf);
264 
265  if (dirp == NULL) { /* this can happen if element does not exist */
266  list = (char **)G_calloc(1, sizeof(char *));
267  return list;
268  }
269 
270  count = 0;
271  while ((dp = readdir(dirp)) != NULL) {
272  if (dp->d_name[0] == '.')
273  continue;
274  count++;
275  }
276  rewinddir(dirp);
277 
278  list = (char **)G_calloc(count + 1, sizeof(char *));
279 
280  count = 0;
281  while ((dp = readdir(dirp)) != NULL) {
282  if (dp->d_name[0] == '.')
283  continue;
284 
285  list[count] = (char *)G_malloc(strlen(dp->d_name) + 1);
286  strcpy(list[count], dp->d_name);
287  count++;
288  }
289  closedir(dirp);
290 
291  return list;
292 }
293 
301 void G_free_list(char **list)
302 {
303  int i = 0;
304 
305  if (!list)
306  return;
307 
308  while (list[i]) {
309  G_free(list[i]);
310  i++;
311  }
312  G_free(list);
313 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
DIR * opendir()
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
void G_ls_format(char **list, int num_items, int perline, FILE *stream)
Prints a listing of items to a stream, in prettified column format.
Definition: ls.c:159
char ** G__ls(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
Definition: ls.c:88
string name
Definition: render.py:1314
int count
tuple gisbase
Definition: forms.py:59
dir_entry * readdir()
int G_set_list_hit_return(int flag)
Definition: gis/list.c:32
int G_list_element(const char *element, const char *desc, const char *mapset, int(*lister)(const char *, const char *, const char *))
General purpose list function.
Definition: gis/list.c:60
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
char * G__mapset_name(int n)
Get name of the n&#39;th mapset from the mapset_name[] list.
Definition: mapset_nme.c:36
flag
Definition: tools.py:1403
int
Definition: g3dcolor.c:48
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: spawn.c:69
int n
Definition: dataquad.c:291
char ** G_list(int element, const char *gisbase, const char *location, const char *mapset)
List specified type of elements. Application must release the allocated memory.
Definition: gis/list.c:226
void G_free_list(char **list)
Free list.
Definition: gis/list.c:301