GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r73120
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list_subgp.c
Go to the documentation of this file.
1 /*!
2  \file list_subgp.c
3 
4  \brief Imagery Library - List subgroup
5 
6  (C) 2001-2008,2013 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author USA CERL
12  */
13 
14 #include <string.h>
15 #include <grass/imagery.h>
16 #include <grass/glocale.h>
17 
18 /*!
19  * \brief Get list of subgroups which a group contatins.
20  *
21  * \param group group name
22  * \param[out] subgs_num number of subgroups which the group contains
23  * \return array of subgroup names
24  */
25 
26 char **I_list_subgroups(const char *group, int *subgs_num)
27 {
28  /* Unlike I_list_subgroup and I_list_subgroup_simple this function
29  returns array of subgroup names, it does not use fprintf.
30  This approach should make the function usable in more cases. */
31 
32  char **subgs;
33  char path[GPATH_MAX];
34  char buf[GPATH_MAX];
35  const char *mapset;
36  struct stat sb;
37 
38  *subgs_num = 0;
39 
40  if (I_find_group(group) == 0)
41  return NULL;
42 
43  mapset = G_mapset();
44  sprintf(buf, "group/%s/subgroup", group);
45  G_file_name(path, buf, "", mapset);
46 
47  if (!G_lstat(path, &sb) == 0 || !S_ISDIR(sb.st_mode))
48  return NULL;
49 
50  subgs = G_ls2(path, subgs_num);
51  return subgs;
52 }
53 
54 /*!
55  * \brief Prints maps in a subgroup (fancy version)
56  *
57  * \param group group name
58  * \param subgroup subgroup name
59  * \param ref group reference (set with I_get_subgroup_ref())
60  * \param fd where to print (typically stdout)
61  * \return 0
62  */
63 int I_list_subgroup(const char *group,
64  const char *subgroup, const struct Ref *ref, FILE * fd)
65 {
66  char buf[80];
67  int i;
68  int len, tot_len;
69  int max;
70 
71  if (ref->nfiles <= 0) {
72  fprintf(fd, _("subgroup <%s> of group <%s> is empty\n"),
73  subgroup, group);
74  return 0;
75  }
76  max = 0;
77  for (i = 0; i < ref->nfiles; i++) {
78  sprintf(buf, "<%s@%s>", ref->file[i].name, ref->file[i].mapset);
79  len = strlen(buf) + 4;
80  if (len > max)
81  max = len;
82  }
83  fprintf(fd,
84  _
85  ("subgroup <%s> of group <%s> references the following raster maps\n"),
86  subgroup, group);
87  fprintf(fd, "-------------\n");
88  tot_len = 0;
89  for (i = 0; i < ref->nfiles; i++) {
90  sprintf(buf, "<%s@%s>", ref->file[i].name, ref->file[i].mapset);
91  tot_len += max;
92  if (tot_len > 78) {
93  fprintf(fd, "\n");
94  tot_len = max;
95  }
96  fprintf(fd, "%-*s", max, buf);
97  }
98  if (tot_len)
99  fprintf(fd, "\n");
100  fprintf(fd, "-------------\n");
101 
102  return 0;
103 }
104 
105 /*!
106  * \brief Prints maps in a subgroup (simple version)
107  *
108  * Same as I_list_subgroup(), but without all the fancy stuff.
109  * Prints one map per line in map@mapset form.
110  *
111  * \param ref group reference (set with I_get_subgroup_ref())
112  * \param fd where to print (typically stdout)
113  * \return 0
114  */
115 /* same as above, but one map per line in map@mapset form */
116 int I_list_subgroup_simple(const struct Ref *ref, FILE * fd)
117 {
118  return I_list_group_simple(ref, fd);
119 }
char name[INAME_LEN]
Definition: imagery.h:22
char ** I_list_subgroups(const char *group, int *subgs_num)
Get list of subgroups which a group contatins.
Definition: list_subgp.c:26
Definition: imagery.h:26
#define NULL
Definition: ccmath.h:32
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:145
#define max(x, y)
Definition: draw2.c:32
fd
Definition: d/range.c:69
int I_list_group_simple(const struct Ref *ref, FILE *fd)
Prints maps in a group (simple version)
Definition: list_gp.c:73
int I_find_group(const char *group)
does group exist?
Definition: imagery/find.c:21
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:38
char ** G_ls2(const char *dir, int *num_files)
Stores a sorted directory listing in an array.
Definition: ls.c:95
char mapset[INAME_LEN]
Definition: imagery.h:23
#define GPATH_MAX
Definition: gis.h:151
int nfiles
Definition: imagery.h:28
struct Ref_Files * file
Definition: imagery.h:29
Definition: path.h:16
#define _(str)
Definition: glocale.h:13
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int I_list_subgroup(const char *group, const char *subgroup, const struct Ref *ref, FILE *fd)
Prints maps in a subgroup (fancy version)
Definition: list_subgp.c:63
int I_list_subgroup_simple(const struct Ref *ref, FILE *fd)
Prints maps in a subgroup (simple version)
Definition: list_subgp.c:116