Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

chr2snum.cpp

Go to the documentation of this file.
00001 // +-------------------------------------------------------------------------+
00002 // |               I__n__t__e__L__i__b           0.6.10 development          |
00003 // | Copyright (c) Andrey Vikt. Stolyarov <crocodil_AT_croco.net> 2000-2007. |
00004 // |                                                                         |
00005 // | This is free software. The library part is available under              |
00006 // |                               GNU LESSER GENERAL PUBLIC LICENSE v.2.1.  |
00007 // | GNU LGPL v2.1 is found in docs/gnu_gpl2.txt,  or at  http://www.gnu.org |
00008 // |     Please see also docs/readme.txt and visit http://www.intelib.org    |
00009 // |                                                                         |
00010 // | !!! THERE IS NO WARRANTY OF ANY KIND, NEITHER EXPRESSED NOR IMPLIED !!! |
00011 // +-------------------------------------------------------------------------+
00012 
00013 
00014 
00015 
00016 #include "../sexpress/sexpress.hpp"
00017 
00018 /*
00019  * The function parses given string assuming it represents a number, 
00020  * either integer or floating point, and returns lisp s-expression 
00021  * of appropriate type.
00022  * NOTE the assumption made that (ch - '0') will give the numberic value 
00023  * for any digit character ch.  
00024  */
00025 SReference Charp2LispNumber(const char* s)
00026 {
00027     intelib_integer_t i=0;
00028     intelib_float_t f=0;
00029     int is_begin = 0;
00030     int is_ok = 0;
00031     int is_neg = 0;
00032     int is_float = 0;
00033     intelib_float_t float_mul = 1;
00034     for(const char *p = s; *p != 0; p++) {
00035         switch(*p) {
00036             case '+':
00037                 if(is_begin)
00038                     goto FINISH;
00039                 else
00040                     is_begin = 1;
00041                 break;
00042             case '-':
00043                 if(is_begin)
00044                     goto FINISH;
00045                 else {
00046                     is_begin = 1;
00047                     is_neg = 1;
00048                 }
00049                 break;
00050             case '.':
00051                 if(is_float)
00052                     goto FINISH;
00053                 else {
00054                     is_float = 1;
00055                     is_begin = 1;
00056                     is_ok = 1;
00057                     f = i;
00058                 }
00059                 break;
00060             case '0':
00061             case '1':
00062             case '2':
00063             case '3':
00064             case '4':
00065             case '5':
00066             case '6':
00067             case '7':
00068             case '8':
00069             case '9':
00070                 is_begin = 1;
00071                 is_ok = 1;
00072                 if(is_float) {
00073                     float_mul *= 10;
00074                     f += (*p - '0') / float_mul;
00075                 } else {
00076                     i *= 10; i += (*p - '0');
00077                 }
00078                 break;
00079             default:
00080                 if(is_begin) goto FINISH; // couldn't use break here...
00081         }
00082     }
00083 FINISH:
00084     if(!is_ok) return *PTheEmptyList;
00085 if(is_neg) { f = -f; i = -i; }
00086     if(is_float) return SReference(f);
00087     else return SReference(i);
00088 }

Generated on Tue Dec 18 00:39:43 2007 for InteLib by  doxygen 1.4.1