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