Loading...
Searching...
No Matches
gis/list.c
Go to the documentation of this file.
/*!
\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 *, 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 *, 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 *, 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"));
}
size_t bufsize =
strlen(gisbase) + strlen(location) + strlen(mapset) + strlen(el) + 4;
buf = (char *)G_malloc(bufsize);
snprintf(buf, bufsize, "%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);
}
G_free
void G_free(void *buf)
Free allocated memory.
Definition alloc.c:147
NULL
#define NULL
Definition ccmath.h:32
G_file_name
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:61
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition gis/error.c:159
G_list_element
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:51
G_list
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:173
G_free_list
void G_free_list(char **list)
Free list.
Definition gis/list.c:249
count
int count
G_ls_format
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:165
G_ls2
char ** G_ls2(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
Definition ls.c:94
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition mapset.c:33
G_get_mapset_name
const char * G_get_mapset_name(int n)
Get name of the n'th mapset from the current mapset search path.
Definition mapset_nme.c:44
name
const char * name
Definition named_colr.c:6
G_open_pager
FILE * G_open_pager(struct Popen *pager)
Definition pager.c:13
G_close_pager
void G_close_pager(struct Popen *pager)
Definition pager.c:35
strcpy
#define strcpy
Definition parson.c:66
list
struct list * list
Definition read_list.c:24
element
Definition lidar.h:85
path
Definition path.h:15