annotate dlib/dlib.h @ 163:9b036ecc40a4

- Added dStr_memmem() to dlib.
author jcid
date Sun, 06 Apr 2008 18:58:30 +0200
parents 7a0dad1cffe0
children 1540ba94d145
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 #include <strings.h> /* for strcasecmp, strncasecmp (POSIX 2001) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9
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
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 *-- Casts -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 /* 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
36 /* (long) works for both 32bit and 64bit */
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
37 #define VOIDP2INT(p) ((long)(p))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 #define INT2VOIDP(i) ((void*)(i))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 *-- Memory -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 #define dNew(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 #define dNew0(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 void *dMalloc (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 void *dRealloc (void *mem, size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 void *dMalloc0 (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 void dFree (void *mem);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 *- Debug macros --------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 #define D_STMT_START do
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 #define D_STMT_END while (0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 #define dReturn_if_fail(expr) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 if (!(expr)) { return; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 #define dReturn_val_if_fail(expr,val) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 if (!(expr)) { return val; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 *- C strings -----------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 char *dStrdup(const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 char *dStrndup(const char *s, size_t sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 char *dStrconcat(const char *s1, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 char *dStrstrip(char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 char *dStrnfill(size_t len, char c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 char *dStrsep(char **orig, const char *delim);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 char *dStristr(const char *haystack, const char *needle);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 /* these are in POSIX 2001. Could be implemented if a port requires it */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 #define dStrcasecmp strcasecmp
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 #define dStrncasecmp strncasecmp
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 #define dStrerror strerror
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 *-- dStr ---------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 #define Dstr_char_t char
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 typedef struct _dstr {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 Dstr_char_t *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 } Dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 Dstr *dStr_new (const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 Dstr *dStr_sized_new (int sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 void dStr_fit (Dstr *ds);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 void dStr_free (Dstr *ds, int all);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 void dStr_append_c (Dstr *ds, int c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 void dStr_append (Dstr *ds, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 void dStr_append_l (Dstr *ds, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 void dStr_insert (Dstr *ds, int pos_0, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 void dStr_truncate (Dstr *ds, int len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 void dStr_erase (Dstr *ds, int pos_0, int len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 void dStr_sprintf (Dstr *ds, const char *format, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 void dStr_sprintfa (Dstr *ds, const char *format, ...);
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
109 int dStr_cmp(Dstr *ds1, Dstr *ds2);
163
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 162
diff changeset
110 char *dStr_memmem(Dstr *haystack, Dstr *needle);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 *-- dList --------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 struct Dlist_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 void **list;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 typedef struct Dlist_ Dlist;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 /* dCompareFunc:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 * Return: 0 if parameters are equal (for dList_find_custom).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 * For finding a data node with an external key, the comparison function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 * parameters are: first the data node, and then the key.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 typedef int (*dCompareFunc) (const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 Dlist *dList_new(int size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 void dList_free (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 void dList_append (Dlist *lp, void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 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
137 void dList_insert_pos (Dlist *lp, void *data, int pos0);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 int dList_length (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 void dList_remove (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 void dList_remove_fast (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 void *dList_nth_data (Dlist *lp, int n0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 void *dList_find (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 int dList_find_idx (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 void dList_sort (Dlist *lp, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 /*
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
150 *- Parse function ------------------------------------------------------------
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
151 */
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
152 int dParser_get_rc_pair(char **line, char **name, char **value);
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
153
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
154 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 *- Misc utility functions ----------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 char *dGetcwd ();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 char *dGethomedir ();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 char *dGetline (FILE *stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 #ifdef __cplusplus
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 #endif /* __cplusplus */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 #endif /* __DLIB_H__ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167