1 /***************************************************************************
2 *
3 * Copyright (C) 2001 – 2018 by Diether Knof
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You can find this license in the file 'gpl.txt'.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307 USA
20 *
21 * Contact:
22 * Diether Knof dknof@posteo.de
23 *
24 **************************************************************************/
25
26 #include "parser.h"
27 #include "string.h"
28
29 namespace Parser {
30
31 /** Gibt zurück, ob text leer ist
32 ** Abschlussroutine für die parse-Templates
33 **/
34 bool
parse(string & text)35 parse(string& text)
36 {
37 return text.empty();
38 } // bool parse(string& text)
39
40 /** Liest n aus text, entfernt den gelesenen Text und die folgenden Leerzeichen
41 ** Ändert text nur, wenn er mit einem unsigned beginnt.
42 **
43 ** @return true, wenn text mit einem unsigned beginnt, sonst false
44 **/
45 bool
parse_(string & text,unsigned & n)46 parse_(string& text, unsigned& n)
47 {
48 try {
49 n = std::stou(text);
50 while (!text.empty() && ::isdigit(text.front()))
51 text.erase(0, 1);
52 String::remove_leading_blanks(text);
53 return true;
54 } catch (...) {
55 return false;
56 }
57 } // bool parse_(string& text, unsigned& n)
58
59 /** Speichert den Text in remaining_text
60 **
61 ** @return true, wenn text nicht leer ist
62 **/
63 bool
parse_(string & text,string & remaining_text)64 parse_(string& text, string& remaining_text)
65 {
66 if (text.empty())
67 return false;
68 remaining_text = text;
69 text.clear();
70 return true;
71 } // bool parse_(string& text, string& remaining_text)
72
73 /** Liest fixtext aus text, entfernt ihn und die folgenden Leerzeichen
74 ** Ändert text nur, wenn er mit fixtext beginnt.
75 **
76 ** @return true, wenn text mit fixtext beginnt, sonst false
77 **/
78 bool
parse_(string & text,char const * fixtext)79 parse_(string& text, char const* fixtext)
80 {
81 auto const n = strlen(fixtext);
82 if (text.compare(0, n, fixtext))
83 return false;
84 text.erase(0, n);
85 String::remove_leading_blanks(text);
86 return true;
87 } // bool parse_(string& text, char const* fixtext)
88
89 }; // namespace Parser
90