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 
9 #include "d_size.h"
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif /* __cplusplus */
14 
15 /*
16  *-- Common macros -----------------------------------------------------------
17  */
18 #ifndef FALSE
19 #define FALSE   (0)
20 #endif
21 
22 #ifndef TRUE
23 #define TRUE    (!FALSE)
24 #endif
25 
26 #undef  MAX
27 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
28 
29 #undef  MIN
30 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
31 
32 /* Handle signed char */
33 #define dIsspace(c) isspace((uchar_t)(c))
34 #define dIsalnum(c) isalnum((uchar_t)(c))
35 
36 #define D_ASCII_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c))
37 #define D_ASCII_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) + 0x20 : (c))
38 /*
39  *-- Casts -------------------------------------------------------------------
40  */
41 /* TODO: include a void* size test in configure.in */
42 /* (long) works for both 32bit and 64bit */
43 #define VOIDP2INT(p)    ((long)(p))
44 #define INT2VOIDP(i)    ((void*)((long)(i)))
45 
46 /*
47  *-- Memory -------------------------------------------------------------------
48  */
49 #define dNew(type, count) \
50    ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
51 #define dNew0(type, count) \
52    ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
53 
54 void *dMalloc (size_t size);
55 void *dRealloc (void *mem, size_t size);
56 void *dMalloc0 (size_t size);
57 void dFree (void *mem);
58 
59 /*
60  *- Debug macros --------------------------------------------------------------
61  */
62 #define D_STMT_START      do
63 #define D_STMT_END        while (0)
64 #define dReturn_if(expr)               \
65    D_STMT_START{                       \
66       if (expr) { return; };           \
67    }D_STMT_END
68 #define dReturn_val_if(expr,val)       \
69    D_STMT_START{                       \
70       if (expr) { return val; };       \
71    }D_STMT_END
72 #define dReturn_if_fail(expr)          \
73    D_STMT_START{                       \
74       if (!(expr)) { return; };        \
75    }D_STMT_END
76 #define dReturn_val_if_fail(expr,val)  \
77    D_STMT_START{                       \
78       if (!(expr)) { return val; };    \
79    }D_STMT_END
80 
81 /*
82  *- C strings -----------------------------------------------------------------
83  */
84 char *dStrdup(const char *s);
85 char *dStrndup(const char *s, size_t sz);
86 char *dStrconcat(const char *s1, ...);
87 char *dStrstrip(char *s);
88 char *dStrnfill(size_t len, char c);
89 char *dStrsep(char **orig, const char *delim);
90 void dStrshred(char *s);
91 char *dStriAsciiStr(const char *haystack, const char *needle);
92 int dStrAsciiCasecmp(const char *s1, const char *s2);
93 int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);
94 
95 #define dStrerror strerror
96 
97 /*
98  *-- dStr ---------------------------------------------------------------------
99  */
100 #define Dstr_char_t    char
101 
102 typedef struct {
103    int sz;          /* allocated size (private) */
104    int len;
105    Dstr_char_t *str;
106 } Dstr;
107 
108 Dstr *dStr_new (const char *s);
109 Dstr *dStr_sized_new (int sz);
110 void dStr_fit (Dstr *ds);
111 void dStr_free (Dstr *ds, int all);
112 void dStr_append_c (Dstr *ds, int c);
113 void dStr_append (Dstr *ds, const char *s);
114 void dStr_append_l (Dstr *ds, const char *s, int l);
115 void dStr_insert (Dstr *ds, int pos_0, const char *s);
116 void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
117 void dStr_truncate (Dstr *ds, int len);
118 void dStr_shred (Dstr *ds);
119 void dStr_erase (Dstr *ds, int pos_0, int len);
120 void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
121 void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
122 void dStr_sprintf (Dstr *ds, const char *format, ...);
123 void dStr_sprintfa (Dstr *ds, const char *format, ...);
124 int  dStr_cmp(Dstr *ds1, Dstr *ds2);
125 char *dStr_memmem(Dstr *haystack, Dstr *needle);
126 const char *dStr_printable(Dstr *in, int maxlen);
127 
128 /*
129  *-- dList --------------------------------------------------------------------
130  */
131 typedef struct {
132    int sz;          /* allocated size (private) */
133    int len;
134    void **list;
135 } 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  */
144 typedef int (*dCompareFunc) (const void *a, const void *b);
145 
146 
147 Dlist *dList_new(int size);
148 void dList_free (Dlist *lp);
149 void dList_append (Dlist *lp, void *data);
150 void dList_prepend (Dlist *lp, void *data);
151 void dList_insert_pos (Dlist *lp, void *data, int pos0);
152 int  dList_length (Dlist *lp);
153 void dList_remove (Dlist *lp, const void *data);
154 void dList_remove_fast (Dlist *lp, const void *data);
155 void *dList_nth_data (Dlist *lp, int n0);
156 void *dList_find (Dlist *lp, const void *data);
157 int  dList_find_idx (Dlist *lp, const void *data);
158 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
159 void dList_sort (Dlist *lp, dCompareFunc func);
160 void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
161 void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
162 
163 /*
164  *- Parse function ------------------------------------------------------------
165  */
166 int dParser_parse_rc_line(char **line, char **name, char **value);
167 
168 /*
169  *- Dlib messages -------------------------------------------------------------
170  */
171 void dLib_show_messages(bool_t show);
172 
173 /*
174  *- Misc utility functions ----------------------------------------------------
175  */
176 char *dGetcwd();
177 char *dGethomedir();
178 char *dGetline(FILE *stream);
179 int dClose(int fd);
180 
181 #ifdef __cplusplus
182 }
183 #endif /* __cplusplus */
184 
185 #endif /* __DLIB_H__ */
186 
187