00001 /****************************************************************************** 00002 * 00003 * $Id: cppvalue.cpp,v 1.6 2001/03/19 19:27:40 root Exp $ 00004 * 00005 * 00006 * Copyright (C) 1997-2008 by Dimitri van Heesch. 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation under the terms of the GNU General Public License is hereby 00010 * granted. No representations are made about the suitability of this software 00011 * for any purpose. It is provided "as is" without express or implied warranty. 00012 * See the GNU General Public License for more details. 00013 * 00014 * Documents produced by Doxygen are derivative works derived from the 00015 * input used in their production; they are not affected by this license. 00016 * 00017 */ 00018 00019 #include <stdlib.h> 00020 00021 #include "cppvalue.h" 00022 #include "constexp.h" 00023 00024 CPPValue parseOctal() 00025 { 00026 long val = 0; 00027 for (const char *p = g_strToken.data(); *p != 0; p++) 00028 { 00029 if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0'; 00030 } 00031 return CPPValue(val); 00032 } 00033 00034 CPPValue parseDecimal() 00035 { 00036 long val = 0; 00037 for (const char *p = g_strToken.data(); *p != 0; p++) 00038 { 00039 if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0'; 00040 } 00041 return CPPValue(val); 00042 } 00043 00044 CPPValue parseHexadecimal() 00045 { 00046 long val = 0; 00047 for (const char *p = g_strToken.data(); *p != 0; p++) 00048 { 00049 if (*p >= '0' && *p <= '9') val = val * 16 + *p - '0'; 00050 else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10; 00051 else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10; 00052 } 00053 //printf("parseHexadecimal %s->%x\n",g_strToken.data(),val); 00054 return CPPValue(val); 00055 } 00056 00057 CPPValue parseCharacter() // does not work for '\n' and the alike 00058 { 00059 if (g_strToken[1]=='\\') 00060 { 00061 switch(g_strToken[2]) 00062 { 00063 case 'n': return CPPValue((long)'\n'); 00064 case 't': return CPPValue((long)'\t'); 00065 case 'v': return CPPValue((long)'\v'); 00066 case 'b': return CPPValue((long)'\b'); 00067 case 'r': return CPPValue((long)'\r'); 00068 case 'f': return CPPValue((long)'\f'); 00069 case 'a': return CPPValue((long)'\a'); 00070 case '\\': return CPPValue((long)'\\'); 00071 case '?': return CPPValue((long)'\?'); 00072 case '\'': return CPPValue((long)'\''); 00073 case '"': return CPPValue((long)'"'); 00074 case '0': return parseOctal(); 00075 case 'x': 00076 case 'X': return parseHexadecimal(); 00077 default: printf("Invalid escape sequence %s found!\n",g_strToken.data()); 00078 return CPPValue(0L); 00079 } 00080 } 00081 return CPPValue((long)g_strToken[1]); 00082 } 00083 00084 CPPValue parseFloat() 00085 { 00086 return CPPValue(atof(g_strToken)); 00087 }