GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
parser_md_common.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/parser_md_common.c
3
4 \brief GIS Library - Argument parsing functions (Markdown output)
5
6 (C) 2023-2025 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 Martin Landa
12 */
13#include <stdio.h>
14#include <string.h>
15
16#include <grass/gis.h>
17#include <grass/glocale.h>
18
19#include "parser_local_proto.h"
20
21/*!
22 * \brief Format text for Markdown output
23 */
24#define do_escape(c, escaped) \
25 case c: \
26 fputs(escaped, f); \
27 break
28
29void G__md_print_escaped(FILE *f, const char *str, const char *indent)
30{
31 const char *s;
32 for (s = str; *s; s++) {
33 switch (*s) {
34 case '\n':
35 fputs(MD_NEWLINE "\n", f);
36 fputs(indent, f);
37 break;
38 case '\t':
39 fputs("&nbsp;&nbsp;&nbsp;&nbsp;", f);
40 break;
41 case '<':
42 fputs("&lt;", f);
43 break;
44 case '>':
45 fputs("&gt;", f);
46 break;
47 case '*':
48 fputs("\\*", f);
49 break;
50 default:
51 fputc(*s, f);
52 }
53 }
54}
55
56void G__md_print_escaped_for_options(FILE *f, const char *str)
57{
58 const char *s;
59
60 for (s = str; *s; s++) {
61 switch (*s) {
62 do_escape('\n', "\n\n");
63 do_escape(',', ", ");
64 default:
65 fputc(*s, f);
66 }
67 }
68}
69
70#undef do_escape
71
72// This can eventually go to a file with functions related to Option,
73// but for now it is here until parser.c is refactored.
74/**
75 * \brief Get number of tuple items if option is a tuple
76 *
77 * Note that parser code generally does not consider tuples with only one
78 * item, so this function never returns 1.
79 *
80 * The number of items is determined by counting commas in the option key
81 * description.
82 *
83 * \param opt Option definition
84 * \return Number of items or zero if not a tuple
85 */
87{
88 // If empty, it cannot be considered a tuple.
89 if (!opt->key_desc)
90 return 0;
91
92 int n_items = 1;
93 const char *ptr;
94
95 for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
96 if (*ptr == ',')
97 n_items++;
98
99 // Only one item is not considered a tuple.
100 if (n_items == 1)
101 return 0;
102 // Only two and more items are a tuple.
103 return n_items;
104}
#define do_escape(c, escaped)
Format text for Markdown output.
void G__md_print_escaped(FILE *f, const char *str, const char *indent)
int G__option_num_tuple_items(const struct Option *opt)
Get number of tuple items if option is a tuple.
void G__md_print_escaped_for_options(FILE *f, const char *str)
Structure that stores option information.
Definition gis.h:563