GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
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"), list[n].text, desc);
48  p->description = str;
49  if (strcmp(p->key, "raster") == 0 || strcmp(p->key, "raster_3d") == 0)
50  p->guisection = _("Raster");
51  else if (strcmp(p->key, "vector") == 0)
52  p->guisection = _("Vector");
53  else if (strcmp(p->key, "region") == 0)
54  p->guisection = _("Region");
55  else if (strcmp(p->key, "group") == 0)
56  p->guisection = _("Group");
57 
58  return p;
59 }
60 
61 /*!
62  \brief Get list of element types separated by comma
63 
64  String buffer is allocated by G_malloc().
65 
66  \param do_all TRUE to add "all" to the buffer
67 
68  \return pointer to allocated buffer with types
69  */
70 const char *M_get_options(int do_all)
71 {
72  int len, n;
73  char *str;
74 
75  for (len = 0, n = 0; n < nlist; n++)
76  len += strlen(list[n].alias) + 1;
77  if (do_all)
78  len += 4;
79  str = G_malloc(len);
80 
81  for (n = 0; n < nlist; n++) {
82  if (n) {
83  strcat(str, ",");
84  strcat(str, list[n].alias);
85  }
86  else
87  strcpy(str, list[n].alias);
88  }
89 
90  if (do_all)
91  strcat(str, ",all");
92 
93  return str;
94 }
95 
96 /*!
97  \brief Get list of element desc separated by comma
98 
99  String buffer is allocated by G_malloc().
100 
101  \param do_all TRUE to add "all" to the buffer
102 
103  \return pointer to allocated buffer with desc
104  */
105 const char *M_get_option_desc(int do_all)
106 {
107  int len, n;
108  char *str;
109  const char *str_all = "all;all types";
110 
111  for (len = 0, n = 0; n < nlist; n++) {
112  len += strlen(list[n].alias) + 1;
113  len += strlen(list[n].text) + 1;
114  }
115  if (do_all)
116  len += strlen(str_all) + 1;
117  str = G_malloc(len);
118 
119  for (n = 0; n < nlist; n++) {
120  if (n) {
121  strcat(str, ";");
122  strcat(str, list[n].alias);
123  strcat(str, ";");
124  strcat(str, list[n].text);
125  }
126  else {
127  strcpy(str, list[n].alias);
128  strcat(str, ";");
129  strcat(str, list[n].text);
130  }
131  }
132 
133  if (do_all) {
134  strcat(str, ";");
135  strcat(str, str_all);
136  }
137 
138  return str;
139 }
#define NULL
Definition: ccmath.h:32
struct Option * G_define_option(void)
Initializes an Option struct.
Definition: parser.c:211
#define G_malloc(n)
Definition: defs/gis.h:94
int G_asprintf(char **, const char *,...) __attribute__((format(printf
#define TYPE_STRING
Definition: gis.h:186
#define NO
Definition: gis.h:188
#define _(str)
Definition: glocale.h:10
const char * M_get_options(int do_all)
Get list of element types separated by comma.
Definition: option.c:70
const char * M_get_option_desc(int do_all)
Get list of element desc separated by comma.
Definition: option.c:105
struct Option * M_define_option(int n, const char *desc, int multiple)
Define option for parser.
Definition: option.c:28
#define strcpy
Definition: parson.c:62
int nlist
Definition: read_list.c:23
Structure that stores option information.
Definition: gis.h:557
const char * key
Definition: gis.h:558
const char * key_desc
Definition: gis.h:564
const char * gisprompt
Definition: gis.h:575
int type
Definition: gis.h:559
const char * description
Definition: gis.h:566
int required
Definition: gis.h:560
int multiple
Definition: gis.h:561
const char * guisection
Definition: gis.h:576
Definition: manage.h:4
char * alias
Definition: manage.h:6