common_func.h 6.57 KB
Newer Older
1
/* common_func.h - commonly used functions */
2 3 4
#ifndef COMMON_FUNC_H
#define COMMON_FUNC_H

5
/* internationalization support via gettext/libintl */
6
#ifdef USE_GETTEXT
7 8
# include <libintl.h>
# define _(str) gettext(str)
9 10
# define TEXT_DOMAIN "rhash"
# ifndef LOCALEDIR
11
#  define LOCALEDIR "/usr/share/locale"
12
# endif /* LOCALEDIR */
13 14
#else
# define _(str) (str)
15
#endif /* USE_GETTEXT */
16

17 18 19
#include <stdint.h>
#include <stdio.h>
#include <time.h> /* for time_t */
20
#include <stddef.h> /* for wchar_t */
21

22 23
#if !defined( _WIN32) && !defined(__CYGWIN__)
# include <sys/time.h> /* for timeval */
24
#elif _MSC_VER > 1300
25
# include "platform.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#endif


#ifdef __cplusplus
extern "C" {
#endif

/* string function */
void sprintI64(char *dst, uint64_t number, int max_width);
int  int_len(uint64_t num);

int  urlencode(char *dst, const char *name);
int  is_binary_string(const char* str);
char* str_tolower(const char* str);
char* str_trim(char* str);
char* str_set(char* buf, int ch, int size);
42
char* str_append(const char* orig, const char* append);
43 44 45 46
size_t strlen_utf8_c(const char *str);

#define IS_COMMENT(c) ((c) == ';' || (c) == '#')

47 48 49
#ifdef _WIN32
typedef wchar_t rsh_tchar;
# define RSH_T(str) L##str
50
# define t2c(tstr) (w2c(tstr))
51 52 53
#else
typedef  char rsh_tchar;
# define RSH_T(str) str
54
# define t2c(tstr) (tstr)
55
#endif /* _WIN32 */
56 57
typedef rsh_tchar* tstr_t;
typedef const rsh_tchar* ctstr_t;
58

59 60 61
#ifdef _WIN32
# define IF_WINDOWS(code) code
# define is_utf8() win_is_utf8()
62
# define to_utf8(str) str_to_utf8(str)
63 64 65 66 67 68 69
#else /* non _WIN32 part */
# define IF_WINDOWS(code)
/* stub for utf8 */
# define is_utf8() 1
# define to_utf8(str) NULL
#endif /* _WIN32 */

70 71 72 73
/* version information */
const char* get_version_string(void);
const char* get_bt_program_name(void);

74

75
#ifdef _WIN32
76 77 78
# define rsh_fprintf win_fprintf
# define rsh_vfprintf win_vfprintf
# define rsh_fwrite win_fwrite
79
#else
80 81 82
# define rsh_fprintf fprintf
# define rsh_vfprintf vfprintf
# define rsh_fwrite fwrite
83 84
#endif

85

86 87 88
/* time data and functions */

/* portable timer definition */
89
#if defined( _WIN32) || defined(__CYGWIN__)
90
typedef unsigned long long timedelta_t;
91
#else
92 93
#include <sys/time.h> /* for timeval */
typedef struct timeval timedelta_t;
94 95
#endif

96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
 * Start a timer.
 *
 * @param timer timer to start
 */
void rsh_timer_start(timedelta_t* timer);

/**
 * Stop given timer.
 *
 * @param timer the timer to stop
 * @return number of seconds timed
 */
double rsh_timer_stop(timedelta_t* timer);
110

111 112
/**
 * Return ticks in milliseconds for time intervals measurement.
113 114
 * This function should be optimized for speed and retrieve
 * internal clock value, if possible.
115 116 117
 *
 * @return ticks count in milliseconds
 */
118
unsigned rhash_get_ticks(void);
119

120 121 122 123 124
/* program exit handlers */
typedef void (*exit_handler_t)(void);
void rsh_install_exit_handler(exit_handler_t handler);
void rsh_remove_exit_handler(void);
void rsh_exit(int code);
125 126 127 128 129 130 131 132 133 134 135

/* clever malloc with error detection */
#define rsh_malloc(size) rhash_malloc(size, __FILE__, __LINE__)
#define rsh_calloc(num, size) rhash_calloc(num, size, __FILE__, __LINE__)
#define rsh_strdup(str)  rhash_strdup(str,  __FILE__, __LINE__)
#define rsh_realloc(mem, size) rhash_realloc(mem, size, __FILE__, __LINE__)
void* rhash_malloc(size_t size, const char* srcfile, int srcline);
void* rhash_calloc(size_t num, size_t size, const char* srcfile, int srcline);
char* rhash_strdup(const char* str, const char* srcfile, int srcline);
void* rhash_realloc(void* mem, size_t size, const char* srcfile, int srcline);

136 137
#ifdef _WIN32
#define rsh_wcsdup(str) rhash_wcsdup(str, __FILE__, __LINE__)
138
wchar_t* rhash_wcsdup(const wchar_t* str, const char* srcfile, int srcline);
139 140
#endif

141 142 143 144 145 146 147 148 149 150 151
extern void (*rsh_report_error)(const char* srcfile, int srcline, const char* format, ...);

/* vector functions */
typedef struct vector_t
{
	void **array;
	size_t size;
	size_t allocated;
	void (*destructor)(void*);
} vector_t;

152 153 154 155 156 157
vector_t* rsh_vector_new(void (*destructor)(void*));
vector_t* rsh_vector_new_simple(void);
void rsh_vector_free(vector_t* vect);
void rsh_vector_destroy(vector_t* vect);
void rsh_vector_add_ptr(vector_t* vect, void *item);
void rsh_vector_add_empty(vector_t* vect, size_t item_size);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#define rsh_vector_add_uint32(vect, item) { \
	rsh_vector_add_empty(vect, item_size); \
	((unsigned*)(vect)->array)[(vect)->size - 1] = item; \
}
#define rsh_vector_add_item(vect, item, item_size) { \
	rsh_vector_add_empty(vect, item_size); \
	memcpy(((char*)(vect)->array) + item_size * ((vect)->size - 1), item, item_size); \
}

/* a vector pattern implementation, allocating elements by blocks */
typedef struct blocks_vector_t
{
	size_t size;
	vector_t blocks;
} blocks_vector_t;

174 175
void rsh_blocks_vector_init(blocks_vector_t*);
void rsh_blocks_vector_destroy(blocks_vector_t* vect);
176 177 178 179 180
#define rsh_blocks_vector_get_item(bvector, index, blocksize, item_type) \
	(&((item_type*)((bvector)->blocks.array[(index) / (blocksize)]))[(index) % (blocksize)])
#define rsh_blocks_vector_get_ptr(bvector, index, blocksize, item_size) \
	(&((unsigned char*)((bvector)->blocks.array[(index) / (blocksize)]))[(item_size) * ((index) % (blocksize))])
#define rsh_blocks_vector_add(bvector, item, blocksize, item_size) { \
181
	if (((bvector)->size % (blocksize)) == 0) \
182 183 184 185 186
	rsh_vector_add_ptr(&((bvector)->blocks), rsh_malloc((item_size) * (blocksize))); \
	memcpy(rsh_blocks_vector_get_ptr((bvector), (bvector)->size, (blocksize), (item_size)), (item), (item_size)); \
	(bvector)->size++; \
}
#define rsh_blocks_vector_add_ptr(bvector, ptr, blocksize) { \
187
	if (((bvector)->size % (blocksize)) == 0) \
188 189 190 191 192
	rsh_vector_add_ptr(&((bvector)->blocks), rsh_malloc(sizeof(void*) * (blocksize))); \
	((void***)(bvector)->blocks.array)[(bvector)->size / (blocksize)][(bvector)->size % (blocksize)] = (void*)ptr; \
	(bvector)->size++; \
}
#define rsh_blocks_vector_add_empty(bvector, blocksize, item_size) { \
193
	if ( (((bvector)->size++) % (blocksize)) == 0) \
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	rsh_vector_add_ptr(&((bvector)->blocks), rsh_malloc((item_size) * (blocksize))); \
}

/* string buffer functions */
typedef struct strbuf_t
{
	char* str;
	size_t allocated;
	size_t len;
} strbuf_t;

strbuf_t* rsh_str_new(void);
void rsh_str_free(strbuf_t* buf);
void rsh_str_ensure_size(strbuf_t *str, size_t new_size);
void rsh_str_append_n(strbuf_t *str, const char* text, size_t len);
void rsh_str_append(strbuf_t *str, const char* text);

#define rsh_str_ensure_length(str, len) \
212
	if ((size_t)(len) >= (size_t)(str)->allocated) rsh_str_ensure_size((str), (len) + 1);
213
#define rsh_wstr_ensure_length(str, len) \
214
	if ((size_t)((len) + sizeof(wchar_t)) > (size_t)(str)->allocated) rsh_str_ensure_size((str), (len) + sizeof(wchar_t));
215

216 217 218 219 220
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif /* COMMON_FUNC_H */