GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
option.c
Go to the documentation of this file.
1 /*!
2  \file lib/manage/option.c
3 
4  \brief Manage Library - Define option for parser
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 <grass/gis.h>
16 #include <grass/glocale.h>
17 
18 #include "manage_local_proto.h"
19 
20 /*!
21  \brief Define option for parser
22 
23  \param n element id
24 
25  \return pointer to Option structure
26  \return NULL on error
27 */
28 struct Option* M_define_option(int n, const char *desc, int multiple)
29 {
30  char *str;
31  struct Option *p;
32 
33  if (n >= nlist)
34  return NULL;
35 
36  p = G_define_option();
37  p->key = list[n].alias;
38  p->type = TYPE_STRING;
39  if (multiple)
40  p->key_desc = "name";
41  else
42  p->key_desc = "from,to";
43  p->required = NO;
44  p->multiple = multiple;
45  G_asprintf(&str, "old,%s,%s", list[n].mainelem, list[n].maindesc);
46  p->gisprompt = str;
47  G_asprintf(&str, _("%s to be %s"),
48  list[n].text, desc);
49  p->description = str;
50  if (strcmp(p->key, "raster") == 0 || strcmp(p->key, "raster_3d") == 0)
51  p->guisection = _("Raster");
52  else if (strcmp(p->key, "vector") == 0)
53  p->guisection = _("Vector");
54  else if (strcmp(p->key, "region") == 0)
55  p->guisection = _("Region");
56  else if (strcmp(p->key, "group") == 0)
57  p->guisection = _("Group");
58 
59  return p;
60 }
61 
62 /*!
63  \brief Get list of element types separated by comma
64 
65  String buffer is allocated by G_malloc().
66 
67  \param do_all TRUE to add "all" to the buffer
68 
69  \return pointer to allocated buffer with types
70 */
71 const char *M_get_options(int do_all)
72 {
73  int len, n;
74  char *str;
75 
76  for (len = 0, n = 0; n < nlist; n++)
77  len += strlen(list[n].alias) + 1;
78  if (do_all)
79  len += 4;
80  str = G_malloc(len);
81 
82  for (n = 0; n < nlist; n++) {
83  if (n) {
84  strcat(str, ",");
85  strcat(str, list[n].alias);
86  }
87  else
88  strcpy(str, list[n].alias);
89  }
90 
91  if (do_all)
92  strcat(str, ",all");
93 
94  return str;
95 }
96 
97 /*!
98  \brief Get list of element desc separated by comma
99 
100  String buffer is allocated by G_malloc().
101 
102  \param do_all TRUE to add "all" to the buffer
103 
104  \return pointer to allocated buffer with desc
105 */
106 const char *M_get_option_desc(int do_all)
107 {
108  int len, n;
109  char *str;
110  const char *str_all = "all;all types";
111 
112  for (len = 0, n = 0; n < nlist; n++) {
113  len += strlen(list[n].alias) + 1;
114  len += strlen(list[n].text) + 1;
115  }
116  if (do_all)
117  len += strlen(str_all) + 1;
118  str = G_malloc(len);
119 
120  for (n = 0; n < nlist; n++) {
121  if (n) {
122  strcat(str, ";");
123  strcat(str, list[n].alias);
124  strcat(str, ";");
125  strcat(str, list[n].text);
126  }
127  else {
128  strcpy(str, list[n].alias);
129  strcat(str, ";");
130  strcat(str, list[n].text);
131  }
132  }
133 
134  if (do_all) {
135  strcat(str, ";");
136  strcat(str, str_all);
137  }
138 
139  return str;
140 }
#define G_malloc(n)
Definition: defs/gis.h:112
#define NO
Definition: gis.h:174
const char * M_get_options(int do_all)
Get list of element types separated by comma.
Definition: option.c:71
int nlist
Definition: read_list.c:23
#define TYPE_STRING
Definition: gis.h:172
char * alias
Definition: manage.h:7
#define NULL
Definition: ccmath.h:32
const char * description
Definition: gis.h:552
int type
Definition: gis.h:545
const char * M_get_option_desc(int do_all)
Get list of element desc separated by comma.
Definition: option.c:106
struct Option * G_define_option(void)
Initializes an Option struct.
Definition: parser.c:210
const char * guisection
Definition: gis.h:560
int multiple
Definition: gis.h:547
int required
Definition: gis.h:546
Definition: manage.h:4
Structure that stores option information.
Definition: gis.h:542
#define _(str)
Definition: glocale.h:10
const char * key
Definition: gis.h:544
const char * gisprompt
Definition: gis.h:559
int G_asprintf(char **, const char *,...) __attribute__((format(printf
struct Option * M_define_option(int n, const char *desc, int multiple)
Define option for parser.
Definition: option.c:28
const char * key_desc
Definition: gis.h:550