GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 char **list_subgroups(const char *group, const char *mapset, int *subgs_num)
19 {
20  /* Unlike I_list_subgroup and I_list_subgroup_simple this function
21  returns array of subgroup names, it does not use fprintf.
22  This approach should make the function usable in more cases. */
23 
24  char **subgs;
25  char path[GPATH_MAX];
26  char buf[GPATH_MAX];
27  struct stat sb;
28 
29  *subgs_num = 0;
30 
31  if (I_find_group2(group, mapset) == 0)
32  return NULL;
33 
34  sprintf(buf, "group/%s/subgroup", group);
35  G_file_name(path, buf, "", mapset);
36 
37  if (G_lstat(path, &sb) || !S_ISDIR(sb.st_mode))
38  return NULL;
39 
40  subgs = G_ls2(path, subgs_num);
41  return subgs;
42 }
43 
44 /*!
45  * \brief Get list of subgroups which a group contatins.
46  *
47  * \param group group name
48  * \param[out] subgs_num number of subgroups which the group contains
49  * \return array of subgroup names
50  */
51 
52 char **I_list_subgroups(const char *group, int *subgs_num)
53 {
54 
55  return list_subgroups(group, G_mapset(), subgs_num);
56 }
57 
58 /*!
59  * \brief Get list of subgroups which a group contatins.
60  *
61  * \param group group name
62  * \param mapset mapset name
63  * \param[out] subgs_num number of subgroups which the group contains
64  * \return array of subgroup names
65  */
66 
67 char **I_list_subgroups2(const char *group, const char *mapset,
68  int *subgs_num)
69 {
70  return list_subgroups(group, mapset, subgs_num);
71 }
72 
73 /*!
74  * \brief Prints maps in a subgroup (fancy version)
75  *
76  * \param group group name
77  * \param subgroup subgroup name
78  * \param ref group reference (set with I_get_subgroup_ref())
79  * \param fd where to print (typically stdout)
80  * \return 0
81  */
82 int I_list_subgroup(const char *group, const char *subgroup,
83  const struct Ref *ref, FILE * fd)
84 {
85  char buf[80];
86  int i;
87  int len, tot_len;
88  int max;
89 
90  if (ref->nfiles <= 0) {
91  fprintf(fd, _("subgroup <%s> of group <%s> is empty\n"),
92  subgroup, group);
93  return 0;
94  }
95  max = 0;
96  for (i = 0; i < ref->nfiles; i++) {
97  I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
98  len = strlen(buf) + 4;
99  if (len > max)
100  max = len;
101  }
102  fprintf(fd,
103  _("subgroup <%s> of group <%s> references the following raster maps\n"),
104  subgroup, group);
105  fprintf(fd, "-------------\n");
106  tot_len = 0;
107  for (i = 0; i < ref->nfiles; i++) {
108  I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
109  tot_len += max;
110  if (tot_len > 78) {
111  fprintf(fd, "\n");
112  tot_len = max;
113  }
114  fprintf(fd, "%-*s", max, buf);
115  }
116  if (tot_len)
117  fprintf(fd, "\n");
118  fprintf(fd, "-------------\n");
119 
120  return 0;
121 }
122 
123 /*!
124  * \brief Prints maps in a subgroup (simple version)
125  *
126  * Same as I_list_subgroup(), but without all the fancy stuff.
127  * Prints one map per line in map@mapset form.
128  *
129  * \param ref group reference (set with I_get_subgroup_ref())
130  * \param fd where to print (typically stdout)
131  * \return 0
132  */
133 /* same as above, but one map per line in map@mapset form */
134 int I_list_subgroup_simple(const struct Ref *ref, FILE * fd)
135 {
136  return I_list_group_simple(ref, fd);
137 }
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
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:52
Definition: imagery.h:26
char ** G_ls2(const char *, int *)
Stores a sorted directory listing in an array.
Definition: ls.c:95
char ** list_subgroups(const char *group, const char *mapset, int *subgs_num)
Definition: list_subgp.c:18
#define NULL
Definition: ccmath.h:32
#define max(x, y)
Definition: draw2.c:32
char mapset[INAME_LEN]
Definition: imagery.h:23
char ** I_list_subgroups2(const char *group, const char *mapset, int *subgs_num)
Get list of subgroups which a group contatins.
Definition: list_subgp.c:67
int I_find_group2(const char *, const char *)
Does the group exists?
Definition: imagery/find.c:41
#define GPATH_MAX
Definition: gis.h:180
int nfiles
Definition: imagery.h:28
struct Ref_Files * file
Definition: imagery.h:29
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
Definition: path.h:16
#define _(str)
Definition: glocale.h:10
int I_list_group_simple(const struct Ref *, FILE *)
Prints maps in a group (simple version)
Definition: list_gp.c:74
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:82
int I_list_subgroup_simple(const struct Ref *ref, FILE *fd)
Prints maps in a subgroup (simple version)
Definition: list_subgp.c:134
void I__list_group_name_fit(char *, const char *, const char *)
Formats map name to fit in a 80 column layout.
Definition: list_gp.c:98
int G_lstat(const char *, struct stat *)
Get file status.
Definition: paths.c:145