Dillo v3.2.0-88-g47ab7c70
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 *-- Casts -------------------------------------------------------------------
52 */
53/* TODO: include a void* size test in configure.in */
54/* (long) works for both 32bit and 64bit */
55#define VOIDP2INT(p) ((long)(p))
56#define INT2VOIDP(i) ((void*)((long)(i)))
57
58/*
59 *-- Memory -------------------------------------------------------------------
60 */
61#define dNew(type, count) \
62 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
63#define dNew0(type, count) \
64 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
65
66void *dMalloc (size_t size);
67void *dRealloc (void *mem, size_t size);
68void *dMalloc0 (size_t size);
69void dFree (void *mem);
70
71/*
72 *- Debug macros --------------------------------------------------------------
73 */
74#define D_STMT_START do
75#define D_STMT_END while (0)
76#define dReturn_if(expr) \
77 D_STMT_START{ \
78 if (expr) { return; }; \
79 }D_STMT_END
80#define dReturn_val_if(expr,val) \
81 D_STMT_START{ \
82 if (expr) { return val; }; \
83 }D_STMT_END
84#define dReturn_if_fail(expr) \
85 D_STMT_START{ \
86 if (!(expr)) { return; }; \
87 }D_STMT_END
88#define dReturn_val_if_fail(expr,val) \
89 D_STMT_START{ \
90 if (!(expr)) { return val; }; \
91 }D_STMT_END
92
93/*
94 *- C strings -----------------------------------------------------------------
95 */
96char *dStrdup(const char *s);
97char *dStrndup(const char *s, size_t sz);
98char *dStrconcat(const char *s1, ...);
99char *dStrstrip(char *s);
100char *dStrnfill(size_t len, char c);
101char *dStrsep(char **orig, const char *delim);
102void dStrshred(char *s);
103char *dStriAsciiStr(const char *haystack, const char *needle);
104int dStrAsciiCasecmp(const char *s1, const char *s2);
105int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n);
106
107#define dStrerror strerror
108
109/*
110 *-- dStr ---------------------------------------------------------------------
111 */
112#define Dstr_char_t char
113
114typedef struct {
115 int sz; /* allocated size (private) */
116 int len;
118} Dstr;
119
120Dstr *dStr_new (const char *s);
121Dstr *dStr_sized_new (int sz);
122void dStr_fit (Dstr *ds);
123void dStr_free (Dstr *ds, int all);
124void dStr_append_c (Dstr *ds, int c);
125void dStr_append (Dstr *ds, const char *s);
126void dStr_append_l (Dstr *ds, const char *s, int l);
127void dStr_insert (Dstr *ds, int pos_0, const char *s);
128void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
129void dStr_truncate (Dstr *ds, int len);
130void dStr_shred (Dstr *ds);
131void dStr_erase (Dstr *ds, int pos_0, int len);
132void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
133void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
134void dStr_sprintf (Dstr *ds, const char *format, ...);
135void dStr_sprintfa (Dstr *ds, const char *format, ...);
136int dStr_cmp(Dstr *ds1, Dstr *ds2);
137char *dStr_memmem(Dstr *haystack, Dstr *needle);
138const char *dStr_printable(Dstr *in, int maxlen);
139void dStr_shorten(Dstr *dst, const char *src, int n);
140
141/*
142 *-- dList --------------------------------------------------------------------
143 */
144typedef struct {
145 int sz; /* allocated size (private) */
146 int len;
147 void **list;
148} Dlist;
149
150/* dCompareFunc:
151 * Return: 0 if parameters are equal (for dList_find_custom).
152 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
153 *
154 * For finding a data node with an external key, the comparison function
155 * parameters are: first the data node, and then the key.
156 */
157typedef int (*dCompareFunc) (const void *a, const void *b);
158
159
160Dlist *dList_new(int size);
161void dList_free (Dlist *lp);
162void dList_append (Dlist *lp, void *data);
163void dList_prepend (Dlist *lp, void *data);
164void dList_insert_pos (Dlist *lp, void *data, int pos0);
165int dList_length (Dlist *lp);
166void dList_remove (Dlist *lp, const void *data);
167void dList_remove_fast (Dlist *lp, const void *data);
168void *dList_nth_data (Dlist *lp, int n0);
169void *dList_find (Dlist *lp, const void *data);
170int dList_find_idx (Dlist *lp, const void *data);
171void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
172void dList_sort (Dlist *lp, dCompareFunc func);
173void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
174void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
175
176/*
177 *- Parse function ------------------------------------------------------------
178 */
179int dParser_parse_rc_line(char **line, char **name, char **value);
180
181/*
182 *- Dlib messages -------------------------------------------------------------
183 */
184void dLib_show_messages(bool_t show);
185
186/*
187 *- Misc utility functions ----------------------------------------------------
188 */
189char *dGetcwd(void);
190char *dGethomedir(void);
191char *dGetline(FILE *stream);
192int dClose(int fd);
193int dUsleep(unsigned long us);
194
195#ifdef __cplusplus
196}
197#endif /* __cplusplus */
198
199#endif /* __DLIB_H__ */
200
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:157
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:112
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:144
int len
Definition dlib.h:146
int sz
Definition dlib.h:145
void ** list
Definition dlib.h:147
Definition dlib.h:114
Dstr_char_t * str
Definition dlib.h:117
int len
Definition dlib.h:116
int sz
Definition dlib.h:115