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