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// own header
17#include "cppvalue.h"
18
19// standard includes
20#include <string>
21
22// other includes
23#include "constexp.h"
24#include "dstring.h"
25#include "message.h"
26
27CPPValue CPPValue::parseOctal(const std::string &token)
28{
29 long val = 0;
30 for (const char c : token)
31 {
32 if (c >= '0' && c <= '7') val = val * 8 + c - '0';
33 }
34 return CPPValue(val);
35}
36
37CPPValue CPPValue::parseDecimal(const std::string &token)
38{
39 long val = 0;
40 for (const char c : token)
41 {
42 if (c >= '0' && c <= '9') val = val * 10 + c - '0';
43 }
44 return CPPValue(val);
45}
46
47CPPValue CPPValue::parseHexadecimal(const std::string &token)
48{
49 long val = 0;
50 for (const char c : token)
51 {
52 if (c >= '0' && c <= '9') val = val * 16 + c - '0';
53 else if (c >= 'a' && c <= 'f') val = val * 16 + c - 'a' + 10;
54 else if (c >= 'A' && c <= 'F') val = val * 16 + c - 'A' + 10;
55 }
56 //printf("parseHexadecimal %s->%x\n",qPrint(token),val);
57 return CPPValue(val);
58}
59
60CPPValue CPPValue::parseBinary(const std::string &token)
61{
62 long val = 0;
63 for (const char c : token)
64 {
65 if (c >= '0' && c <= '1') val = val * 2 + c - '0';
66 }
67 return CPPValue(val);
68}
69
70CPPValue CPPValue::parseCharacter(const std::string &token) // does not work for '\n' and the alike
71{
72 ASSERT(!token.empty());
73 if (token[1]=='\\')
74 {
75 ASSERT(token.length()>1);
76 switch(token[2])
77 {
78 case 'n': return CPPValue('\n');
79 case 't': return CPPValue('\t');
80 case 'v': return CPPValue('\v');
81 case 'b': return CPPValue('\b');
82 case 'r': return CPPValue('\r');
83 case 'f': return CPPValue('\f');
84 case 'a': return CPPValue('\a');
85 case '\\': return CPPValue('\\');
86 case '?': return CPPValue('\?');
87 case '\'': return CPPValue('\'');
88 case '"': return CPPValue('"');
89 case '0': // fall through
90 case '1': // fall through
91 case '2': // fall through
92 case '3': // fall through
93 case '4': // fall through
94 case '5': // fall through
95 case '6': // fall through
96 case '7': // fall through
97 return parseOctal(token);
98 case 'x':
99 case 'X': return parseHexadecimal(token);
100 default: printf("Invalid escape sequence %s found!\n",std::string(token).c_str());
101 return CPPValue(0L);
102 }
103 }
104 return CPPValue(token[1]);
105}
106
107CPPValue CPPValue::parseFloat(const std::string &token)
108{
109 return CPPValue(std::stod(token));
110}
constexpr CPPValue(char c) noexcept
Definition cppvalue.h:28
static CPPValue parseOctal(const std::string &token)
Definition cppvalue.cpp:27
static CPPValue parseDecimal(const std::string &token)
Definition cppvalue.cpp:37
static CPPValue parseHexadecimal(const std::string &token)
Definition cppvalue.cpp:47
static CPPValue parseCharacter(const std::string &token)
Definition cppvalue.cpp:70
static CPPValue parseFloat(const std::string &token)
Definition cppvalue.cpp:107
static CPPValue parseBinary(const std::string &token)
Definition cppvalue.cpp:60
#define ASSERT(x)
Definition message.h:142