GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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, int *subgs_num)
68 {
69  return list_subgroups(group, mapset, subgs_num);
70 }
71 
72 /*!
73  * \brief Prints maps in a subgroup (fancy version)
74  *
75  * \param group group name
76  * \param subgroup subgroup name
77  * \param ref group reference (set with I_get_subgroup_ref())
78  * \param fd where to print (typically stdout)
79  * \return 0
80  */
81 int I_list_subgroup(const char *group, const char *subgroup,
82  const struct Ref *ref, FILE *fd)
83 {
84  char buf[80];
85  int i;
86  int len, tot_len;
87  int max;
88 
89  if (ref->nfiles <= 0) {
90  fprintf(fd, _("subgroup <%s> of group <%s> is empty\n"), subgroup,
91  group);
92  return 0;
93  }
94  max = 0;
95  for (i = 0; i < ref->nfiles; i++) {
96  I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
97  len = strlen(buf) + 4;
98  if (len > max)
99  max = len;
100  }
101  fprintf(
102  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 }
#define NULL
Definition: ccmath.h:32
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
int G_lstat(const char *, struct stat *)
Get file status.
Definition: paths.c:145
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
char ** G_ls2(const char *, int *)
Stores a sorted directory listing in an array.
Definition: ls.c:94
int I_list_group_simple(const struct Ref *, FILE *)
Prints maps in a group (simple version)
Definition: list_gp.c:73
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:97
int I_find_group2(const char *, const char *)
Does the group exists?
Definition: imagery/find.c:40
#define max(x, y)
Definition: draw2.c:30
#define GPATH_MAX
Definition: gis.h:194
#define _(str)
Definition: glocale.h:10
int I_list_subgroup_simple(const struct Ref *ref, FILE *fd)
Prints maps in a subgroup (simple version)
Definition: list_subgp.c:134
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_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:81
char ** list_subgroups(const char *group, const char *mapset, int *subgs_num)
Definition: list_subgp.c:18
char ** I_list_subgroups(const char *group, int *subgs_num)
Get list of subgroups which a group contatins.
Definition: list_subgp.c:52
char name[INAME_LEN]
Definition: imagery.h:20
char mapset[INAME_LEN]
Definition: imagery.h:21
Definition: imagery.h:24
int nfiles
Definition: imagery.h:25
struct Ref_Files * file
Definition: imagery.h:26
Definition: path.h:15