1 /* 2 * This file is part of LibCSS. 3 * Licensed under the MIT License, 4 * http://www.opensource.org/licenses/mit-license.php 5 * Copyright 2007-8 John-Mark Bell <jmb@netsurf-browser.org> 6 */ 7 8 #ifndef css_utils_h_ 9 #define css_utils_h_ 10 11 #include <libwapcaplet/libwapcaplet.h> 12 13 #include <libcss/types.h> 14 #include <libcss/errors.h> 15 16 #ifndef max 17 #define max(a,b) ((a)>(b)?(a):(b)) 18 #endif 19 20 #ifndef min 21 #define min(a,b) ((a)<(b)?(a):(b)) 22 #endif 23 24 #ifndef SLEN 25 /* Calculate length of a string constant */ 26 #define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */ 27 #endif 28 29 #ifndef UNUSED 30 #define UNUSED(x) ((void)(x)) 31 #endif 32 33 #ifndef N_ELEMENTS 34 #define N_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0])) 35 #endif 36 37 css_fixed css__number_from_lwc_string(lwc_string *string, bool int_only, 38 size_t *consumed); 39 css_fixed css__number_from_string(const uint8_t *data, size_t len, 40 bool int_only, size_t *consumed); 41 isDigit(uint8_t c)42static inline bool isDigit(uint8_t c) 43 { 44 return '0' <= c && c <= '9'; 45 } 46 isHex(uint8_t c)47static inline bool isHex(uint8_t c) 48 { 49 return isDigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); 50 } 51 charToHex(uint8_t c)52static inline uint32_t charToHex(uint8_t c) 53 { 54 c -= '0'; 55 56 if (c > 9) 57 c -= 'A' - '9' - 1; 58 59 if (c > 15) 60 c -= 'a' - 'A'; 61 62 return c; 63 } 64 css_error_from_lwc_error(lwc_error err)65static inline css_error css_error_from_lwc_error(lwc_error err) 66 { 67 switch (err) { 68 case lwc_error_ok: 69 return CSS_OK; 70 case lwc_error_oom: 71 return CSS_NOMEM; 72 case lwc_error_range: 73 return CSS_BADPARM; 74 default: 75 break; 76 } 77 return CSS_INVALID; 78 } 79 80 #endif 81