diff options
-rw-r--r-- | src/parse/css21props.c | 2 | ||||
-rw-r--r-- | src/utils/utils.h | 44 |
2 files changed, 44 insertions, 2 deletions
diff --git a/src/parse/css21props.c b/src/parse/css21props.c index 8a315b5..cbf4040 100644 --- a/src/parse/css21props.c +++ b/src/parse/css21props.c @@ -1683,7 +1683,7 @@ css_error parse_font_weight(css_css21 *c, if (token->lower.ptr == c->strings[INHERIT]) { flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { - int32_t num = 100; /** \todo strntol */ + int32_t num = integer_from_css_string(&token->lower); switch (num) { case 100: value = FONT_WEIGHT_100; break; case 200: value = FONT_WEIGHT_200; break; diff --git a/src/utils/utils.h b/src/utils/utils.h index ac19e59..85a4f9e 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -2,12 +2,14 @@ * This file is part of LibCSS. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php - * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org> + * Copyright 2007-8 John-Mark Bell <jmb@netsurf-browser.org> */ #ifndef css_utils_h_ #define css_utils_h_ +#include <libcss/types.h> + #ifndef max #define max(a,b) ((a)>(b)?(a):(b)) #endif @@ -25,4 +27,44 @@ #define UNUSED(x) ((x)=(x)) #endif +static inline int32_t integer_from_css_string(const css_string *string) +{ + size_t len; + const uint8_t *ptr; + int sign = 1; + int32_t val = 0; + + if (string == NULL || string->len == 0) + return 0; + + len = string->len; + ptr = string->ptr; + + /* Extract sign, if any */ + if (ptr[0] == '-') { + sign = -1; + len--; + ptr++; + } else if (ptr[0] == '+') { + len--; + ptr++; + } + + /** \todo check for overflow */ + + /* Now extract value, assuming base 10 */ + while (len > 0) { + /* Stop on first non-digit */ + if (ptr[0] <= '0' || '9' <= ptr[0]) + break; + + val *= 10; + val += ptr[0] - '0'; + ptr++; + len--; + } + + return val * sign; +} + #endif |