Dillo v3.2.0-151-g90488cbf
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-2026 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 <ctype.h>
17#include <stdio.h> /* for FILE* */
18#include <stddef.h> /* for size_t */
19#include <stdarg.h> /* for va_list */
20#include <string.h> /* for strerror */
21
22#include "d_size.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif /* __cplusplus */
27
28/*
29 *-- Common macros -----------------------------------------------------------
30 */
31#ifndef FALSE
32#define FALSE (0)
33#endif
34
35#ifndef TRUE
36#define TRUE (!FALSE)
37#endif
38
39#undef MAX
40#define MAX(a, b) (((a) > (b)) ? (a) : (b))
41
42#undef MIN
43#define MIN(a, b) (((a) < (b)) ? (a) : (b))
44
45/* Prevent negative char values from being transformed in negative integers.
46 * Instead force the value to be casted first to unsigned char, then to int. */
47static inline int dIsalnum(unsigned char c) { return isalnum(c); }
48static inline int dIsalpha(unsigned char c) { return isalpha(c); }
49static inline int dIscntrl(unsigned char c) { return iscntrl(c); }
50static inline int dIsdigit(unsigned char c) { return isdigit(c); }
51static inline int dIsprint(unsigned char c) { return isprint(c); }
52static inline int dIspunct(unsigned char c) { return ispunct(c); }
53static inline int dIsspace(unsigned char c) { return isspace(c); }
54static inline int dIsxdigit(unsigned char c) { return isxdigit(c); }
55static inline int dTolower(unsigned char c) { return tolower(c); }
56/* Provide portable isascii */
57static inline int dIsascii(unsigned char c) { return (c & ~0x7f) == 0; }
58
59#define D_ASCII_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c))
60#define D_ASCII_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) + 0x20 : (c))
61
62/* Use 4 K of default buffer size if not defined */
63#ifndef BUFSIZ
64#define BUFSIZ 4096
65#endif
66
67/*
68 *-- Casts -------------------------------------------------------------------
69 */
70/* TODO: include a void* size test in configure.in */
71/* (long) works for both 32bit and 64bit */
72#define VOIDP2INT(p) ((long)(p))
73#define INT2VOIDP(i) ((void*)((long)(i)))
74
75/*
76 *-- Memory -------------------------------------------------------------------
77 */
78#define dNew(type, count) \
79 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
80#define dNew0(type, count) \
81 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
82
83void *dMalloc (size_t size);
84void *dRealloc (void *mem, size_t size);
85void *dMalloc0 (size_t size);
86void dFree (void *mem);
87
88/*
89 *- Debug macros --------------------------------------------------------------
90 */
91#define D_STMT_START do
92#define D_STMT_END while (0)
93#define dReturn_if(expr) \
94 D_STMT_START{ \
95 if (expr) { return; }; \
96 }D_STMT_END
97#define dReturn_val_if(expr,val) \
98 D_STMT_START{ \
99 if (expr) { return val; }; \
100 }D_STMT_END
101#define dReturn_if_fail(expr) \
102 D_STMT_START{ \
103 if (!(expr)) { return; }; \
104 }D_STMT_END
105#define dReturn_val_if_fail(expr,val) \
106 D_STMT_START{ \
107 if (!(expr)) { return val; }; \
108 }D_STMT_END
109
110/*
111 *- C strings -----------------------------------------------------------------
112 */
113char *dStrdup(const char *s);
114char *dStrndup(const char *s, size_t sz);
115char *dStrconcat(const char *s1, ...);
116char *dStrstrip(char *s);
117char *dStrnfill(size_t len, char c);
118char *dStrsep(char **orig, const char *delim);
119void dStrshred(char *s);
120char *dStriAsciiStr(const char *haystack, const char *needle);
121int dStrAsciiCasecmp(const char *s1, const char *s2);
122int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);
123
124#define dStrerror strerror
125
126/*
127 *-- dStr ---------------------------------------------------------------------
128 */
129#define Dstr_char_t char
130
131typedef struct {
132 int sz; /* allocated size (private) */
133 int len;
135} Dstr;
136
137Dstr *dStr_new (const char *s);
138Dstr *dStr_sized_new (int sz);
139void dStr_fit (Dstr *ds);
140void dStr_free (Dstr *ds, int all);
141void dStr_append_c (Dstr *ds, int c);
142void dStr_append (Dstr *ds, const char *s);
143void dStr_append_l (Dstr *ds, const char *s, int l);
144void dStr_insert (Dstr *ds, int pos_0, const char *s);
145void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
146void dStr_truncate (Dstr *ds, int len);
147void dStr_shred (Dstr *ds);
148void dStr_erase (Dstr *ds, int pos_0, int len);
149void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
150void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
151void dStr_sprintf (Dstr *ds, const char *format, ...);
152void dStr_sprintfa (Dstr *ds, const char *format, ...);
153int dStr_cmp(Dstr *ds1, Dstr *ds2);
154char *dStr_memmem(Dstr *haystack, Dstr *needle);
155const char *dStr_printable(Dstr *in, int maxlen);
156void dStr_shorten(Dstr *dst, const char *src, int n);
157
158/*
159 *-- dList --------------------------------------------------------------------
160 */
161typedef struct {
162 int sz; /* allocated size (private) */
163 int len;
164 void **list;
165} Dlist;
166
167/* dCompareFunc:
168 * Return: 0 if parameters are equal (for dList_find_custom).
169 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
170 *
171 * For finding a data node with an external key, the comparison function
172 * parameters are: first the data node, and then the key.
173 */
174typedef int (*dCompareFunc) (const void *a, const void *b);
175
176
177Dlist *dList_new(int size);
178void dList_free (Dlist *lp);
179void dList_append (Dlist *lp, void *data);
180void dList_prepend (Dlist *lp, void *data);
181void dList_insert_pos (Dlist *lp, void *data, int pos0);
182int dList_length (Dlist *lp);
183void dList_remove (Dlist *lp, const void *data);
184void dList_remove_fast (Dlist *lp, const void *data);
185void *dList_nth_data (Dlist *lp, int n0);
186void *dList_find (Dlist *lp, const void *data);
187int dList_find_idx (Dlist *lp, const void *data);
188void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
189void dList_sort (Dlist *lp, dCompareFunc func);
190void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
191void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
192
193/*
194 *- Parse function ------------------------------------------------------------
195 */
196int dParser_parse_rc_line(char **line, char **name, char **value);
197
198/*
199 *- Dlib messages -------------------------------------------------------------
200 */
201void dLib_show_messages(bool_t show);
202
203/*
204 *- Misc utility functions ----------------------------------------------------
205 */
206char *dGetcwd(void);
207char *dGethomedir(void);
208char *dGetline(FILE *stream);
209int dClose(int fd);
210int dUsleep(unsigned long us);
211
212#ifdef __cplusplus
213}
214#endif /* __cplusplus */
215
216#endif /* __DLIB_H__ */
217
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:955
static int dIsprint(unsigned char c)
Definition dlib.h:51
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:101
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
Definition dlib.c:796
int dStr_cmp(Dstr *ds1, Dstr *ds2)
Compare two dStrs.
Definition dlib.c:477
char * dStrsep(char **orig, const char *delim)
strsep() implementation
Definition dlib.c:158
void * dMalloc0(size_t size)
Definition dlib.c:60
void dFree(void *mem)
Definition dlib.c:67
static int dIsdigit(unsigned char c)
Definition dlib.h:50
char * dStr_memmem(Dstr *haystack, Dstr *needle)
Return a pointer to the first occurrence of needle in haystack.
Definition dlib.c:489
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:202
static int dIsalpha(unsigned char c)
Definition dlib.h:48
void dStr_sprintfa(Dstr *ds, const char *format,...)
Printf-like function that appends.
Definition dlib.c:463
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
Definition dlib.c:121
void dLib_show_messages(bool_t show)
Definition dlib.c:903
void dStr_append(Dstr *ds, const char *s)
Append a C string to a Dstr.
Definition dlib.c:315
void dList_insert_pos(Dlist *lp, void *data, int pos0)
Insert an element at a given position [0 based].
Definition dlib.c:603
char * dStrdup(const char *s)
Definition dlib.c:76
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:575
int(* dCompareFunc)(const void *a, const void *b)
Definition dlib.h:174
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
Definition dlib.c:253
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
Definition dlib.c:214
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
Definition dlib.c:387
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:640
static int dIsspace(unsigned char c)
Definition dlib.h:53
void dStr_shorten(Dstr *dst, const char *src, int n)
Shorten string so it fits in n characters.
Definition dlib.c:550
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:689
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
Definition dlib.c:650
void * dMalloc(size_t size)
Definition dlib.c:44
static int dIspunct(unsigned char c)
Definition dlib.h:52
void dStr_free(Dstr *ds, int all)
Free a dillo string.
Definition dlib.c:336
#define Dstr_char_t
Definition dlib.h:129
void dStrshred(char *s)
Clear the contents of the string.
Definition dlib.c:139
int dClose(int fd)
Close a FD handling EINTR.
Definition dlib.c:978
static int dTolower(unsigned char c)
Definition dlib.h:55
char * dStriAsciiStr(const char *haystack, const char *needle)
Case insensitive strstr.
Definition dlib.c:183
int dList_find_idx(Dlist *lp, const void *data)
Search a data item.
Definition dlib.c:710
static int dIsascii(unsigned char c)
Definition dlib.h:57
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
Definition dlib.c:307
static int dIsalnum(unsigned char c)
Definition dlib.h:47
void dStr_append_c(Dstr *ds, int c)
Append one character.
Definition dlib.c:348
char * dStrndup(const char *s, size_t sz)
Definition dlib.c:87
void dStr_sprintf(Dstr *ds, const char *format,...)
Printf-like function.
Definition dlib.c:449
void dList_sort(Dlist *lp, dCompareFunc func)
Sort the list using a custom function.
Definition dlib.c:785
void dStr_vsprintfa(Dstr *ds, const char *format, va_list argp)
vsprintf-like function that appends.
Definition dlib.c:400
Dstr * dStr_new(const char *s)
Create a new string.
Definition dlib.c:324
void dStr_shred(Dstr *ds)
Clear a Dstr.
Definition dlib.c:378
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
Definition dlib.c:624
void dStr_vsprintf(Dstr *ds, const char *format, va_list argp)
vsprintf-like function.
Definition dlib.c:438
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
Definition dlib.c:823
void dList_free(Dlist *lp)
Free a list (not its elements)
Definition dlib.c:591
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:277
void * dList_find_custom(Dlist *lp, const void *data, dCompareFunc func)
Search a data item using a custom function.
Definition dlib.c:731
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:861
void dList_prepend(Dlist *lp, void *data)
Prepend a data item to the list.
Definition dlib.c:632
int dUsleep(unsigned long us)
Portable usleep() function.
Definition dlib.c:994
static int dIscntrl(unsigned char c)
Definition dlib.h:49
void dStr_fit(Dstr *ds)
Return memory if there's too much allocated.
Definition dlib.c:268
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:512
static int dIsxdigit(unsigned char c)
Definition dlib.h:54
void * dRealloc(void *mem, size_t size)
Definition dlib.c:52
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:297
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
Definition dlib.c:367
void dList_remove(Dlist *lp, const void *data)
Definition dlib.c:668
char * dStrnfill(size_t len, char c)
Return a new string of length 'len' filled with 'c' characters.
Definition dlib.c:148
char * dGethomedir(void)
Return the home directory in a static string (don't free)
Definition dlib.c:933
char * dGetcwd(void)
Return the current working directory in a new string.
Definition dlib.c:915
void * dList_find(Dlist *lp, const void *data)
Return the found data item, or NULL if not present.
Definition dlib.c:699
Definition dlib.h:161
int len
Definition dlib.h:163
int sz
Definition dlib.h:162
void ** list
Definition dlib.h:164
Definition dlib.h:131
Dstr_char_t * str
Definition dlib.h:134
int len
Definition dlib.h:133
int sz
Definition dlib.h:132