Dillo v3.1.1-111-gd4f56d0d
Loading...
Searching...
No Matches
dlib.h
Go to the documentation of this file.
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
12extern "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
54void *dMalloc (size_t size);
55void *dRealloc (void *mem, size_t size);
56void *dMalloc0 (size_t size);
57void 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 */
84char *dStrdup(const char *s);
85char *dStrndup(const char *s, size_t sz);
86char *dStrconcat(const char *s1, ...);
87char *dStrstrip(char *s);
88char *dStrnfill(size_t len, char c);
89char *dStrsep(char **orig, const char *delim);
90void dStrshred(char *s);
91char *dStriAsciiStr(const char *haystack, const char *needle);
92int dStrAsciiCasecmp(const char *s1, const char *s2);
93int 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
102typedef struct {
103 int sz; /* allocated size (private) */
104 int len;
106} Dstr;
107
108Dstr *dStr_new (const char *s);
109Dstr *dStr_sized_new (int sz);
110void dStr_fit (Dstr *ds);
111void dStr_free (Dstr *ds, int all);
112void dStr_append_c (Dstr *ds, int c);
113void dStr_append (Dstr *ds, const char *s);
114void dStr_append_l (Dstr *ds, const char *s, int l);
115void dStr_insert (Dstr *ds, int pos_0, const char *s);
116void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
117void dStr_truncate (Dstr *ds, int len);
118void dStr_shred (Dstr *ds);
119void dStr_erase (Dstr *ds, int pos_0, int len);
120void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
121void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
122void dStr_sprintf (Dstr *ds, const char *format, ...);
123void dStr_sprintfa (Dstr *ds, const char *format, ...);
124int dStr_cmp(Dstr *ds1, Dstr *ds2);
125char *dStr_memmem(Dstr *haystack, Dstr *needle);
126const char *dStr_printable(Dstr *in, int maxlen);
127
128/*
129 *-- dList --------------------------------------------------------------------
130 */
131typedef 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 */
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(void);
177char *dGethomedir(void);
178char *dGetline(FILE *stream);
179int dClose(int fd);
180int dUsleep(unsigned long us);
181
182#ifdef __cplusplus
183}
184#endif /* __cplusplus */
185
186#endif /* __DLIB_H__ */
187
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:928
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:102
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
Definition dlib.c:769
int dStr_cmp(Dstr *ds1, Dstr *ds2)
Compare two dStrs.
Definition dlib.c:478
char * dStrsep(char **orig, const char *delim)
strsep() implementation
Definition dlib.c:159
void * dMalloc0(size_t size)
Definition dlib.c:61
void dFree(void *mem)
Definition dlib.c:68
char * dStr_memmem(Dstr *haystack, Dstr *needle)
Return a pointer to the first occurrence of needle in haystack.
Definition dlib.c:490
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:203
void dStr_sprintfa(Dstr *ds, const char *format,...)
Printf-like function that appends.
Definition dlib.c:464
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
Definition dlib.c:122
void dLib_show_messages(bool_t show)
Definition dlib.c:876
void dStr_append(Dstr *ds, const char *s)
Append a C string to a Dstr.
Definition dlib.c:316
void dList_insert_pos(Dlist *lp, void *data, int pos0)
Insert an element at a given position [0 based].
Definition dlib.c:576
char * dStrdup(const char *s)
Definition dlib.c:77
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:548
int(* dCompareFunc)(const void *a, const void *b)
Definition dlib.h:144
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
Definition dlib.c:254
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
Definition dlib.c:215
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
Definition dlib.c:388
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:613
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:662
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
Definition dlib.c:623
void * dMalloc(size_t size)
Definition dlib.c:45
void dStr_free(Dstr *ds, int all)
Free a dillo string.
Definition dlib.c:337
#define Dstr_char_t
Definition dlib.h:100
void dStrshred(char *s)
Clear the contents of the string.
Definition dlib.c:140
int dClose(int fd)
Close a FD handling EINTR.
Definition dlib.c:951
char * dStriAsciiStr(const char *haystack, const char *needle)
Case insensitive strstr.
Definition dlib.c:184
int dList_find_idx(Dlist *lp, const void *data)
Search a data item.
Definition dlib.c:683
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
Definition dlib.c:308
void dStr_append_c(Dstr *ds, int c)
Append one character.
Definition dlib.c:349
char * dStrndup(const char *s, size_t sz)
Definition dlib.c:88
void dStr_sprintf(Dstr *ds, const char *format,...)
Printf-like function.
Definition dlib.c:450
void dList_sort(Dlist *lp, dCompareFunc func)
Sort the list using a custom function.
Definition dlib.c:758
void dStr_vsprintfa(Dstr *ds, const char *format, va_list argp)
vsprintf-like function that appends.
Definition dlib.c:401
Dstr * dStr_new(const char *s)
Create a new string.
Definition dlib.c:325
void dStr_shred(Dstr *ds)
Clear a Dstr.
Definition dlib.c:379
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
Definition dlib.c:597
void dStr_vsprintf(Dstr *ds, const char *format, va_list argp)
vsprintf-like function.
Definition dlib.c:439
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
Definition dlib.c:796
void dList_free(Dlist *lp)
Free a list (not its elements)
Definition dlib.c:564
void dStr_insert_l(Dstr *ds, int pos_0, const char *s, int l)
Insert a C string, at a given position, into a Dstr (providing length).
Definition dlib.c:278
void * dList_find_custom(Dlist *lp, const void *data, dCompareFunc func)
Search a data item using a custom function.
Definition dlib.c:704
int dParser_parse_rc_line(char **line, char **name, char **value)
Take a dillo rc line and return 'name' and 'value' pointers to it.
Definition dlib.c:834
void dList_prepend(Dlist *lp, void *data)
Prepend a data item to the list.
Definition dlib.c:605
int dUsleep(unsigned long us)
Portable usleep() function.
Definition dlib.c:967
void dStr_fit(Dstr *ds)
Return memory if there's too much allocated.
Definition dlib.c:269
const char * dStr_printable(Dstr *in, int maxlen)
Return a printable representation of the provided Dstr, limited to a length of roughly maxlen.
Definition dlib.c:513
void * dRealloc(void *mem, size_t size)
Definition dlib.c:53
void dStr_insert(Dstr *ds, int pos_0, const char *s)
Insert a C string, at a given position, into a Dstr.
Definition dlib.c:298
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
Definition dlib.c:368
void dList_remove(Dlist *lp, const void *data)
Definition dlib.c:641
char * dStrnfill(size_t len, char c)
Return a new string of length 'len' filled with 'c' characters.
Definition dlib.c:149
char * dGethomedir(void)
Return the home directory in a static string (don't free)
Definition dlib.c:906
char * dGetcwd(void)
Return the current working directory in a new string.
Definition dlib.c:888
void * dList_find(Dlist *lp, const void *data)
Return the found data item, or NULL if not present.
Definition dlib.c:672
Definition dlib.h:131
int len
Definition dlib.h:133
int sz
Definition dlib.h:132
void ** list
Definition dlib.h:134
Definition dlib.h:102
Dstr_char_t * str
Definition dlib.h:105
int len
Definition dlib.h:104
int sz
Definition dlib.h:103