aboutsummaryrefslogtreecommitdiff
path: root/dlib
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
commitf5c598b518d1f906148534d015f50075d3e8242d (patch)
tree21dd70add5b366c3dd80641b77f6b18e0baa009e /dlib
parente98d02a01ffeb18ede86af025e51ae1ec011c75a (diff)
parent5f0fc0e48b8cbee7e1795935da0abff6627fd498 (diff)
merge
Diffstat (limited to 'dlib')
-rw-r--r--dlib/dlib.c78
-rw-r--r--dlib/dlib.h20
2 files changed, 74 insertions, 24 deletions
diff --git a/dlib/dlib.c b/dlib/dlib.c
index f92f64e7..b87d0ce1 100644
--- a/dlib/dlib.c
+++ b/dlib/dlib.c
@@ -27,6 +27,15 @@
#include "dlib.h"
+static bool_t dLib_show_msg = TRUE;
+
+/* dlib msgs go to stderr to avoid problems with filter dpis */
+#define DLIB_MSG(...) \
+ D_STMT_START { \
+ if (dLib_show_msg) \
+ fprintf(stderr, __VA_ARGS__); \
+ } D_STMT_END
+
/*
*- Memory --------------------------------------------------------------------
*/
@@ -115,8 +124,8 @@ char *dStrstrip(char *s)
int len;
if (s && *s) {
- for (p = s; isspace(*p); ++p);
- for (len = strlen(p); len && isspace(p[len-1]); --len);
+ for (p = s; dIsspace(*p); ++p);
+ for (len = strlen(p); len && dIsspace(p[len-1]); --len);
if (p > s)
memmove(s, p, len);
s[len] = 0;
@@ -160,18 +169,20 @@ char *dStrsep(char **orig, const char *delim)
char *dStristr(const char *haystack, const char *needle)
{
int i, j;
-
- for (i = 0, j = 0; haystack[i] && needle[j]; ++i)
- if (tolower(haystack[i]) == tolower(needle[j])) {
- ++j;
- } else if (j) {
- i -= j;
- j = 0;
- }
-
- if (!needle[j]) /* Got all */
- return (char *)(haystack + i - j);
- return NULL;
+ char *ret = NULL;
+
+ if (haystack && needle) {
+ for (i = 0, j = 0; haystack[i] && needle[j]; ++i)
+ if (tolower(haystack[i]) == tolower(needle[j])) {
+ ++j;
+ } else if (j) {
+ i -= j;
+ j = 0;
+ }
+ if (!needle[j]) /* Got all */
+ ret = (char *)(haystack + i - j);
+ }
+ return ret;
}
@@ -204,10 +215,11 @@ static void dStr_resize(Dstr *ds, int n_sz, int keep)
*/
Dstr *dStr_sized_new (int sz)
{
+ Dstr *ds;
if (sz < 2)
sz = 2;
- Dstr *ds = dNew(Dstr, 1);
+ ds = dNew(Dstr, 1);
ds->str = NULL;
dStr_resize(ds, sz + 1, 0); /* (sz + 1) for the extra '\0' */
return ds;
@@ -476,10 +488,11 @@ const char *dStr_printable(Dstr *in, int maxlen)
*/
Dlist *dList_new(int size)
{
+ Dlist *l;
if (size <= 0)
return NULL;
- Dlist *l = dNew(Dlist, 1);
+ l = dNew(Dlist, 1);
l->len = 0;
l->sz = size;
l->list = dNew(void*, l->sz);
@@ -756,9 +769,10 @@ void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func)
* - line is modified!
* - it skips blank lines and lines starting with '#'
*
- * Return value: 0 on successful value/pair, -1 otherwise
+ * Return value: 1 on blank line or comment, 0 on successful value/pair,
+ * -1 otherwise.
*/
-int dParser_get_rc_pair(char **line, char **name, char **value)
+int dParser_parse_rc_line(char **line, char **name, char **value)
{
char *eq, *p;
int len, ret = -1;
@@ -768,15 +782,22 @@ int dParser_get_rc_pair(char **line, char **name, char **value)
*name = NULL;
*value = NULL;
dStrstrip(*line);
- if (*line[0] != '#' && (eq = strchr(*line, '='))) {
+ if (!*line[0] || *line[0] == '#') {
+ /* blank line or comment */
+ ret = 1;
+ } else if ((eq = strchr(*line, '='))) {
/* get name */
- for (p = *line; *p && *p != '=' && !isspace(*p); ++p);
+ for (p = *line; *p && *p != '=' && !dIsspace(*p); ++p);
*p = 0;
*name = *line;
+ /* skip whitespace */
+ if (p < eq)
+ for (++p; dIsspace(*p); ++p);
+
/* get value */
if (p == eq) {
- for (++p; isspace(*p); ++p);
+ for (++p; dIsspace(*p); ++p);
len = strlen(p);
if (len >= 2 && *p == '"' && p[len-1] == '"') {
p[len-1] = 0;
@@ -791,6 +812,14 @@ int dParser_get_rc_pair(char **line, char **name, char **value)
}
/*
+ *- Dlib messages -------------------------------------------------------------
+ */
+void dLib_show_messages(bool_t show)
+{
+ dLib_show_msg = show;
+}
+
+/*
*- Misc utility functions ----------------------------------------------------
*/
@@ -825,6 +854,9 @@ char *dGethomedir ()
} else if (getenv("HOMEDRIVE") && getenv("HOMEPATH")) {
homedir = dStrconcat(getenv("HOMEDRIVE"), getenv("HOMEPATH"), NULL);
+ } else {
+ DLIB_MSG("dGethomedir: $HOME not set, using '/'.\n");
+ homedir = dStrdup("/");
}
}
return homedir;
@@ -844,10 +876,10 @@ char *dGetline (FILE *stream)
dReturn_val_if_fail (stream, 0);
dstr = dStr_sized_new(64);
- while((ch = fgetc(stream)) != EOF) {
+ while ((ch = fgetc(stream)) != EOF) {
if (ch == '\\') {
/* continue with the next line */
- while((ch = fgetc(stream)) != EOF && ch != '\n');
+ while ((ch = fgetc(stream)) != EOF && ch != '\n') ;
continue;
}
dStr_append_c(dstr, ch);
diff --git a/dlib/dlib.h b/dlib/dlib.h
index aa32beb4..ae9c7286 100644
--- a/dlib/dlib.h
+++ b/dlib/dlib.h
@@ -7,6 +7,7 @@
#include <string.h> /* for strerror */
#include <strings.h> /* for strcasecmp, strncasecmp (POSIX 2001) */
+#include "d_size.h"
#ifdef __cplusplus
extern "C" {
@@ -29,6 +30,10 @@ extern "C" {
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+/* Handle signed char */
+#define dIsspace(c) isspace((uchar_t)(c))
+#define dIsalnum(c) isalnum((uchar_t)(c))
+
/*
*-- Casts -------------------------------------------------------------------
*/
@@ -55,6 +60,14 @@ void dFree (void *mem);
*/
#define D_STMT_START do
#define D_STMT_END while (0)
+#define dReturn_if(expr) \
+ D_STMT_START{ \
+ if (expr) { return; }; \
+ }D_STMT_END
+#define dReturn_val_if(expr,val) \
+ D_STMT_START{ \
+ if (expr) { return val; }; \
+ }D_STMT_END
#define dReturn_if_fail(expr) \
D_STMT_START{ \
if (!(expr)) { return; }; \
@@ -150,7 +163,12 @@ void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
/*
*- Parse function ------------------------------------------------------------
*/
-int dParser_get_rc_pair(char **line, char **name, char **value);
+int dParser_parse_rc_line(char **line, char **name, char **value);
+
+/*
+ *- Dlib messages -------------------------------------------------------------
+ */
+void dLib_show_messages(bool_t show);
/*
*- Misc utility functions ----------------------------------------------------