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
|
|
|
*/
|
|
|
|
|
2014-05-12 00:31:14 +04:00
|
|
|
/**
|
|
|
|
* \file utils/utils.h
|
|
|
|
* \brief Interface to a number of general purpose functionality.
|
|
|
|
* \todo Many of these functions and macros should have their own headers.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <regex.h>
|
2008-05-25 19:04:22 +04:00
|
|
|
#include <assert.h>
|
2014-05-04 21:29:53 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "utils/errors.h"
|
2002-08-12 03:04:02 +04:00
|
|
|
|
2014-11-09 18:28:03 +03:00
|
|
|
/** Rectangle coordinates */
|
2014-11-02 23:10:32 +03:00
|
|
|
struct rect {
|
2014-11-09 18:28:03 +03:00
|
|
|
int x0, y0; /**< Top left */
|
|
|
|
int x1, y1; /**< Bottom right */
|
2014-11-02 23:10:32 +03: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
|
|
|
|
2015-08-17 21:09:47 +03:00
|
|
|
#ifndef __cplusplus
|
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
|
2015-08-17 21:09:47 +03:00
|
|
|
#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
|
|
|
|
2016-03-18 00:55:12 +03:00
|
|
|
/* Windows does not have sizet formating codes or POSIX mkdir so work
|
|
|
|
* around that
|
|
|
|
*/
|
2012-08-13 20:00:43 +04:00
|
|
|
#if defined(_WIN32)
|
2016-03-18 00:55:12 +03:00
|
|
|
/** windows printf formatting for size_t type */
|
|
|
|
#define PRIsizet "Iu"
|
|
|
|
/** windows printf formatting for ssize_t type */
|
|
|
|
#define PRIssizet "Id"
|
|
|
|
/** windows mkdir function */
|
2014-04-28 20:36:51 +04:00
|
|
|
#define nsmkdir(dir, mode) mkdir((dir))
|
2012-08-13 20:00:43 +04:00
|
|
|
#else
|
2016-03-18 00:55:12 +03:00
|
|
|
/** c99 standard printf formatting for size_t type */
|
|
|
|
#define PRIsizet "zu"
|
|
|
|
/** c99 standard printf formatting for ssize_t type */
|
|
|
|
#define PRIssizet "zd"
|
|
|
|
/** POSIX mkdir function */
|
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.
|
|
|
|
*
|
2014-05-12 00:31:14 +04:00
|
|
|
* \param x a constant C string.
|
|
|
|
* \return The length of C string without its terminator.
|
2008-05-25 19:51:30 +04:00
|
|
|
*/
|
|
|
|
#define SLEN(x) (sizeof((x)) - 1)
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
|
2014-05-12 00:31:14 +04:00
|
|
|
/**
|
|
|
|
* Replace consecutive whitespace with a single space.
|
|
|
|
*
|
|
|
|
* @todo determine if squash_whitespace utf-8 safe and that it needs to be
|
|
|
|
*
|
|
|
|
* \param s source string
|
|
|
|
* \return heap allocated result, or NULL on memory exhaustion
|
|
|
|
*/
|
2002-05-22 01:27:29 +04:00
|
|
|
char * squash_whitespace(const char * s);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a string without its underscores
|
2014-11-09 18:28:03 +03:00
|
|
|
*
|
|
|
|
* \param s The string to change.
|
2014-05-12 00:31:14 +04:00
|
|
|
* \param replacespace true to insert a space where there was an underscore
|
2014-11-09 18:28:03 +03:00
|
|
|
* \return The altered string
|
2014-05-12 00:31:14 +04:00
|
|
|
*/
|
2009-12-18 02:55:02 +03:00
|
|
|
char *remove_underscores(const char *s, bool replacespace);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts NUL terminated UTF-8 encoded string s containing zero or more
|
|
|
|
* spaces (char 32) or TABs (char 9) to non-breaking spaces
|
|
|
|
* (0xC2 + 0xA0 in UTF-8 encoding).
|
|
|
|
*
|
|
|
|
* Caller needs to free() result. Returns NULL in case of error. No
|
|
|
|
* checking is done on validness of the UTF-8 input string.
|
|
|
|
*/
|
2004-07-06 00:19:52 +04:00
|
|
|
char *cnv_space2nbsp(const char *s);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a directory exists.
|
|
|
|
*/
|
2003-11-08 02:47:55 +03:00
|
|
|
bool is_dir(const char *path);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a regular expression, handling errors.
|
|
|
|
*
|
|
|
|
* Parameters as for regcomp(), see man regex.
|
|
|
|
*/
|
2014-10-26 01:06:18 +03:00
|
|
|
nserror regcomp_wrapper(regex_t *preg, const char *regex, int cflags);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a human redable representation of a size in bytes.
|
|
|
|
*
|
|
|
|
* Does a simple conversion which assumes the user speaks English.
|
|
|
|
* The buffer returned is one of three static ones so may change each
|
|
|
|
* time this call is made. Don't store the buffer for later use.
|
|
|
|
* It's done this way for convenience and to fight possible memory
|
|
|
|
* leaks, it is not necessarily pretty.
|
|
|
|
*
|
|
|
|
* @todo This implementation is strange doe sit need
|
|
|
|
* reconsidering?
|
|
|
|
*
|
2014-11-09 18:28:03 +03:00
|
|
|
* @param bytesize The size in bytes.
|
2014-05-12 00:31:14 +04:00
|
|
|
* @return A human readable string representing the size.
|
|
|
|
*/
|
2004-04-25 15:40:05 +04:00
|
|
|
char *human_friendly_bytesize(unsigned long bytesize);
|
2014-05-12 00:31:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a number of centiseconds, that increases in real time, for the
|
|
|
|
* purposes of measuring how long something takes in wall-clock terms.
|
|
|
|
*
|
|
|
|
* The implementation uses gettimeofday() for this. Should the call
|
|
|
|
* to gettimeofday() fail, it returns zero.
|
|
|
|
*
|
|
|
|
* \return number of centiseconds that increases monotonically
|
|
|
|
*/
|
2007-01-30 18:32:31 +03:00
|
|
|
unsigned int wallclock(void);
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2014-05-04 21:29:53 +04:00
|
|
|
/**
|
|
|
|
* Generate a string from one or more component elemnts separated with
|
|
|
|
* a single value.
|
|
|
|
*
|
|
|
|
* This is similar in intent to the perl join function creating a
|
|
|
|
* single delimited string from an array of several.
|
|
|
|
*
|
|
|
|
* @note If a string is allocated it must be freed by the caller.
|
|
|
|
*
|
|
|
|
* @param[in,out] str pointer to string pointer if this is NULL enough
|
|
|
|
* storage will be allocated for the complete path.
|
|
|
|
* @param[in,out] size The size of the space available if \a str not
|
|
|
|
* NULL on input and if not NULL set to the total
|
|
|
|
* output length on output.
|
|
|
|
* @param[in] sep The character to separete the elemnts with.
|
2014-11-09 18:28:03 +03:00
|
|
|
* @param[in] nelm The number of elements up to a maximum of 16.
|
2014-05-04 21:29:53 +04:00
|
|
|
* @param[in] ap The elements of the path as string pointers.
|
|
|
|
* @return NSERROR_OK and the complete path is written to str or error
|
|
|
|
* code on faliure.
|
|
|
|
*/
|
|
|
|
nserror vsnstrjoin(char **str, size_t *size, char sep, size_t nelm, va_list ap);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a string from one or more component elemnts separated with
|
|
|
|
* a single value.
|
|
|
|
*
|
|
|
|
* This is similar in intent to the perl join function creating a
|
|
|
|
* single delimited string from an array of several.
|
|
|
|
*
|
|
|
|
* @note If a string is allocated it must be freed by the caller.
|
|
|
|
*
|
|
|
|
* @param[in,out] str pointer to string pointer if this is NULL enough
|
|
|
|
* storage will be allocated for the complete path.
|
|
|
|
* @param[in,out] size The size of the space available if \a str not
|
|
|
|
* NULL on input and if not NULL set to the total
|
|
|
|
* output length on output.
|
|
|
|
* @param[in] sep The character to separete the elemnts with.
|
2014-11-09 18:28:03 +03:00
|
|
|
* @param[in] nelm The number of elements up to a maximum of 16.
|
2014-05-04 21:29:53 +04:00
|
|
|
* @param[in] ... The elements of the path as string pointers.
|
|
|
|
* @return NSERROR_OK and the complete path is written to str or error
|
|
|
|
* code on faliure.
|
|
|
|
*/
|
|
|
|
nserror snstrjoin(char **str, size_t *size, char sep, size_t nelm, ...);
|
2013-11-15 01:01:51 +04:00
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
/* Platform specific functions */
|
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
|
|
|
|
2002-08-12 03:04:02 +04:00
|
|
|
#endif
|