GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
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
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
snprintf(element_list, GPATH_MAX,
"%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
131
void
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
}
M__add_element
void M__add_element(const char *elem, const char *desc)
Add element to the list.
Definition
add_elem.c:24
M__empty
int M__empty(char *elem)
Check if element is empty.
Definition
empty.c:28
G_getl
int G_getl(char *buf, int n, FILE *fd)
Gets a line of text from a file.
Definition
getl.c:33
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
G_gisbase
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition
gisbase.c:39
strcpy
#define strcpy
Definition
parson.c:66
nlist
int nlist
Definition
read_list.c:23
list
struct list * list
Definition
read_list.c:24
M_read_list
int M_read_list(int check_if_empty, int *num)
Read list of elements.
Definition
read_list.c:48
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
G_strip
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition
strings.c:300
manage
read_list.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0