Dillo v3.2.0-143-gabad1053
Loading...
Searching...
No Matches
dlib.h
Go to the documentation of this file.
1/*
2 * File: dlib.h
3 *
4 * Copyright (C) 2006-2007 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright (C) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef __DLIB_H__
14#define __DLIB_H__
15
16#include <stdio.h> /* for FILE* */
17#include <stddef.h> /* for size_t */
18#include <stdarg.h> /* for va_list */
19#include <string.h> /* for strerror */
20
21#include "d_size.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif /* __cplusplus */
26
27/*
28 *-- Common macros -----------------------------------------------------------
29 */
30#ifndef FALSE
31#define FALSE (0)
32#endif
33
34#ifndef TRUE
35#define TRUE (!FALSE)
36#endif
37
38#undef MAX
39#define MAX(a, b) (((a) > (b)) ? (a) : (b))
40
41#undef MIN
42#define MIN(a, b) (((a) < (b)) ? (a) : (b))
43
44/* Handle signed char */
45#define dIsspace(c) isspace((uchar_t)(c))
46#define dIsalnum(c) isalnum((uchar_t)(c))
47
48#define D_ASCII_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c))
49#define D_ASCII_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) + 0x20 : (c))
50
51/* Use 4 K of default buffer size if not defined */
52#ifndef BUFSIZ
53#define BUFSIZ 4096
54#endif
55
56/*
57 *-- Casts -------------------------------------------------------------------
58 */
59/* TODO: include a void* size test in configure.in */
60/* (long) works for both 32bit and 64bit */
61#define VOIDP2INT(p) ((long)(p))
62#define INT2VOIDP(i) ((void*)((long)(i)))
63
64/*
65 *-- Memory -------------------------------------------------------------------
66 */
67#define dNew(type, count) \
68 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
69#define dNew0(type, count) \
70 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
71
72void *dMalloc (size_t size);
73void *dRealloc (void *mem, size_t size);
74void *dMalloc0 (size_t size);
75void dFree (void *mem);
76
77/*
78 *- Debug macros --------------------------------------------------------------
79 */
80#define D_STMT_START do
81#define D_STMT_END while (0)
82#define dReturn_if(expr) \
83 D_STMT_START{ \
84 if (expr) { return; }; \
85 }D_STMT_END
86#define dReturn_val_if(expr,val) \
87 D_STMT_START{ \
88 if (expr) { return val; }; \
89 }D_STMT_END
90#define dReturn_if_fail(expr) \
91 D_STMT_START{ \
92 if (!(expr)) { return; }; \
93 }D_STMT_END
94#define dReturn_val_if_fail(expr,val) \
95 D_STMT_START{ \
96 if (!(expr)) { return val; }; \
97 }D_STMT_END
98
99/*
100 *- C strings -----------------------------------------------------------------
101 */
102char *dStrdup(const char *s);
103char *dStrndup(const char *s, size_t sz);
104char *dStrconcat(const char *s1, ...);
105char *dStrstrip(char *s);
106char *dStrnfill(size_t len, char c);
107char *dStrsep(char **orig, const char *delim);
108void dStrshred(char *s);
109char *dStriAsciiStr(const char *haystack, const char *needle);
110int dStrAsciiCasecmp(const char *s1, const char *s2);
111int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);
112
113#define dStrerror strerror
114
115/*
116 *-- dStr ---------------------------------------------------------------------
117 */
118#define Dstr_char_t char
119
120typedef struct {
121 int sz; /* allocated size (private) */
122 int len;
124} Dstr;
125
126Dstr *dStr_new (const char *s);
127Dstr *dStr_sized_new (int sz);
128void dStr_fit (Dstr *ds);
129void dStr_free (Dstr *ds, int all);
130void dStr_append_c (Dstr *ds, int c);
131void dStr_append (Dstr *ds, const char *s);
132void dStr_append_l (Dstr *ds, const char *s, int l);
133void dStr_insert (Dstr *ds, int pos_0, const char *s);
134void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
135void dStr_truncate (Dstr *ds, int len);
136void dStr_shred (Dstr *ds);
137void dStr_erase (Dstr *ds, int pos_0, int len);
138void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
139void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
140void dStr_sprintf (Dstr *ds, const char *format, ...);
141void dStr_sprintfa (Dstr *ds, const char *format, ...);
142int dStr_cmp(Dstr *ds1, Dstr *ds2);
143char *dStr_memmem(Dstr *haystack, Dstr *needle);
144const char *dStr_printable(Dstr *in, int maxlen);
145void dStr_shorten(Dstr *dst, const char *src, int n);
146
147/*
148 *-- dList --------------------------------------------------------------------
149 */
150typedef struct {
151 int sz; /* allocated size (private) */
152 int len;
153 void **list;
154} Dlist;
155
156/* dCompareFunc:
157 * Return: 0 if parameters are equal (for dList_find_custom).
158 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
159 *
160 * For finding a data node with an external key, the comparison function
161 * parameters are: first the data node, and then the key.
162 */
163typedef int (*dCompareFunc) (const void *a, const void *b);
164
165
166Dlist *dList_new(int size);
167void dList_free (Dlist *lp);
168void dList_append (Dlist *lp, void *data);
169void dList_prepend (Dlist *lp, void *data);
170void dList_insert_pos (Dlist *lp, void *data, int pos0);
171int dList_length (Dlist *lp);
172void dList_remove (Dlist *lp, const void *data);
173void dList_remove_fast (Dlist *lp, const void *data);
174void *dList_nth_data (Dlist *lp, int n0);
175void *dList_find (Dlist *lp, const void *data);
176int dList_find_idx (Dlist *lp, const void *data);
177void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
178void dList_sort (Dlist *lp, dCompareFunc func);
179void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
180void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
181
182/*
183 *- Parse function ------------------------------------------------------------
184 */
185int dParser_parse_rc_line(char **line, char **name, char **value);
186
187/*
188 *- Dlib messages -------------------------------------------------------------
189 */
190void dLib_show_messages(bool_t show);
191
192/*
193 *- Misc utility functions ----------------------------------------------------
194 */
195char *dGetcwd(void);
196char *dGethomedir(void);
197char *dGetline(FILE *stream);
198int dClose(int fd);
199int dUsleep(unsigned long us);
200
201#ifdef __cplusplus
202}
203#endif /* __cplusplus */
204
205#endif /* __DLIB_H__ */
206
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:956
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:797
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:904
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:604
char * dStrdup(const char *s)
Definition dlib.c:77
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:576
int(* dCompareFunc)(const void *a, const void *b)
Definition dlib.h:163
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:641
void dStr_shorten(Dstr *dst, const char *src, int n)
Shorten string so it fits in n characters.
Definition dlib.c:551
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:690
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
Definition dlib.c:651
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:118
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:979
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:711
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:786
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:625
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:824
void dList_free(Dlist *lp)
Free a list (not its elements)
Definition dlib.c:592
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:732
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:862
void dList_prepend(Dlist *lp, void *data)
Prepend a data item to the list.
Definition dlib.c:633
int dUsleep(unsigned long us)
Portable usleep() function.
Definition dlib.c:995
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:669
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:934
char * dGetcwd(void)
Return the current working directory in a new string.
Definition dlib.c:916
void * dList_find(Dlist *lp, const void *data)
Return the found data item, or NULL if not present.
Definition dlib.c:700
Definition dlib.h:150
int len
Definition dlib.h:152
int sz
Definition dlib.h:151
void ** list
Definition dlib.h:153
Definition dlib.h:120
Dstr_char_t * str
Definition dlib.h:123
int len
Definition dlib.h:122
int sz
Definition dlib.h:121