Module: sems Branch: master Commit: 932510b94a25ab997037b991f398244c6d78fde2 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=932510b94a25ab997037b991f398244c6d78fde2
Author: Stefan Sayer <[email protected]> Committer: Stefan Sayer <[email protected]> Date: Thu Nov 25 01:44:08 2010 +0100 str2int, str2long and long2str methods str2int is str2i with proper return code str2long and long2str work on long --- core/AmUtils.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/AmUtils.h | 45 ++++++++++++++++++- 2 files changed, 173 insertions(+), 1 deletions(-) diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 0ecb188..10c107f 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -88,6 +88,26 @@ string int2str(int val) return string((char*)(buffer+i+1)); } +string long2str(long int val) +{ + char buffer[64] = {0,0}; + int i=62; + ldiv_t d; + + d.quot = abs(val); + do{ + d = ldiv(d.quot,10); + buffer[i] = _int2str_lookup[d.rem]; + }while(--i && d.quot); + + if (i && (val<0)) { + buffer[i]='-'; + i--; + } + + return string((char*)(buffer+i+1)); +} + static char _int2hex_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','A','B','C','D','E','F' }; string int2hex(unsigned int val) @@ -234,6 +254,115 @@ bool str2i(char*& str, unsigned int& result, char sep) return true; } +bool str2int(const string& str, int& result) +{ + char* s = (char*)str.c_str(); + return str2int(s,result); +} + +bool str2int(char*& str, int& result, char sep) +{ + int ret=0; + int i=0; + char* init = str; + int sign = 1; + + for(; (*str != '\0') && (*str == ' '); str++); + + if (*str == '-') { + sign = -1; + str++; + for(; (*str != '\0') && (*str == ' '); str++); + } + + for(; *str != '\0';str++){ + if ( (*str <= '9' ) && (*str >= '0') ){ + ret=ret*10+*str-'0'; + i++; + if (i>10) goto error_digits; + } else { + + bool eol = false; + switch(*str){ + case 0xd: + case 0xa: + case 0x0: + eol = true; + } + + if( (*str != sep) && !eol ) + goto error_char; + + break; + } + } + + result = ret * sign; + return true; + + error_digits: + DBG("str2int: too many digits in [%s]\n", init); + return false; + error_char: + DBG("str2i: unexpected char 0x%x in %s\n", *str, init); + return false; +} + +// long int could probably be the same size as int +bool str2long(const string& str, long& result) +{ + char* s = (char*)str.c_str(); + return str2long(s,result); +} + +bool str2long(char*& str, long& result, char sep) +{ + long ret=0; + int i=0; + char* init = str; + long sign = 1; + + for(; (*str != '\0') && (*str == ' '); str++); + + if (*str == '-') { + sign = -1; + str++; + for(; (*str != '\0') && (*str == ' '); str++); + } + + for(; *str != '\0';str++){ + if ( (*str <= '9' ) && (*str >= '0') ){ + ret=ret*10+*str-'0'; + i++; + if (i>20) goto error_digits; + } else { + + bool eol = false; + switch(*str){ + case 0xd: + case 0xa: + case 0x0: + eol = true; + } + + if( (*str != sep) && !eol ) + goto error_char; + + break; + } + } + + result = ret * sign; + return true; + + error_digits: + DBG("str2long: too many digits in [%s]\n", init); + return false; + error_char: + DBG("str2long: unexpected char 0x%x in %s\n", *str, init); + return false; +} + int parse_return_code(const char* lbuf, unsigned int& res_code, string& res_msg ) { char res_code_str[4] = {'\0'}; diff --git a/core/AmUtils.h b/core/AmUtils.h index 890840f..88c72f6 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -49,6 +49,12 @@ using std::string; */ string int2str(int val); + +/** + * Convert a long to a string. + */ +string long2str(long int val); + /** * Convert an unsigned int to a string using hexdecimal representation. */ @@ -76,7 +82,7 @@ string double2str(double val); * Convert a string to an uint. * @param str [in] string to convert. * @param result [out] result integer. - * @return true if failed. + * @return true if failed (!!!) */ bool str2i(const string& str, unsigned int& result); @@ -85,9 +91,46 @@ bool str2i(const string& str, unsigned int& result); * @param str [in,out] gets incremented until sep char or error occurs * @param result [out] result of the function * @param sep [in] character seprating the number to convert and the next token + * @return true if failed (!!!) */ bool str2i(char*& str, unsigned int& result, char sep = ' '); + +/** + * Convert a string to an int. + * @param str [in] string to convert. + * @param result [out] result integer. + * @return true on success (!!!) + */ +bool str2int(const string& str, int& result); + +/** + * Internal version of preceeding 'str2int' method. + * @param str [in,out] gets incremented until sep char or error occurs + * @param result [out] result of the function + * @param sep [in] character seprating the number to convert and the next token + * @return true on success (!!!) + */ +bool str2int(char*& str, int& result, char sep = ' '); + +/** + * Convert a string to a long int. + * On many systems nowadays this could be the same as str2int. + * @param str [in] string to convert. + * @param result [out] result integer. + * @return true if on success (!!!). + */ +bool str2long(const string& str, long& result); + +/** + * Internal version of preceeding 'str2long' method. + * @param str [in,out] gets incremented until sep char or error occurs + * @param result [out] result of the function + * @param sep [in] character seprating the number to convert and the next token + * @return true on success + */ +bool str2long(char*& str, long& result, char sep = ' '); + /** * Parse code/reason line. * Syntax: "code reason" _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
