GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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
24struct list *list;
25
26static 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 */
48int 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'"),
74 element_list);
75
76 line = 0;
77 while (G_getl(buf, sizeof(buf), fd)) {
78 line++;
79 if (*buf == '#')
80 continue;
81 if (*buf == ' ' || *buf == '\t') { /* support element */
82 *desc = 0;
83 if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
84 continue;
85 if (*elem == '#')
86 continue;
87 if (nlist == 0)
88 format_error(element_list, line, buf);
89
90 G_strip(elem);
91 G_strip(desc);
92 M__add_element(elem, desc);
93 }
94 else { /* main element */
95
96 if (sscanf(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 = (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
106 list[nlist].mainelem = G_store(elem);
107 list[nlist].alias = G_store(alias);
108 list[nlist].maindesc = G_store(desc);
109 list[nlist].text = G_store(text);
110 list[nlist].nelem = 0;
111 list[nlist].element = 0;
112 list[nlist].desc = 0;
113 list[nlist].status = 0;
114 if (!check_if_empty || !M__empty(elem)) {
115 list[nlist].status = 1;
116 any = 1;
117 }
118 nlist++;
119 M__add_element(elem, desc);
120 }
121 }
122
123 if (num)
124 *num = nlist;
125
126 fclose(fd);
127
128 return any;
129}
130
131void format_error(char *element_list, int line, char *buf)
132{
133 G_fatal_error(_("Format error: file ('%s') line (%d) - %s"), element_list,
134 line, buf);
135}
void M__add_element(const char *elem, const char *desc)
Add element to the list.
Definition add_elem.c:24
int M__empty(char *elem)
Check if element is empty.
Definition empty.c:28
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition getl.c:31
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition gis/error.c:159
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition gisbase.c:39
#define strcpy
Definition parson.c:62
int nlist
Definition read_list.c:23
struct list * list
Definition read_list.c:24
int M_read_list(int check_if_empty, int *num)
Read list of elements.
Definition read_list.c:48
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition strings.c:300