summaryrefslogtreecommitdiff
path: root/dlib/dlib.h
blob: 0d194dbe61b71e5b512a32524ad1eb9992a04f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef __DLIB_H__
#define __DLIB_H__

#include <stdio.h>     /* for FILE*  */
#include <stddef.h>    /* for size_t */
#include <stdarg.h>    /* for va_list */
#include <string.h>    /* for strerror */

#include "d_size.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/*
 *-- Common macros -----------------------------------------------------------
 */
#ifndef FALSE
#define FALSE   (0)
#endif

#ifndef TRUE
#define TRUE    (!FALSE)
#endif

#undef  MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))

#undef  MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))

/* Handle signed char */
#define dIsspace(c) isspace((uchar_t)(c))
#define dIsalnum(c) isalnum((uchar_t)(c))

#define D_ASCII_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c))
#define D_ASCII_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) + 0x20 : (c))
/*
 *-- Casts -------------------------------------------------------------------
 */
/* TODO: include a void* size test in configure.in */
/* (long) works for both 32bit and 64bit */
#define VOIDP2INT(p)    ((long)(p))
#define INT2VOIDP(i)    ((void*)((long)(i)))

/*
 *-- Memory -------------------------------------------------------------------
 */
#define dNew(type, count) \
   ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
#define dNew0(type, count) \
   ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))

void *dMalloc (size_t size);
void *dRealloc (void *mem, size_t size);
void *dMalloc0 (size_t size);
void dFree (void *mem);

/*
 *- Debug macros --------------------------------------------------------------
 */
#define D_STMT_START      do
#define D_STMT_END        while (0)
#define dReturn_if(expr)               \
   D_STMT_START{                       \
      if (expr) { return; };           \
   }D_STMT_END
#define dReturn_val_if(expr,val)       \
   D_STMT_START{                       \
      if (expr) { return val; };       \
   }D_STMT_END
#define dReturn_if_fail(expr)          \
   D_STMT_START{                       \
      if (!(expr)) { return; };        \
   }D_STMT_END
#define dReturn_val_if_fail(expr,val)  \
   D_STMT_START{                       \
      if (!(expr)) { return val; };    \
   }D_STMT_END

/*
 *- C strings -----------------------------------------------------------------
 */
char *dStrdup(const char *s);
char *dStrndup(const char *s, size_t sz);
char *dStrconcat(const char *s1, ...);
char *dStrstrip(char *s);
char *dStrnfill(size_t len, char c);
char *dStrsep(char **orig, const char *delim);
void dStrshred(char *s);
char *dStriAsciiStr(const char *haystack, const char *needle);
int dStrAsciiCasecmp(const char *s1, const char *s2);
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);

#define dStrerror strerror

/*
 *-- dStr ---------------------------------------------------------------------
 */
#define Dstr_char_t    char

typedef struct _dstr {
   int sz;          /* allocated size (private) */
   int len;
   Dstr_char_t *str;
} Dstr;

Dstr *dStr_new (const char *s);
Dstr *dStr_sized_new (int sz);
void dStr_fit (Dstr *ds);
void dStr_free (Dstr *ds, int all);
void dStr_append_c (Dstr *ds, int c);
void dStr_append (Dstr *ds, const char *s);
void dStr_append_l (Dstr *ds, const char *s, int l);
void dStr_insert (Dstr *ds, int pos_0, const char *s);
void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
void dStr_truncate (Dstr *ds, int len);
void dStr_shred (Dstr *ds);
void dStr_erase (Dstr *ds, int pos_0, int len);
void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
void dStr_sprintf (Dstr *ds, const char *format, ...);
void dStr_sprintfa (Dstr *ds, const char *format, ...);
int  dStr_cmp(Dstr *ds1, Dstr *ds2);
char *dStr_memmem(Dstr *haystack, Dstr *needle);
const char *dStr_printable(Dstr *in, int maxlen);

/*
 *-- dList --------------------------------------------------------------------
 */
struct Dlist_ {
   int sz;          /* allocated size (private) */
   int len;
   void **list;
};

typedef struct Dlist_ Dlist;

/* dCompareFunc:
 * Return: 0 if parameters are equal (for dList_find_custom).
 * Return: 0 if equal,  < 0 if (a < b),  > 0 if (b < a) --for insert sorted.
 *
 * For finding a data node with an external key, the comparison function
 * parameters are: first the data node, and then the key.
 */
typedef int (*dCompareFunc) (const void *a, const void *b);


Dlist *dList_new(int size);
void dList_free (Dlist *lp);
void dList_append (Dlist *lp, void *data);
void dList_prepend (Dlist *lp, void *data);
void dList_insert_pos (Dlist *lp, void *data, int pos0);
int  dList_length (Dlist *lp);
void dList_remove (Dlist *lp, const void *data);
void dList_remove_fast (Dlist *lp, const void *data);
void *dList_nth_data (Dlist *lp, int n0);
void *dList_find (Dlist *lp, const void *data);
int  dList_find_idx (Dlist *lp, const void *data);
void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
void dList_sort (Dlist *lp, dCompareFunc func);
void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);

/*
 *- Parse function ------------------------------------------------------------
 */
int dParser_parse_rc_line(char **line, char **name, char **value);

/*
 *- Dlib messages -------------------------------------------------------------
 */
void dLib_show_messages(bool_t show);

/*
 *- Misc utility functions ----------------------------------------------------
 */
char *dGetcwd();
char *dGethomedir();
char *dGetline(FILE *stream);
int dClose(int fd);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __DLIB_H__ */