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