2003-06-30 16:44:03 +04:00
|
|
|
/*
|
2007-01-30 01:27:15 +03:00
|
|
|
* Copyright 2004-2007 James Bursa <bursa@users.sourceforge.net>
|
2008-08-05 05:30:31 +04:00
|
|
|
* Copyright 2004 John Tytgat <joty@netsurf-browser.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2002-04-22 13:24:35 +04:00
|
|
|
*/
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
#ifndef _NETSURF_UTILS_UTILS_H_
|
|
|
|
#define _NETSURF_UTILS_UTILS_H_
|
2002-08-12 03:04:02 +04:00
|
|
|
|
2010-08-03 01:44:35 +04:00
|
|
|
#include <inttypes.h>
|
2003-11-08 02:47:55 +03:00
|
|
|
#include <stdbool.h>
|
2004-07-06 00:19:52 +04:00
|
|
|
#include <stddef.h>
|
2002-08-12 03:04:02 +04:00
|
|
|
#include <stdlib.h>
|
2003-12-27 23:15:23 +03:00
|
|
|
#include <sys/types.h>
|
2010-10-17 04:08:35 +04:00
|
|
|
#include <sys/time.h>
|
2003-12-27 23:15:23 +03:00
|
|
|
#include <regex.h>
|
2008-05-25 19:04:22 +04:00
|
|
|
#include <assert.h>
|
2002-08-12 03:04:02 +04:00
|
|
|
|
2013-11-15 01:01:51 +04:00
|
|
|
struct dirent;
|
|
|
|
|
2005-04-15 09:52:04 +04:00
|
|
|
#ifndef NOF_ELEMENTS
|
|
|
|
#define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array)))
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2005-04-22 01:36:23 +04:00
|
|
|
#ifndef ABS
|
|
|
|
#define ABS(x) (((x)>0)?(x):(-(x)))
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
#ifdef __MINT__ /* avoid using GCCs builtin min/max functions */
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2005-04-22 01:36:23 +04:00
|
|
|
#ifndef min
|
|
|
|
#define min(x,y) (((x)<(y))?(x):(y))
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2005-04-22 01:36:23 +04:00
|
|
|
#ifndef max
|
|
|
|
#define max(x,y) (((x)>(y))?(x):(y))
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2008-08-13 21:37:59 +04:00
|
|
|
#ifndef PRIxPTR
|
|
|
|
#define PRIxPTR "x"
|
|
|
|
#endif
|
2012-08-13 20:00:43 +04:00
|
|
|
|
2010-08-03 01:44:35 +04:00
|
|
|
#ifndef PRId64
|
|
|
|
#define PRId64 "lld"
|
|
|
|
#endif
|
2008-08-13 21:37:59 +04:00
|
|
|
|
2014-04-28 20:36:51 +04:00
|
|
|
/* Windows does not have POSIX formating codes or mkdir so work around that */
|
2012-08-13 20:00:43 +04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#define SSIZET_FMT "Iu"
|
2014-04-28 20:36:51 +04:00
|
|
|
#define nsmkdir(dir, mode) mkdir((dir))
|
2012-08-13 20:00:43 +04:00
|
|
|
#else
|
|
|
|
#define SSIZET_FMT "zd"
|
2014-04-28 20:36:51 +04:00
|
|
|
#define nsmkdir(dir, mode) mkdir((dir), (mode))
|
2012-08-13 20:00:43 +04:00
|
|
|
#endif
|
|
|
|
|
2012-03-19 22:24:43 +04:00
|
|
|
#if defined(__GNUC__) && (__GNUC__ < 3)
|
|
|
|
#define FLEX_ARRAY_LEN_DECL 0
|
|
|
|
#else
|
|
|
|
#define FLEX_ARRAY_LEN_DECL
|
|
|
|
#endif
|
|
|
|
|
2008-08-13 21:37:59 +04:00
|
|
|
#if defined(__HAIKU__) || defined(__BEOS__)
|
|
|
|
#define strtof(s,p) ((float)(strtod((s),(p))))
|
|
|
|
#endif
|
2005-04-15 09:52:04 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
#if !defined(ceilf) && defined(__MINT__)
|
|
|
|
#define ceilf(x) (float)ceil((double)x)
|
|
|
|
#endif
|
|
|
|
|
2008-05-25 19:51:30 +04:00
|
|
|
/**
|
|
|
|
* Calculate length of constant C string.
|
|
|
|
*
|
|
|
|
* \param x a constant C string.
|
|
|
|
* \return the length of C string without its terminating NUL accounted.
|
|
|
|
*/
|
|
|
|
#define SLEN(x) (sizeof((x)) - 1)
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
enum query_response {
|
|
|
|
QUERY_CONTINUE,
|
|
|
|
QUERY_YES,
|
|
|
|
QUERY_NO,
|
|
|
|
QUERY_ESCAPE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int query_id;
|
|
|
|
|
|
|
|
#define QUERY_INVALID ((query_id)-1)
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
void (*confirm)(query_id id, enum query_response res, void *pw);
|
|
|
|
void (*cancel)(query_id, enum query_response res, void *pw);
|
|
|
|
} query_callback;
|
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
#ifndef timeradd
|
|
|
|
#define timeradd(a, aa, result) \
|
|
|
|
do { \
|
|
|
|
(result)->tv_sec = (a)->tv_sec + (aa)->tv_sec; \
|
|
|
|
(result)->tv_usec = (a)->tv_usec + (aa)->tv_usec; \
|
|
|
|
if ((result)->tv_usec >= 1000000) { \
|
|
|
|
++(result)->tv_sec; \
|
|
|
|
(result)->tv_usec -= 1000000; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef timersub
|
|
|
|
#define timersub(a, aa, result) \
|
|
|
|
do { \
|
|
|
|
(result)->tv_sec = (a)->tv_sec - (aa)->tv_sec; \
|
|
|
|
(result)->tv_usec = (a)->tv_usec - (aa)->tv_usec; \
|
|
|
|
if ((result)->tv_usec < 0) { \
|
|
|
|
--(result)->tv_sec; \
|
|
|
|
(result)->tv_usec += 1000000; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-04-30 20:06:03 +04:00
|
|
|
|
2002-04-22 13:24:35 +04:00
|
|
|
char * strip(char * const s);
|
|
|
|
int whitespace(const char * str);
|
2002-05-22 01:27:29 +04:00
|
|
|
char * squash_whitespace(const char * s);
|
2009-12-18 02:55:02 +03:00
|
|
|
char *remove_underscores(const char *s, bool replacespace);
|
2004-07-06 00:19:52 +04:00
|
|
|
char *cnv_space2nbsp(const char *s);
|
2003-11-08 02:47:55 +03:00
|
|
|
bool is_dir(const char *path);
|
2003-12-27 23:15:23 +03:00
|
|
|
void regcomp_wrapper(regex_t *preg, const char *regex, int cflags);
|
2004-04-25 15:40:05 +04:00
|
|
|
char *human_friendly_bytesize(unsigned long bytesize);
|
2006-02-06 03:10:09 +03:00
|
|
|
const char *rfc1123_date(time_t t);
|
2007-01-30 18:32:31 +03:00
|
|
|
unsigned int wallclock(void);
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2013-11-15 01:01:51 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Comparison function for sorting directories.
|
|
|
|
*
|
|
|
|
* Correctly orders non zero-padded numerical parts.
|
|
|
|
* ie. produces "file1, file2, file10" rather than "file1, file10, file2".
|
|
|
|
*
|
|
|
|
* d1 first directory entry
|
|
|
|
* d2 second directory entry
|
|
|
|
*/
|
|
|
|
int dir_sort_alpha(const struct dirent **d1, const struct dirent **d2);
|
|
|
|
|
2008-05-25 19:04:22 +04:00
|
|
|
/**
|
|
|
|
* Return a hex digit for the given numerical value.
|
|
|
|
*
|
|
|
|
* \return character in range 0-9a-f
|
|
|
|
*/
|
|
|
|
inline static char digit2lowcase_hex(unsigned char digit) {
|
|
|
|
assert(digit < 16);
|
|
|
|
return "0123456789abcdef"[digit];
|
|
|
|
}
|
|
|
|
|
2011-10-05 02:32:04 +04:00
|
|
|
/**
|
|
|
|
* Return a hex digit for the given numerical value.
|
|
|
|
*
|
|
|
|
* \return character in range 0-9A-F
|
|
|
|
*/
|
|
|
|
inline static char digit2uppercase_hex(unsigned char digit) {
|
|
|
|
assert(digit < 16);
|
|
|
|
return "0123456789ABCDEF"[digit];
|
|
|
|
}
|
|
|
|
|
2008-05-25 19:04:22 +04:00
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
/* Platform specific functions */
|
|
|
|
void die(const char * const error);
|
2004-06-11 00:41:26 +04:00
|
|
|
void warn_user(const char *warning, const char *detail);
|
2014-01-16 04:52:17 +04:00
|
|
|
void PDF_Password(char **owner_pass, char **user_pass, char *path);
|
|
|
|
|
2002-08-12 03:04:02 +04:00
|
|
|
#endif
|