35#define DLIB_MSG(...) \
38 fprintf(stderr, __VA_ARGS__); \
47 void *value = malloc (size);
55 void *value = realloc (mem, size);
64 memset (value, 0, size);
80 int len = strlen(s)+1;
81 char *ns =
dNew(
char, len);
91 char *ns =
dNew(
char, sz+1);
110 for (s = (
char*)s1; s; s = va_arg(args,
char*))
129 for (len = strlen(p); len &&
dIsspace(p[len-1]); --len);
143 memset(s, 0, strlen(s));
151 char *ret =
dNew(
char, len+1);
152 for (ret[len] = 0; len > 0; ret[--len] = c);
166 p = strpbrk(str, delim);
189 if (haystack && needle) {
190 for (i = 0, j = 0; haystack[i] && needle[j]; ++i)
198 ret = (
char *)(haystack + i - j);
207 while ((*s1 || *s2) &&
219 while (n-- && (*s1 || *s2) &&
237 if (keep && n_sz > ds->
len) {
282 if (ds && s && l && pos_0 >= 0 && pos_0 <= ds->len) {
283 for (n_sz = ds->
sz; ds->
len + l >= n_sz; n_sz *= 2);
288 memmove(ds->
str+pos_0+l, ds->
str+pos_0, ds->
len-pos_0);
289 memcpy(ds->
str+pos_0, s, l);
354 if (ds->
sz > ds->
len + 1) {
370 if (ds && len < ds->len) {
381 if (ds && ds->
sz > 0)
382 memset(ds->
str,
'\0', ds->
sz);
390 if (ds && pos_0 >= 0 && len > 0 && pos_0 + len <= ds->len) {
391 memmove(ds->
str + pos_0, ds->
str + pos_0 + len, ds->
len - pos_0 - len);
408 va_copy(argp2, argp);
409 n = vsnprintf(ds->
str + ds->
len, ds->
sz - ds->
len, format, argp2);
415 if (n > -1 && n + 1 < ds->
sz - ds->
len) {
422 if (n > -1 && n < ds->sz - ds->
len) {
426 n_sz = ds->
len + n + 1;
455 va_start(argp, format);
469 va_start(argp, format);
494 if (needle && haystack) {
495 if (needle->
len == 0)
496 return haystack->
str;
498 for (i = 0; i <= (haystack->
len - needle->
len); i++) {
499 if (haystack->
str[i] == needle->
str[0] &&
500 !memcmp(haystack->
str + i, needle->
str, needle->
len))
501 return haystack->
str + i;
516 static const char *
const HEX =
"0123456789ABCDEF";
517 static Dstr *out = NULL;
527 for (i = 0; (i < in->
len) && (out->
len < maxlen); ++i) {
528 if (isprint(in->
str[i]) || (in->
str[i] ==
'\n')) {
536 if (out->
len >= maxlen)
580 if (!lp || pos0 < 0 || pos0 > lp->
len)
583 if (lp->
sz == lp->
len) {
589 for (i = lp->
len - 1; i > pos0; --i)
591 lp->
list[pos0] = data;
630 for (i = 0; i < lp->
len; ++i) {
631 if (lp->
list[i] == data) {
648 for (i = 0; i < lp->
len; ++i) {
649 if (lp->
list[i] == data) {
651 for (j = i; j < lp->
len; ++j)
664 if (!lp || n0 < 0 || n0 >= lp->
len)
675 return (i >= 0) ? lp->
list[i] : NULL;
690 for (i = 0; i < lp->
len; ++i) {
691 if (lp->
list[i] == data) {
712 for (i = 0; i < lp->
len; ++i) {
713 if (func(lp->
list[i], data) == 0) {
727 void **p = left, **q = right, **t = left;
730 while (p != t && compare(*p, *t) < 0)
732 while (q != t && compare(*q, *t) > 0)
760 if (lp && lp->
len > 1) {
778 st = func(lp->
list[i], data);
806 st = func(lp->
list[i], data);
844 if (!*line[0] || *line[0] ==
'#') {
847 }
else if ((eq = strchr(*line,
'='))) {
849 for (p = *line; *p && *p !=
'=' && !
dIsspace(*p); ++p);
861 if (len >= 2 && *p ==
'"' && p[len-1] ==
'"') {
893 char *buffer =
dNew(
char, size);
894 if (getcwd (buffer, size) == buffer)
908 static char *homedir = NULL;
911 if (getenv(
"HOME")) {
912 homedir =
dStrdup(getenv(
"HOME"));
914 }
else if (getenv(
"HOMEDRIVE") && getenv(
"HOMEPATH")) {
915 homedir =
dStrconcat(getenv(
"HOMEDRIVE"), getenv(
"HOMEPATH"), NULL);
917 DLIB_MSG(
"dGethomedir: $HOME not set, using '/'.\n");
937 while ((ch = fgetc(stream)) != EOF) {
943 line = (dstr->
len) ? dstr->
str : NULL;
957 while (st == -1 && errno == EINTR);
972 ts.tv_sec = usec / 1000000UL;
973 ts.tv_nsec = (usec % 1000000UL) * 1000UL;
976 res = nanosleep(&ts, &ts);
977 }
while (res && errno == EINTR);
char * dGetline(FILE *stream)
Get a line from a FILE stream.
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
int dStr_cmp(Dstr *ds1, Dstr *ds2)
Compare two dStrs.
char * dStrsep(char **orig, const char *delim)
strsep() implementation
void * dMalloc0(size_t size)
char * dStr_memmem(Dstr *haystack, Dstr *needle)
Return a pointer to the first occurrence of needle in haystack.
int dStrAsciiCasecmp(const char *s1, const char *s2)
void dStr_sprintfa(Dstr *ds, const char *format,...)
Printf-like function that appends.
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
void dLib_show_messages(bool_t show)
void dStr_append(Dstr *ds, const char *s)
Append a C string to a Dstr.
void dList_insert_pos(Dlist *lp, void *data, int pos0)
Insert an element at a given position [0 based].
char * dStrdup(const char *s)
Dlist * dList_new(int size)
Create a new empty list.
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
int dList_length(Dlist *lp)
For completing the ADT.
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
void * dMalloc(size_t size)
void dStr_free(Dstr *ds, int all)
Free a dillo string.
static void dStr_resize(Dstr *ds, int n_sz, int keep)
Private allocator.
void dStrshred(char *s)
Clear the contents of the string.
int dClose(int fd)
Close a FD handling EINTR.
char * dStriAsciiStr(const char *haystack, const char *needle)
Case insensitive strstr.
int dList_find_idx(Dlist *lp, const void *data)
Search a data item.
static void QuickSort(void **left, void **right, dCompareFunc compare)
QuickSort implementation.
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
void dStr_append_c(Dstr *ds, int c)
Append one character.
int dUsleep(unsigned long usec)
Portable usleep() function.
char * dStrndup(const char *s, size_t sz)
static bool_t dLib_show_msg
void dStr_sprintf(Dstr *ds, const char *format,...)
Printf-like function.
void dList_sort(Dlist *lp, dCompareFunc func)
Sort the list using a custom function.
void dStr_vsprintfa(Dstr *ds, const char *format, va_list argp)
vsprintf-like function that appends.
Dstr * dStr_new(const char *s)
Create a new string.
void dStr_shred(Dstr *ds)
Clear a Dstr.
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
void dStr_vsprintf(Dstr *ds, const char *format, va_list argp)
vsprintf-like function.
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
void dList_free(Dlist *lp)
Free a list (not its elements)
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).
void * dList_find_custom(Dlist *lp, const void *data, dCompareFunc func)
Search a data item using a custom function.
int dParser_parse_rc_line(char **line, char **name, char **value)
Take a dillo rc line and return 'name' and 'value' pointers to it.
void dList_prepend(Dlist *lp, void *data)
Prepend a data item to the list.
void dStr_fit(Dstr *ds)
Return memory if there's too much allocated.
const char * dStr_printable(Dstr *in, int maxlen)
Return a printable representation of the provided Dstr, limited to a length of roughly maxlen.
void * dRealloc(void *mem, size_t size)
void dStr_insert(Dstr *ds, int pos_0, const char *s)
Insert a C string, at a given position, into a Dstr.
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
void dList_remove(Dlist *lp, const void *data)
char * dStrnfill(size_t len, char c)
Return a new string of length 'len' filled with 'c' characters.
char * dGethomedir(void)
Return the home directory in a static string (don't free)
char * dGetcwd(void)
Return the current working directory in a new string.
void * dList_find(Dlist *lp, const void *data)
Return the found data item, or NULL if not present.
int(* dCompareFunc)(const void *a, const void *b)
#define dReturn_val_if_fail(expr, val)
#define D_ASCII_TOLOWER(c)
#define dNew(type, count)