lib/sqlNum.c

Go to the documentation of this file.
00001 /* sqlnum.c - Routines to convert from ascii to integer
00002  * representation of numbers. 
00003  *
00004  * This file is copyright 2002 Jim Kent, but license is hereby
00005  * granted for all use - public, private or commercial. */
00006 
00007 #include "common.h"
00008 #include "sqlNum.h"
00009 
00010 static char const rcsid[] = "$Id: sqlNum.c,v 1.16 2005/12/09 22:39:36 hiram Exp $";
00011 
00012 unsigned sqlUnsigned(char *s)
00013 /* Convert series of digits to unsigned integer about
00014  * twice as fast as atoi (by not having to skip white 
00015  * space or stop except at the null byte.) */
00016 {
00017 unsigned res = 0;
00018 char *p = s;
00019 char c;
00020 
00021 while (((c = *(p++)) >= '0') && (c <= '9'))
00022     {
00023     res *= 10;
00024     res += c - '0';
00025     }
00026 if (c != '\0')
00027     errAbort("invalid unsigned number: \"%s\"", s);
00028 return res;
00029 }
00030 
00031 unsigned long sqlUnsignedLong(char *s)
00032 /* Convert series of digits to unsigned long about
00033  * twice as fast as atol (by not having to skip white 
00034  * space or stop except at the null byte.) */
00035 {
00036 unsigned long res = 0;
00037 char *p = s;
00038 char c;
00039 
00040 while (((c = *(p++)) >= '0') && (c <= '9'))
00041     {
00042     res *= 10;
00043     res += c - '0';
00044     }
00045 if (c != '\0')
00046     errAbort("invalid unsigned number: \"%s\"", s);
00047 return res;
00048 }
00049 
00050 int sqlSigned(char *s)
00051 /* Convert string to signed integer.  Unlike atol assumes 
00052  * all of string is number. */
00053 {
00054 int res = 0;
00055 char *p, *p0 = s;
00056 
00057 if (*p0 == '-')
00058     p0++;
00059 p = p0;
00060 while ((*p >= '0') && (*p <= '9'))
00061     {
00062     res *= 10;
00063     res += *p - '0';
00064     p++;
00065     }
00066 /* test for invalid character, empty, or just a minus */
00067 if ((*p != '\0') || (p == p0))
00068     errAbort("invalid signed number: \"%s\"", s);
00069 if (*s == '-')
00070     return -res;
00071 else
00072     return res;
00073 }
00074 
00075 long long sqlLongLong(char *s)
00076 /* Convert string to a long long.  Unlike atol assumes all of string is
00077  * number. */
00078 {
00079 long long res = 0;
00080 char *p, *p0 = s;
00081 
00082 if (*p0 == '-')
00083     p0++;
00084 p = p0;
00085 while ((*p >= '0') && (*p <= '9'))
00086     {
00087     res *= 10;
00088     res += *p - '0';
00089     p++;
00090     }
00091 /* test for invalid character, empty, or just a minus */
00092 if ((*p != '\0') || (p == p0))
00093     errAbort("invalid signed number: \"%s\"", s);
00094 if (*s == '-')
00095     return -res;
00096 else
00097     return res;
00098 }
00099 
00100 float sqlFloat(char *s)
00101 /* Convert string to a float.  Assumes all of string is number
00102  * and aborts on an error. */
00103 {
00104 char* end;
00105 /*      used to have an ifdef here to use strtof() but that doesn't
00106  *      actually exist on all systems and since strtod() does, may as
00107  *      well use it since it will do the job here.
00108  */
00109 float val = (float) strtod(s, &end);
00110 
00111 if ((end == s) || (*end != '\0'))
00112     errAbort("invalid float: %s", s);
00113 return val;
00114 }
00115 
00116 double sqlDouble(char *s)
00117 /* Convert string to a double.  Assumes all of string is number
00118  * and aborts on an error. */
00119 {
00120 char* end;
00121 double val = strtod(s, &end);
00122 
00123 if ((end == s) || (*end != '\0'))
00124     errAbort("invalid double: %s", s);
00125 return val;
00126 }

Generated on Tue Dec 25 18:39:32 2007 for blat by  doxygen 1.5.2