Doxygen
Loading...
Searching...
No Matches
cppvalue.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2021 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#include <stdlib.h>
17
18#include "cppvalue.h"
19#include "constexp.h"
20
21CPPValue CPPValue::parseOctal(const std::string& token)
22{
23 long val = 0;
24 for (const char *p = token.c_str(); *p != 0; p++)
25 {
26 if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
27 }
28 return CPPValue(val);
29}
30
31CPPValue CPPValue::parseDecimal(const std::string& token)
32{
33 long val = 0;
34 for (const char *p = token.c_str(); *p != 0; p++)
35 {
36 if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
37 }
38 return CPPValue(val);
39}
40
41CPPValue CPPValue::parseHexadecimal(const std::string& token)
42{
43 long val = 0;
44 for (const char *p = token.c_str(); *p != 0; p++)
45 {
46 if (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
47 else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
48 else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10;
49 }
50 //printf("parseHexadecimal %s->%x\n",qPrint(token),val);
51 return CPPValue(val);
52}
53
54CPPValue CPPValue::parseBinary(const std::string& token)
55{
56 long val = 0;
57 for (const char *p = token.c_str(); *p != 0; p++)
58 {
59 if (*p >= '0' && *p <= '1') val = val * 2 + *p - '0';
60 }
61 return CPPValue(val);
62}
63
64CPPValue CPPValue::parseCharacter(const std::string& token) // does not work for '\n' and the alike
65{
66 if (token[1]=='\\')
67 {
68 switch(token[2])
69 {
70 case 'n': return CPPValue('\n');
71 case 't': return CPPValue('\t');
72 case 'v': return CPPValue('\v');
73 case 'b': return CPPValue('\b');
74 case 'r': return CPPValue('\r');
75 case 'f': return CPPValue('\f');
76 case 'a': return CPPValue('\a');
77 case '\\': return CPPValue('\\');
78 case '?': return CPPValue('\?');
79 case '\'': return CPPValue('\'');
80 case '"': return CPPValue('"');
81 case '0': // fall through
82 case '1': // fall through
83 case '2': // fall through
84 case '3': // fall through
85 case '4': // fall through
86 case '5': // fall through
87 case '6': // fall through
88 case '7': // fall through
89 return parseOctal(token);
90 case 'x':
91 case 'X': return parseHexadecimal(token);
92 default: printf("Invalid escape sequence %s found!\n",token.c_str());
93 return CPPValue(0L);
94 }
95 }
96 return CPPValue(token[1]);
97}
98
99CPPValue CPPValue::parseFloat(const std::string& token)
100{
101 return CPPValue(std::stod(token));
102}
static CPPValue parseOctal(const std::string &token)
Definition cppvalue.cpp:21
static CPPValue parseDecimal(const std::string &token)
Definition cppvalue.cpp:31
CPPValue(char c)
Definition cppvalue.h:28
static CPPValue parseHexadecimal(const std::string &token)
Definition cppvalue.cpp:41
static CPPValue parseCharacter(const std::string &token)
Definition cppvalue.cpp:64
static CPPValue parseFloat(const std::string &token)
Definition cppvalue.cpp:99
static CPPValue parseBinary(const std::string &token)
Definition cppvalue.cpp:54