Doxygen
cppvalue.cpp
浏览该文件的文档.
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 
21 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 
31 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 
41 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 
54 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 
64 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((long)'\n');
71  case 't': return CPPValue((long)'\t');
72  case 'v': return CPPValue((long)'\v');
73  case 'b': return CPPValue((long)'\b');
74  case 'r': return CPPValue((long)'\r');
75  case 'f': return CPPValue((long)'\f');
76  case 'a': return CPPValue((long)'\a');
77  case '\\': return CPPValue((long)'\\');
78  case '?': return CPPValue((long)'\?');
79  case '\'': return CPPValue((long)'\'');
80  case '"': return CPPValue((long)'"');
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((long)token[1]);
97 }
98 
99 CPPValue parseFloat(const std::string& token)
100 {
101  return CPPValue(std::stod(token));
102 }
cppvalue.h
parseFloat
CPPValue parseFloat(const std::string &token)
Definition: cppvalue.cpp:99
CPPValue
A class representing a C-preprocessor value.
Definition: cppvalue.h:23
parseHexadecimal
CPPValue parseHexadecimal(const std::string &token)
Definition: cppvalue.cpp:41
parseBinary
CPPValue parseBinary(const std::string &token)
Definition: cppvalue.cpp:54
parseOctal
CPPValue parseOctal(const std::string &token)
Definition: cppvalue.cpp:21
parseDecimal
CPPValue parseDecimal(const std::string &token)
Definition: cppvalue.cpp:31
parseCharacter
CPPValue parseCharacter(const std::string &token)
Definition: cppvalue.cpp:64
constexp.h