Subversion Repositories Kolibri OS

Rev

Rev 3584 | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2004-2007 James Bursa <bursa@users.sourceforge.net>
  3.  * Copyright 2004 John Tytgat <joty@netsurf-browser.org>
  4.  *
  5.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  6.  *
  7.  * NetSurf is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; version 2 of the License.
  10.  *
  11.  * NetSurf is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. #ifndef _NETSURF_UTILS_UTILS_H_
  21. #define _NETSURF_UTILS_UTILS_H_
  22.  
  23. #include <inttypes.h>
  24. #include <stdbool.h>
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29. #include <regex.h>
  30. #include <assert.h>
  31.  
  32. #ifndef NOF_ELEMENTS
  33. #define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array)))
  34. #endif
  35.  
  36. #ifndef ABS
  37. #define ABS(x) (((x)>0)?(x):(-(x)))
  38. #endif
  39.  
  40. #ifdef __MINT__ /* avoid using GCCs builtin min/max functions */
  41. #undef min
  42. #undef max
  43. #endif
  44.  
  45. #ifndef min
  46. #define min(x,y) (((x)<(y))?(x):(y))
  47. #endif
  48.  
  49. #ifndef max
  50. #define max(x,y) (((x)>(y))?(x):(y))
  51. #endif
  52.  
  53. #ifndef PRIxPTR
  54. #define PRIxPTR "x"
  55. #endif
  56.  
  57. #ifndef PRId64
  58. #define PRId64 "lld"
  59. #endif
  60.  
  61. #if defined(_WIN32)
  62. #define SSIZET_FMT "Iu"
  63. #else
  64. #define SSIZET_FMT "zd"
  65. #endif
  66.  
  67. #if defined(__GNUC__) && (__GNUC__ < 3)
  68. #define FLEX_ARRAY_LEN_DECL 0
  69. #else
  70. #define FLEX_ARRAY_LEN_DECL
  71. #endif
  72.  
  73. #if defined(__HAIKU__) || defined(__BEOS__)
  74. #define strtof(s,p) ((float)(strtod((s),(p))))
  75. #endif
  76.  
  77. #if !defined(ceilf) && defined(__MINT__)
  78. #define ceilf(x) (float)ceil((double)x)
  79. #endif
  80.  
  81. /**
  82.  * Calculate length of constant C string.
  83.  *
  84.  * \param  x       a constant C string.
  85.  * \return the length of C string without its terminating NUL accounted.
  86.  */
  87. #define SLEN(x) (sizeof((x)) - 1)
  88.  
  89. enum query_response {
  90.   QUERY_CONTINUE,
  91.   QUERY_YES,
  92.   QUERY_NO,
  93.   QUERY_ESCAPE
  94. };
  95.  
  96. typedef int query_id;
  97.  
  98. #define QUERY_INVALID ((query_id)-1)
  99.  
  100. typedef struct
  101. {
  102.         void (*confirm)(query_id id, enum query_response res, void *pw);
  103.         void (*cancel)(query_id, enum query_response res, void *pw);
  104. } query_callback;
  105.  
  106. #ifdef HAVE_MKDIR
  107. #define nsmkdir(dir, mode) mkdir((dir), (mode))
  108. #else
  109. #define nsmkdir(dir, mode) mkdir((dir))
  110. #endif
  111.  
  112. #ifndef timeradd
  113. #define timeradd(a, aa, result)                                         \
  114.         do {                                                            \
  115.                 (result)->tv_sec = (a)->tv_sec + (aa)->tv_sec;          \
  116.                 (result)->tv_usec = (a)->tv_usec + (aa)->tv_usec;       \
  117.                 if ((result)->tv_usec >= 1000000) {                     \
  118.                         ++(result)->tv_sec;                             \
  119.                         (result)->tv_usec -= 1000000;                   \
  120.                 }                                                       \
  121.         } while (0)
  122. #endif
  123.  
  124. #ifndef timersub
  125. #define timersub(a, aa, result)                                         \
  126.         do {                                                            \
  127.                 (result)->tv_sec = (a)->tv_sec - (aa)->tv_sec;          \
  128.                 (result)->tv_usec = (a)->tv_usec - (aa)->tv_usec;       \
  129.                 if ((result)->tv_usec < 0) {                            \
  130.                         --(result)->tv_sec;                             \
  131.                         (result)->tv_usec += 1000000;                   \
  132.                 }                                                       \
  133.         } while (0)
  134. #endif
  135.  
  136.  
  137. /**
  138.  * Private-word-capable realloc() implementation which
  139.  * behaves as most NS libraries expect in the face of
  140.  * realloc(ptr, 0) and realloc(NULL, size).
  141.  *
  142.  * \param ptr The pointer for reallocation
  143.  * \param size The number of bytes for the allocation
  144.  * \param pw A "private word" which we ignore.
  145.  * \return The new pointer (NULL on frees or errors)
  146.  */
  147. void *ns_realloc(void *ptr, size_t size, void *pw);
  148.  
  149. char * strip(char * const s);
  150. int whitespace(const char * str);
  151. char * squash_whitespace(const char * s);
  152. char *remove_underscores(const char *s, bool replacespace);
  153. char *cnv_space2nbsp(const char *s);
  154. bool is_dir(const char *path);
  155. void regcomp_wrapper(regex_t *preg, const char *regex, int cflags);
  156. char *human_friendly_bytesize(unsigned long bytesize);
  157. const char *rfc1123_date(time_t t);
  158. unsigned int wallclock(void);
  159.  
  160. /**
  161.  * Return a hex digit for the given numerical value.
  162.  *
  163.  * \return character in range 0-9a-f
  164.  */
  165. inline static char digit2lowcase_hex(unsigned char digit) {
  166.         assert(digit < 16);
  167.         return "0123456789abcdef"[digit];
  168. }
  169.  
  170. /**
  171.  * Return a hex digit for the given numerical value.
  172.  *
  173.  * \return character in range 0-9A-F
  174.  */
  175. inline static char digit2uppercase_hex(unsigned char digit) {
  176.         assert(digit < 16);
  177.         return "0123456789ABCDEF"[digit];
  178. }
  179.  
  180.  
  181. /* Platform specific functions */
  182. void die(const char * const error);
  183. void warn_user(const char *warning, const char *detail);
  184. query_id query_user(const char *query, const char *detail,
  185.         const query_callback *cb, void *pw, const char *yes, const char *no);
  186. void query_close(query_id);
  187. void PDF_Password(char **owner_pass, char **user_pass, char *path);
  188. char *filename_from_path(char *path);
  189. bool path_add_part(char *path, int length, const char *newpart);
  190. #endif
  191.