diff options
author | Antti-Juhani Kaijanaho <ajk@debian.org> | 2006-01-28 21:54:20 +0100 |
---|---|---|
committer | Antti-Juhani Kaijanaho <ajk@debian.org> | 2006-01-28 21:54:20 +0100 |
commit | f87204b3d2424b27b84783df015031062e7cb668 (patch) | |
tree | 2361d08911d3984a5d9431836d541198bab296de /strutil.c | |
parent | e87c95bb705d192324c6214cb7309e9442ed88ca (diff) | |
download | dctrl-tools-f87204b3d2424b27b84783df015031062e7cb668.tar.gz |
Import 2.3
Diffstat (limited to 'strutil.c')
-rw-r--r-- | strutil.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -19,6 +19,43 @@ #include <ctype.h> #include "strutil.h" +bool str2intmax(intmax_t * rvp, char const * s, size_t len) +{ + bool negative = false; + size_t i = 0; + while (i < len + && s[i] == ' ' + && s[i] == '\t' + && s[i] == '\n' + && s[i] == '\r') + ++i; + if (i < len && s[i] == '-') { + ++i; + negative = true; + } + uintmax_t abs = 0; + for (; i < len; ++i) { + char ch = s[i]; + if (! ('0' <= ch && ch <= '9')) { + break; + } + int r = ch - '0'; + abs = abs * 10 + r; + if (abs > (uintmax_t)INTMAX_MAX) { + return false; + } + } + while (i < len + && s[i] == ' ' + && s[i] == '\t' + && s[i] == '\n' + && s[i] == '\r') + ++i; + if (i < len) return false; // broken input + *rvp = negative ? -abs : abs; + return true; +} + const char * left_trimmed(const char * s) { const char * p; |