Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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) ((x)=(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.  
  42. static inline bool isDigit(uint8_t c)
  43. {
  44.         return '0' <= c && c <= '9';
  45. }
  46.  
  47. static inline bool isHex(uint8_t c)
  48. {
  49.         return isDigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
  50. }
  51.  
  52. static 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.  
  65. static inline css_error
  66. css_error_from_lwc_error(lwc_error err)
  67. {
  68.         switch (err) {
  69.         case lwc_error_ok:
  70.                 return CSS_OK;
  71.         case lwc_error_oom:
  72.                 return CSS_NOMEM;
  73.         case lwc_error_range:
  74.                 return CSS_BADPARM;
  75.         default:
  76.                 break;
  77.         }
  78.         return CSS_INVALID;
  79. }
  80.  
  81. #endif
  82.