GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r73122
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
read_list.c
Go to the documentation of this file.
1 /*!
2  \file lib/manage/read_list.c
3 
4  \brief Manage Library - Read list of elements
5 
6  (C) 2001-2011 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 Original author CERL
12 */
13 
14 #include <string.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
21 #include "manage_local_proto.h"
22 
23 int nlist;
24 struct list *list;
25 
26 static void format_error(char *, int, char *);
27 
28 /*!
29  \brief Read list of elements
30 
31  Format:
32 
33  \code
34  # ... comments
35  main element:alias:description:menu text
36  sub element:description
37  sub element:description
38  .
39  .
40  .
41  \endcode
42 
43  \param check_if_empty TRUE for check if element is empty
44 
45  \return 0
46  \return 1
47 */
48 int M_read_list(int check_if_empty, int *num)
49 {
50  FILE *fd;
51  char element_list[GPATH_MAX];
52  char buf[1024];
53  char elem[100];
54  char alias[100];
55  char desc[100];
56  char text[100];
57  int any;
58  int line;
59  char *env;
60 
61  nlist = 0;
62  list = 0;
63  any = 0;
64 
65  env = getenv("ELEMENT_LIST");
66  if (env)
67  strcpy(element_list, env);
68  else
69  sprintf(element_list, "%s/etc/element_list", G_gisbase());
70  fd = fopen(element_list, "r");
71 
72  if (!fd)
73  G_fatal_error(_("Unable to open data base element list '%s'"), element_list);
74 
75  line = 0;
76  while (G_getl(buf, sizeof(buf), fd)) {
77  line++;
78  if (*buf == '#')
79  continue;
80  if (*buf == ' ' || *buf == '\t') { /* support element */
81  *desc = 0;
82  if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
83  continue;
84  if (*elem == '#')
85  continue;
86  if (nlist == 0)
87  format_error(element_list, line, buf);
88 
89  G_strip(elem);
90  G_strip(desc);
91  M__add_element(elem, desc);
92  }
93  else { /* main element */
94 
95  if (sscanf
96  (buf, "%[^:]:%[^:]:%[^:]:%[^\n]", elem, alias, desc,
97  text) != 4)
98  format_error(element_list, line, buf);
99 
100  G_strip(elem);
101  G_strip(alias);
102  G_strip(desc);
103  G_strip(text);
104 
105  list =
106  (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
107  list[nlist].mainelem = G_store(elem);
108  list[nlist].alias = G_store(alias);
109  list[nlist].maindesc = G_store(desc);
110  list[nlist].text = G_store(text);
111  list[nlist].nelem = 0;
112  list[nlist].element = 0;
113  list[nlist].desc = 0;
114  list[nlist].status = 0;
115  if (!check_if_empty || !M__empty(elem)) {
116  list[nlist].status = 1;
117  any = 1;
118  }
119  nlist++;
120  M__add_element(elem, desc);
121  }
122  }
123 
124  if (num)
125  *num = nlist;
126 
127  fclose(fd);
128 
129  return any;
130 }
131 
132 void format_error(char *element_list, int line, char *buf)
133 {
134  G_fatal_error(_("Format error: file ('%s') line (%d) - %s"), element_list, line,
135  buf);
136 }
void M__add_element(const char *elem, const char *desc)
Add element to the list.
Definition: add_elem.c:24
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:258
char * mainelem
Definition: manage.h:12
int nlist
Definition: read_list.c:23
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
char * alias
Definition: manage.h:7
fd
Definition: d/range.c:69
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
char * text
Definition: manage.h:9
struct list * list
Definition: read_list.c:24
fclose(fd)
#define GPATH_MAX
Definition: gis.h:151
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition: getl.c:31
int M_read_list(int check_if_empty, int *num)
Read list of elements.
Definition: read_list.c:48
Definition: manage.h:4
char status
Definition: manage.h:11
#define _(str)
Definition: glocale.h:13
const char ** desc
Definition: manage.h:8
long num
Definition: raster3d/cats.c:85
const char ** element
Definition: manage.h:6
char * maindesc
Definition: manage.h:13
int nelem
Definition: manage.h:10
char * getenv()
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41
int M__empty(char *elem)
Check if element is empty.
Definition: empty.c:28