changelog shortlog tags changeset files revisions annotate raw

dlib/dlib.h

changeset 1685: e27f1df9ae80
parent:5bab6f4dc3cc
author: Jeremy Henty, corvid
date: Fri Jul 30 17:34:15 2010 +0000 (37 hours ago)
permissions: -rw-r--r--
description: clearer Capi_filters_allow msg
1#ifndef __DLIB_H__
2#define __DLIB_H__
3
4#include <stdio.h> /* for FILE* */
5#include <stddef.h> /* for size_t */
6#include <stdarg.h> /* for va_list */
7#include <string.h> /* for strerror */
8#include <strings.h> /* for strcasecmp, strncasecmp (POSIX 2001) */
9
10#include "d_size.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif /* __cplusplus */
15
16/*
17 *-- Common macros -----------------------------------------------------------
18 */
19#ifndef FALSE
20#define FALSE (0)
21#endif
22
23#ifndef TRUE
24#define TRUE (!FALSE)
25#endif
26
27#undef MAX
28#define MAX(a, b) (((a) > (b)) ? (a) : (b))
29
30#undef MIN
31#define MIN(a, b) (((a) < (b)) ? (a) : (b))
32
33/* Handle signed char */
34#define dIsspace(c) isspace((uchar_t)(c))
35#define dIsalnum(c) isalnum((uchar_t)(c))
36
37/*
38 *-- Casts -------------------------------------------------------------------
39 */
40/* TODO: include a void* size test in configure.in */
41/* (long) works for both 32bit and 64bit */
42#define VOIDP2INT(p) ((long)(p))
43#define INT2VOIDP(i) ((void*)((long)(i)))
44
45/*
46 *-- Memory -------------------------------------------------------------------
47 */
48#define dNew(type, count) \
49 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
50#define dNew0(type, count) \
51 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
52
53void *dMalloc (size_t size);
54void *dRealloc (void *mem, size_t size);
55void *dMalloc0 (size_t size);
56void dFree (void *mem);
57
58/*
59 *- Debug macros --------------------------------------------------------------
60 */
61#define D_STMT_START do
62#define D_STMT_END while (0)
63#define dReturn_if(expr) \
64 D_STMT_START{ \
65 if (expr) { return; }; \
66 }D_STMT_END
67#define dReturn_val_if(expr,val) \
68 D_STMT_START{ \
69 if (expr) { return val; }; \
70 }D_STMT_END
71#define dReturn_if_fail(expr) \
72 D_STMT_START{ \
73 if (!(expr)) { return; }; \
74 }D_STMT_END
75#define dReturn_val_if_fail(expr,val) \
76 D_STMT_START{ \
77 if (!(expr)) { return val; }; \
78 }D_STMT_END
79
80/*
81 *- C strings -----------------------------------------------------------------
82 */
83char *dStrdup(const char *s);
84char *dStrndup(const char *s, size_t sz);
85char *dStrconcat(const char *s1, ...);
86char *dStrstrip(char *s);
87char *dStrnfill(size_t len, char c);
88char *dStrsep(char **orig, const char *delim);
89char *dStristr(const char *haystack, const char *needle);
90
91/* these are in POSIX 2001. Could be implemented if a port requires it */
92#define dStrcasecmp strcasecmp
93#define dStrncasecmp strncasecmp
94#define dStrerror strerror
95
96/*
97 *-- dStr ---------------------------------------------------------------------
98 */
99#define Dstr_char_t char
100
101typedef struct _dstr {
102 int sz; /* allocated size (private) */
103 int len;
104 Dstr_char_t *str;
105} Dstr;
106
107Dstr *dStr_new (const char *s);
108Dstr *dStr_sized_new (int sz);
109void dStr_fit (Dstr *ds);
110void dStr_free (Dstr *ds, int all);
111void dStr_append_c (Dstr *ds, int c);
112void dStr_append (Dstr *ds, const char *s);
113void dStr_append_l (Dstr *ds, const char *s, int l);
114void dStr_insert (Dstr *ds, int pos_0, const char *s);
115void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
116void dStr_truncate (Dstr *ds, int len);
117void dStr_erase (Dstr *ds, int pos_0, int len);
118void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
119void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
120void dStr_sprintf (Dstr *ds, const char *format, ...);
121void dStr_sprintfa (Dstr *ds, const char *format, ...);
122int dStr_cmp(Dstr *ds1, Dstr *ds2);
123char *dStr_memmem(Dstr *haystack, Dstr *needle);
124const char *dStr_printable(Dstr *in, int maxlen);
125
126/*
127 *-- dList --------------------------------------------------------------------
128 */
129struct Dlist_ {
130 int sz; /* allocated size (private) */
131 int len;
132 void **list;
133};
134
135typedef struct Dlist_ Dlist;
136
137/* dCompareFunc:
138 * Return: 0 if parameters are equal (for dList_find_custom).
139 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
140 *
141 * For finding a data node with an external key, the comparison function
142 * parameters are: first the data node, and then the key.
143 */
144typedef int (*dCompareFunc) (const void *a, const void *b);
145
146
147Dlist *dList_new(int size);
148void dList_free (Dlist *lp);
149void dList_append (Dlist *lp, void *data);
150void dList_prepend (Dlist *lp, void *data);
151void dList_insert_pos (Dlist *lp, void *data, int pos0);
152int dList_length (Dlist *lp);
153void dList_remove (Dlist *lp, const void *data);
154void dList_remove_fast (Dlist *lp, const void *data);
155void *dList_nth_data (Dlist *lp, int n0);
156void *dList_find (Dlist *lp, const void *data);
157int dList_find_idx (Dlist *lp, const void *data);
158void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
159void dList_sort (Dlist *lp, dCompareFunc func);
160void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
161void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
162
163/*
164 *- Parse function ------------------------------------------------------------
165 */
166int dParser_parse_rc_line(char **line, char **name, char **value);
167
168/*
169 *- Dlib messages -------------------------------------------------------------
170 */
171void dLib_show_messages(bool_t show);
172
173/*
174 *- Misc utility functions ----------------------------------------------------
175 */
176char *dGetcwd ();
177char *dGethomedir ();
178char *dGetline (FILE *stream);
179
180
181#ifdef __cplusplus
182}
183#endif /* __cplusplus */
184
185#endif /* __DLIB_H__ */
186