GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
gis/list.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/list.c
3
4 \brief List elements
5
6 \author Unknown (probably CERL)
7
8 SPDX-FileCopyrightText: 2000, 2010 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12#include <stdlib.h>
13#include <unistd.h>
14#include <signal.h>
15#include <string.h>
16#include <sys/types.h>
17#include <dirent.h>
18
19#include <grass/gis.h>
20#include <grass/glocale.h>
21
22static int list_element(FILE *, const char *, const char *, const char *,
23 int (*)(const char *, const char *, char *));
24
25/*!
26 \brief General purpose list function
27
28 Will list files from all mapsets in the mapset list for a specified
29 database element.
30
31 Note: output is to stdout piped thru the more utility
32
33 \code
34 lister (char *name char *mapset, char* buf)
35 \endcode
36
37 Given file <em>name</em>, and <em>mapset</em>, lister() should
38 copy a string into 'buf' when called with name == "", should set
39 buf to general title for mapset list.
40
41 \param element database element (eg, "cell", "cellhd", etc.)
42 \param desc description for element (if NULL, element is used)
43 \param mapset mapset to be listed "" to list all mapsets in mapset search
44 list
45 "." will list current mapset
46 \param lister if given will call this routine to get a list title.
47 NULL if no titles desired.
48 */
49void G_list_element(const char *element, const char *desc, const char *mapset,
50 int (*lister)(const char *, const char *, char *))
51{
52 struct Popen pager;
53 int n;
54 FILE *more;
55 int count;
56
57 count = 0;
58 if (desc == 0 || *desc == 0)
59 desc = element;
60
61 /*
62 * G_popen() the more command to page the output
63 */
64 more = G_open_pager(&pager);
65 fprintf(more, "----------------------------------------------\n");
66
67 /*
68 * if no specific mapset is requested, list the mapsets
69 * from the mapset search list
70 * otherwise just list the specified mapset
71 */
72 if (mapset == 0 || *mapset == 0)
73 for (n = 0; (mapset = G_get_mapset_name(n)); n++)
74 count += list_element(more, element, desc, mapset, lister);
75 else
76 count += list_element(more, element, desc, mapset, lister);
77
78 if (count == 0) {
79 if (mapset == 0 || *mapset == 0)
80 fprintf(more, _("no %s files available in current mapset\n"), desc);
81 else
82 fprintf(more, _("no %s files available in mapset <%s>\n"), desc,
83 mapset);
84
85 fprintf(more, "----------------------------------------------\n");
86 }
87 /*
88 * close the more
89 */
91}
92
93static int list_element(FILE *out, const char *element, const char *desc,
94 const char *mapset,
95 int (*lister)(const char *, const char *, char *))
96{
97 char path[GPATH_MAX];
98 int count = 0;
99 char **list;
100 int i;
101
102 /*
103 * convert . to current mapset
104 */
105 if (strcmp(mapset, ".") == 0)
106 mapset = G_mapset();
107
108 /*
109 * get the full name of the GIS directory within the mapset
110 * and list its contents (if it exists)
111 *
112 * if lister() routine is given, the ls command must give 1 name
113 */
114 G_file_name(path, element, "", mapset);
115 if (access(path, 0) != 0) {
116 fprintf(out, "\n");
117 return count;
118 }
119
120 /*
121 * if a title so that we can call lister() with the names
122 * otherwise the ls must be forced into columnar form.
123 */
124
125 list = G_ls2(path, &count);
126
127 if (count > 0) {
128 fprintf(out, _("%s files available in mapset <%s>:\n"), desc, mapset);
129 if (lister) {
130 char title[400];
131 char name[GNAME_MAX];
132
133 *name = *title = 0;
134 lister(name, mapset, title);
135 if (*title)
136 fprintf(out, "\n%-18s %-.60s\n", name, title);
137 }
138 }
139
140 if (lister) {
141 for (i = 0; i < count; i++) {
142 char title[400];
143
144 lister(list[i], mapset, title);
145 fprintf(out, "%-18s %-.60s\n", list[i], title);
146 }
147 }
148 else
149 G_ls_format(list, count, 0, out);
150
151 fprintf(out, "\n");
152
153 for (i = 0; i < count; i++)
154 G_free((char *)list[i]);
155 if (list)
156 G_free(list);
157
158 return count;
159}
160
161/*!
162 \brief List specified type of elements. Application must release
163 the allocated memory.
164
165 \param element element type (G_ELEMENT_RASTER, G_ELEMENT_VECTOR,
166 G_ELEMENT_REGION ) \param gisbase path to GISBASE \param location location
167 name \param mapset mapset name
168
169 \return zero terminated array of element names
170 */
171char **G_list(int element, const char *gisbase, const char *location,
172 const char *mapset)
173{
174 char *el;
175 char *buf;
176 DIR *dirp;
177 struct dirent *dp;
178 int count;
179 char **list;
180
181 switch (element) {
182 case G_ELEMENT_RASTER:
183 el = "cell";
184 break;
185
186 case G_ELEMENT_GROUP:
187 el = "group";
188 break;
189
190 case G_ELEMENT_VECTOR:
191 el = "vector";
192 break;
193
194 case G_ELEMENT_REGION:
195 el = "windows";
196 break;
197
198 default:
199 G_fatal_error(_("G_list: Unknown element type"));
200 }
201
202 size_t bufsize =
203 strlen(gisbase) + strlen(location) + strlen(mapset) + strlen(el) + 4;
204 buf = (char *)G_malloc(bufsize);
205
206 snprintf(buf, bufsize, "%s/%s/%s/%s", gisbase, location, mapset, el);
207
208 dirp = opendir(buf);
209 G_free(buf);
210
211 if (dirp == NULL) { /* this can happen if element does not exist */
212 list = (char **)G_calloc(1, sizeof(char *));
213 return list;
214 }
215
216 count = 0;
217 while ((dp = readdir(dirp)) != NULL) {
218 if (dp->d_name[0] == '.')
219 continue;
220 count++;
221 }
223
224 list = (char **)G_calloc(count + 1, sizeof(char *));
225
226 count = 0;
227 while ((dp = readdir(dirp)) != NULL) {
228 if (dp->d_name[0] == '.')
229 continue;
230
231 list[count] = (char *)G_malloc(strlen(dp->d_name) + 1);
232 strcpy(list[count], dp->d_name);
233 count++;
234 }
235 closedir(dirp);
236
237 return list;
238}
239
240/*!
241 \brief Free list
242
243 \param list char* array to be freed
244
245 \return
246 */
247void G_free_list(char **list)
248{
249 int i = 0;
250
251 if (!list)
252 return;
253
254 while (list[i]) {
255 G_free(list[i]);
256 i++;
257 }
258 G_free(list);
259}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_calloc(m, n)
Definition defs/gis.h:137
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_close_pager(struct Popen *)
Definition pager.c:35
#define G_malloc(n)
Definition defs/gis.h:136
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:59
void G_ls_format(char **, int, int, FILE *)
Prints a listing of items to a stream, in prettified column format.
Definition ls.c:162
const char * G_get_mapset_name(int)
Get name of the n'th mapset from the current mapset search path.
Definition mapset_nme.c:42
char ** G_ls2(const char *, int *)
Stores a sorted directory listing in an array.
Definition ls.c:91
FILE * G_open_pager(struct Popen *)
Definition pager.c:13
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
struct DIR DIR
Definition dirent.h:18
void G_list_element(const char *element, const char *desc, const char *mapset, int(*lister)(const char *, const char *, char *))
General purpose list function.
Definition gis/list.c:49
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:171
void G_free_list(char **list)
Free list.
Definition gis/list.c:247
#define GPATH_MAX
Definition gis.h:196
@ G_ELEMENT_RASTER
Definition gis.h:429
@ G_ELEMENT_VECTOR
Definition gis.h:431
@ G_ELEMENT_GROUP
Definition gis.h:435
@ G_ELEMENT_REGION
Definition gis.h:434
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
int count
DIR * opendir(const char *name)
Definition msvc/dirent.c:15
void rewinddir(DIR *dir)
Definition msvc/dirent.c:92
struct dirent * readdir(DIR *dir)
Definition msvc/dirent.c:75
int closedir(DIR *dir)
Definition msvc/dirent.c:54
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:66
Definition gis.h:626
Definition manage.h:4
Definition path.h:15
#define access
Definition unistd.h:7