/* librock/text/ss.c version 1998.08.20 librock_CHISEL _summary string scanning and parsing Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************/ #ifdef librock_NOTLIBROCK /**************************************************************/ /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED #include #define librock_PTR #define librock_CONST const #define librock_PRIVATE static /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_ss #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_ss /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_ss[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_sscons librock_sscons #define librock_body_ssdec librock_ssdec #define librock_body_ssconsl librock_ssconsl /**************************************************************/ #endif #ifndef librock_NOIMPL_ssconsl /****************************************************************/ librock_PRIVATE int librock_xtod(char ch) { /* Helper function. return the integer corresponding to the ASCII character */ if ((ch >= '0')&&(ch <= '9')) { return(ch - '0'); } else if (((ch >= 'a')&&(ch <= 'z'))|| ((ch >= 'A')&&(ch <= 'Z'))) { ch &= 0x1f; return(ch + 9); } else { return 0x7fff; /* Show not a character */ } } /* xtod */ int librock_body_ssconsl(librock_CONST char librock_PTR *strptr,int radix,long librock_PTR *plRet) { /* string scan for a long integer in the specified radix (base) */ #define librock_FNTYPE_ssconsl fni_s_i_L #ifdef librock_MANUAL_ssconsl /* librock_ssconsl - string scan for a long int in the specified radix (base) librock_sscons - string scan for an int in the specified radix (base) librock_ssdec - string scan for an int in base 10 */ /**/ #include int librock_ssconsl( const char *strptr, int radix, long librock_PTR *plRet ); int librock_sscons( const char *sptr, int radix, int *piRet ); int librock_ssdec( const char *sptr, int *piRet ); /* Skip leading ' ' or '\t', accept an optional '-', then convert to an int or long int in the specified radix (base.) Stores conversion result indirectly (at pRet, if not NULL), and returns the number of characters processed. Radix can be up to 36. A-Z are taken in upper or lower case for digits after 0-9. When radix == 0, takes prefix from the string prefix - radix 0 - octal 0x - hex 0b - binary default - decimal Processing stops at the first character not a valid digit in the radix. Returns number of characters processed or 0 if could not process any characters. If 0 is returned, the value at *pRet is unchanged. Typical use is to replace atol(), to gain the additional radix conversions or to determine that characters were processed. */ #ifdef librock_TYPICAL_USE_sscons const char *ptr = "07 0x08 0b1001 10 Q 11"; while(1) { int val; int cnt = librock_sscons(ptr,0,&val); if (!cnt) { break; } printf("%d\n",val); ptr += cnt; while (*ptr == ' ') { ptr++; } } #endif /* // [No external calls] */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ int index; /* used for printing errors */ long ret; /* used for generating return values */ long temp; int ind; /* the return value */ int sign = 0; int conv; register librock_CONST char librock_PTR *sptr; sptr = strptr; while((*sptr==' ')||(*sptr == '\t')) { sptr++; /* skip leading whitespace */ } if (*sptr == '-') { sptr++; sign = -1; if (*sptr <= ' ') { return 0; } } ind = sptr - strptr; if (radix) { /* Already preselected */ index = 0; if (librock_xtod(sptr[index]) >= radix) { /* Test added 2002-04-05 */ return 0; } } else if (sptr[0] == '0') { /* Accept a prefix */ if (sptr[1] == 'x') { /* hexadecimal */ radix = 16; index = 2; } else if (sptr[1] == 'b') { /* this is binary */ radix = 2; index = 2; } else if ((sptr[1] >= '0')&&(sptr[1] <= '7')) { /* octal */ radix = 8; index = 1; } else { radix = 10; index = 0; } } else if ((sptr[0] >= '0')&&(sptr[0] <= '9')) { radix = 10; index = 0; } else { /* garbage? */ return 0; } ret = 0; while((conv=librock_xtod(sptr[index])) < radix) { temp = radix * ret; if ((unsigned long) temp + conv < (unsigned long) ret) { /* Overflow */ return 0; } ret = temp + conv; index++; } if (sign) { ret = -ret; } if (index) { if (plRet) { *plRet = ret; } } return(ind+index); } /* librock_ssconsl */ /****************************************************************/ #endif /* NOIMP */ #ifndef librock_NOIMPL_sscons #define librock_FNTYPE_sscons fni_s_i_I /****************************************************************/ int librock_body_sscons(librock_CONST char librock_PTR *sptr,int radix, int librock_PTR *piRet) { /* string scan for an integer in the specified radix (base) */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ long ret1; int i; int ret; ret = librock_ssconsl(sptr,radix,&ret1); if (!ret) { /* 7-28-2001 */ return ret; } i = (int) ret1; if ((long) i != ret1) { return 0; } if (piRet) { *piRet = i; } return ret; } /* librock_sscons */ /****************************************************************/ #endif /* NOIMP */ #ifndef librock_NOIMPL_ssdec #define librock_FNTYPE_ssdec fni_s_I /****************************************************************/ int librock_body_ssdec(librock_CONST char librock_PTR *sptr,int librock_PTR *piRet) { /* string scan for an integer of radix 10. */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ return librock_sscons(sptr,10,piRet); } /* librock_ssdec */ /****************************************************************/ #endif /* NOIMP */ /* $Log: ss.c,v $ Revision 1.7 2002/08/01 14:54:45 forrest@mibsoftware.com rights=#1 Update TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.6 2002/04/09 03:27:57 forrest@mibsoftware.com rights=#1 FNTYPE. Update LICENSE in manual. Revision 1.5 2002/04/06 04:00:39 forrest@mibsoftware.com rights=#1 Add FNTYPE decls for regression tests. Revision 1.4 2002/02/10 03:50:13 Administrator Fix _body_ssconsl Revision 1.3 2002/02/05 21:48:43 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:08:28 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:25:23 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */