annotate dlib/dlib.h @ 2762:8a508291ac35

Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
author Jorge Arellano Cid <jcid@dillo.org>
date Wed, 09 Jan 2013 12:47:23 -0300
parents 29c53b9ebe92
children fb399dcc5866
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 #ifndef __DLIB_H__
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 #define __DLIB_H__
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 #include <stdio.h> /* for FILE* */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 #include <stddef.h> /* for size_t */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 #include <stdarg.h> /* for va_list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 #include <string.h> /* for strerror */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
9 #include "d_size.h"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 #ifdef __cplusplus
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 extern "C" {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #endif /* __cplusplus */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 *-- Common macros -----------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #ifndef FALSE
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #define FALSE (0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #ifndef TRUE
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #define TRUE (!FALSE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #undef MAX
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 #undef MIN
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
32 /* Handle signed char */
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
33 #define dIsspace(c) isspace((uchar_t)(c))
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
34 #define dIsalnum(c) isalnum((uchar_t)(c))
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
35
2332
29c53b9ebe92 locale-independent ASCII character case handling
corvid <corvid@lavabit.com>
parents: 2291
diff changeset
36 #define D_ASCII_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c))
29c53b9ebe92 locale-independent ASCII character case handling
corvid <corvid@lavabit.com>
parents: 2291
diff changeset
37 #define D_ASCII_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) + 0x20 : (c))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 *-- Casts -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 /* TODO: include a void* size test in configure.in */
4
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
42 /* (long) works for both 32bit and 64bit */
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
43 #define VOIDP2INT(p) ((long)(p))
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 167
diff changeset
44 #define INT2VOIDP(i) ((void*)((long)(i)))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 *-- Memory -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #define dNew(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #define dNew0(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 void *dMalloc (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 void *dRealloc (void *mem, size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 void *dMalloc0 (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 void dFree (void *mem);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 *- Debug macros --------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 #define D_STMT_START do
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 #define D_STMT_END while (0)
786
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
64 #define dReturn_if(expr) \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
65 D_STMT_START{ \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
66 if (expr) { return; }; \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
67 }D_STMT_END
1106
5bab6f4dc3cc dReturn_val_if arg
corvid <corvid@lavabit.com>
parents: 1104
diff changeset
68 #define dReturn_val_if(expr,val) \
786
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
69 D_STMT_START{ \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
70 if (expr) { return val; }; \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
71 }D_STMT_END
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 #define dReturn_if_fail(expr) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 if (!(expr)) { return; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 #define dReturn_val_if_fail(expr,val) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 if (!(expr)) { return val; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 *- C strings -----------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 char *dStrdup(const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 char *dStrndup(const char *s, size_t sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 char *dStrconcat(const char *s1, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 char *dStrstrip(char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 char *dStrnfill(size_t len, char c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 char *dStrsep(char **orig, const char *delim);
2291
2af950507af5 password shredding
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
90 void dStrshred(char *s);
2332
29c53b9ebe92 locale-independent ASCII character case handling
corvid <corvid@lavabit.com>
parents: 2291
diff changeset
91 char *dStriAsciiStr(const char *haystack, const char *needle);
29c53b9ebe92 locale-independent ASCII character case handling
corvid <corvid@lavabit.com>
parents: 2291
diff changeset
92 int dStrAsciiCasecmp(const char *s1, const char *s2);
29c53b9ebe92 locale-independent ASCII character case handling
corvid <corvid@lavabit.com>
parents: 2291
diff changeset
93 int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 #define dStrerror strerror
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 *-- dStr ---------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 #define Dstr_char_t char
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 typedef struct _dstr {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 Dstr_char_t *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 } Dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 Dstr *dStr_new (const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 Dstr *dStr_sized_new (int sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 void dStr_fit (Dstr *ds);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 void dStr_free (Dstr *ds, int all);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 void dStr_append_c (Dstr *ds, int c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 void dStr_append (Dstr *ds, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 void dStr_append_l (Dstr *ds, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 void dStr_insert (Dstr *ds, int pos_0, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 void dStr_truncate (Dstr *ds, int len);
2291
2af950507af5 password shredding
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
118 void dStr_shred (Dstr *ds);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 void dStr_erase (Dstr *ds, int pos_0, int len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 void dStr_sprintf (Dstr *ds, const char *format, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 void dStr_sprintfa (Dstr *ds, const char *format, ...);
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
124 int dStr_cmp(Dstr *ds1, Dstr *ds2);
163
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 162
diff changeset
125 char *dStr_memmem(Dstr *haystack, Dstr *needle);
167
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
126 const char *dStr_printable(Dstr *in, int maxlen);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 *-- dList --------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 struct Dlist_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 void **list;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 typedef struct Dlist_ Dlist;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 /* dCompareFunc:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 * Return: 0 if parameters are equal (for dList_find_custom).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 * For finding a data node with an external key, the comparison function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 * parameters are: first the data node, and then the key.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 typedef int (*dCompareFunc) (const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 Dlist *dList_new(int size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 void dList_free (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 void dList_append (Dlist *lp, void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 void dList_prepend (Dlist *lp, void *data);
162
7a0dad1cffe0 - Fixed a SEGFAULT bug in http.c (handling of web->url).
jcid
parents: 157
diff changeset
153 void dList_insert_pos (Dlist *lp, void *data, int pos0);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 int dList_length (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 void dList_remove (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 void dList_remove_fast (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 void *dList_nth_data (Dlist *lp, int n0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 void *dList_find (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 int dList_find_idx (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 void dList_sort (Dlist *lp, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 /*
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
166 *- Parse function ------------------------------------------------------------
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
167 */
1053
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 786
diff changeset
168 int dParser_parse_rc_line(char **line, char **name, char **value);
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
169
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
170 /*
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
171 *- Dlib messages -------------------------------------------------------------
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
172 */
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
173 void dLib_show_messages(bool_t show);
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
174
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
175 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 *- Misc utility functions ----------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 */
2762
8a508291ac35 Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
Jorge Arellano Cid <jcid@dillo.org>
parents: 2332
diff changeset
178 char *dGetcwd();
8a508291ac35 Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
Jorge Arellano Cid <jcid@dillo.org>
parents: 2332
diff changeset
179 char *dGethomedir();
8a508291ac35 Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
Jorge Arellano Cid <jcid@dillo.org>
parents: 2332
diff changeset
180 char *dGetline(FILE *stream);
8a508291ac35 Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
Jorge Arellano Cid <jcid@dillo.org>
parents: 2332
diff changeset
181 int dClose(int fd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 #ifdef __cplusplus
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 #endif /* __cplusplus */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 #endif /* __DLIB_H__ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188