From: Stephen D. <sd...@us...> - 2005-08-19 07:50:52
|
Update of /cvsroot/naviserver/naviserver/nsd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19013/nsd Modified Files: str.c Log Message: * include/ns.h: * nsd/str.c (Ns_StrToInt): Convenience function for parsing integers. atoi is not always threadsafe (e.g. Darwin) and makes it impossible to handle errors, strtol makes it difficult. Index: str.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/str.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** str.c 19 Aug 2005 05:49:34 -0000 1.4 --- str.c 19 Aug 2005 07:50:44 -0000 1.5 *************** *** 196,199 **** --- 196,237 ---- *---------------------------------------------------------------------- * + * Ns_StrToInt -- + * + * Attempt to convert the string value to an integer. + * + * Results: + * NS_OK and *intPtr updated, NS_ERROR if the number cannot be + * parsed or overflows. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + + int + Ns_StrToInt(CONST char *string, int *intPtr) + { + long lval; + char *ep; + + errno = 0; + lval = strtol(string, &ep, 10); + if (string[0] == '\0' || *ep != '\0') { + return NS_ERROR; + } + if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) + || (lval > INT_MAX || lval < INT_MIN)) { + return NS_ERROR; + } + *intPtr = (int) lval; + + return NS_OK; + } + + + /* + *---------------------------------------------------------------------- + * * Ns_Match -- * |