changeset 7:41885619fc79

Updates.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Nov 2008 22:17:02 +0200
parents 2f92373537b5
children acc2a8035dd5
files th_config.c th_string.c th_string.h th_util.c
diffstat 4 files changed, 62 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/th_config.c	Sat Oct 18 08:49:26 2008 +0300
+++ b/th_config.c	Wed Nov 26 22:17:02 2008 +0200
@@ -19,12 +19,12 @@
 #define LNEXT		(pNode->next)
 
 
-void th_config_error(char_t * pcFilename, size_t lineNum,
-	const char_t * pcFormat, ...)
+void th_config_error(char * filename, size_t lineNum,
+	const char * pcFormat, ...)
 {
 	va_list ap;
 	va_start(ap, pcFormat);
-	fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, pcFilename, lineNum);
+	fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, filename, lineNum);
 	vfprintf(stderr, pcFormat, ap);
 	va_end(ap);
 }
@@ -65,7 +65,7 @@
 
 /* Allocate and add new item to configuration
  */
-th_cfgitem_t *th_config_add(th_config_t * cfg, char_t * itemName, int itemType,
+th_cfgitem_t *th_config_add(th_config_t * cfg, char * itemName, int itemType,
 			     BOOL(*itemValidate) (th_cfgitem_t *), void *itemData)
 {
 	th_cfgitem_t *pNode;
@@ -103,7 +103,7 @@
 
 /* Add integer type setting into give configuration
  */
-int th_config_add_int(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
+int th_config_add_int(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		      int *itemData, int itemDef)
 {
 	th_cfgitem_t *pNode;
@@ -120,7 +120,7 @@
 
 /* Add unsigned integer type setting into give configuration
  */
-int th_config_add_uint(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
+int th_config_add_uint(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		       unsigned int * itemData, unsigned int itemDef)
 {
 	th_cfgitem_t *pNode;
@@ -137,8 +137,8 @@
 
 /* Add strint type setting into given configuration
  */
-int th_config_add_str(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
-		      char_t ** itemData, char_t * itemDef)
+int th_config_add_str(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
+		      char ** itemData, char * itemDef)
 {
 	th_cfgitem_t *pNode;
 
@@ -147,7 +147,7 @@
 		return -1;
 
 	if (itemDef != NULL)
-		*itemData = th_strdup(itemDef);
+		*itemData = strdup(itemDef);
 	else
 		*itemData = NULL;
 
@@ -157,7 +157,7 @@
 
 /* Add boolean type setting into given configuration
  */
-int th_config_add_bool(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
+int th_config_add_bool(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		       BOOL * itemData, BOOL itemDef)
 {
 	th_cfgitem_t *pNode;
@@ -191,11 +191,11 @@
 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c))
 
-int th_config_read(char_t * pcFilename, th_config_t * cfg)
+int th_config_read(char * filename, th_config_t * cfg)
 {
 	FILE *inFile;
 	th_cfgitem_t *item;
-	char_t tmpStr[SET_MAX_BUF + 1];
+	char tmpStr[SET_MAX_BUF + 1];
 	size_t lineNum, strPos;
 	int c, parseMode, prevMode, nextMode, tmpCh;
 	BOOL isFound, isStart, tmpBool, isError, validError;
@@ -203,7 +203,7 @@
 	assert(cfg);
 
 	/* Open the file */
-	if ((inFile = fopen(pcFilename, "rb")) == NULL)
+	if ((inFile = fopen(filename, "rb")) == NULL)
 		return -1;
 
 	/* Initialize values */
@@ -222,7 +222,7 @@
 			switch (c = fgetc(inFile)) {
 			case EOF:
 				if (parseMode != PM_NORMAL) {
-					th_config_error(pcFilename, lineNum,
+					th_config_error(filename, lineNum,
 					"Unexpected end of file.\n");
 					parseMode = PM_ERROR;
 				} else
@@ -260,7 +260,7 @@
 				strPos = 0;
 			} else {
 				/* Error! Invalid character found */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 					"Unexpected character '%c'\n", c);
 				parseMode = PM_ERROR;
 			}
@@ -284,14 +284,14 @@
 				else
 				{
 					/* Error! Key name string too long! */
-					th_config_error(pcFilename, lineNum,
+					th_config_error(filename, lineNum,
 						"Config key name too long!");
 					parseMode = PM_ERROR;
 				}
 				c = -1;
 			} else {
 				/* Error! Invalid character found */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 					"Unexpected character '%c'\n", c);
 				parseMode = PM_ERROR;
 			}
@@ -334,7 +334,7 @@
 					strPos = 0;
 				} else {
 					/* Error! No configuration key by this name found */
-					th_config_error(pcFilename, lineNum,
+					th_config_error(filename, lineNum,
 						"No such configuration setting ('%s')\n",
 						tmpStr);
 					parseMode = PM_ERROR;
@@ -343,7 +343,7 @@
 				c = -1;
 			} else {
 				/* Error! '=' expected! */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 					"Unexpected character '%c', '=' expected.\n", c);
 				parseMode = PM_ERROR;
 			}
@@ -375,7 +375,7 @@
 			} else if (c == tmpCh) {
 				/* End of string, set the value */
 				tmpStr[strPos] = 0;
-				th_pstrcpy((char_t **) item->itemData, tmpStr);
+				th_pstrcpy((char **) item->itemData, tmpStr);
 				if (item->itemValidate) {
 					if (!item->itemValidate(item))
 						validError = TRUE;
@@ -389,7 +389,7 @@
 				else
 				{
 					/* Error! String too long! */
-					th_config_error(pcFilename, lineNum,
+					th_config_error(filename, lineNum,
 						"String too long! Maximum is %d characters.",
 						SET_MAX_BUF);
 					parseMode = PM_ERROR;
@@ -403,7 +403,7 @@
 			/* Integer parsing mode */
 			if (isStart && (item->itemType == ITEM_UINT) && (c == '-')) {
 				/* Error! Negative values not allowed for unsigned ints */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 						"Negative value specified, unsigned value expected.");
 				parseMode = PM_ERROR;
 			} else if (isStart && (c == '-' || c == '+')) {
@@ -435,14 +435,14 @@
 				parseMode = PM_NORMAL;
 			} else {
 				/* Error! Unexpected character. */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 						"Unexpected character, ", SET_MAX_BUF);
 				parseMode = PM_ERROR;
 			}
 
 			if (isError) {
 				/* Error! String too long! */
-				th_config_error(pcFilename, lineNum,
+				th_config_error(filename, lineNum,
 						"String too long! Maximum is %d characters.",
 						SET_MAX_BUF);
 				parseMode = PM_ERROR;
@@ -520,7 +520,7 @@
 	item = cfg->items;
 	while (item) {
 		if (!item->itemData || ((item->itemType == ITEM_STRING) &&
-			*(char_t **) item->itemData == NULL)) {
+			*(char **) item->itemData == NULL)) {
 			
 			fprintf(outFile, "#%s = ", item->itemName);
 			
@@ -548,7 +548,7 @@
 			switch (item->itemType) {
 			case ITEM_STRING:
 				fprintf(outFile, "\"%s\"",
-					*((char_t **) item->itemData));
+					*((char **) item->itemData));
 				break;
 
 			case ITEM_INT:
--- a/th_string.c	Sat Oct 18 08:49:26 2008 +0300
+++ b/th_string.c	Wed Nov 26 22:17:02 2008 +0200
@@ -29,42 +29,16 @@
 }
 
 
-/* Duplicate a string [strdup]
- */
-char *th_strdup(char * str)
+char *th_strncpy(char * dst, const char * src, size_t n)
 {
-	char *result, *s, *d;
-
-	if (!str) return NULL;
-	
-	/* Allocate memory for destination */
-	result = th_stralloc(strlen(str) + 1);
-	if (!result)
-		return NULL;
-
-	/* Copy to the destination */
-	s = str;
-	d = result;
-	while (*s) {
-		*(d++) = *(s++);
-	}
-	*d = 0;
-
-	return result;
-}
-
-
-char *th_strncpy(char * dst, char * src, size_t n)
-{
-	char *s, *d;
+	const char *s = src;
+	char *d = dst;
 	size_t i;
-	assert(src);
-	assert(dst);
+	assert(src != NULL);
+	assert(dst != NULL);
 
 	/* Copy to the destination */
 	i = n;
-	s = src;
-	d = dst;
 	while (*s && (i > 0)) {
 		*(d++) = *(s++);
 		i--;
@@ -86,8 +60,8 @@
 int th_strncmp(char * str1, char * str2, size_t n)
 {
 	char *s1, *s2;
-	assert(str1);
-	assert(str2);
+	assert(str1 != NULL);
+	assert(str2 != NULL);
 
 	/* Check the string pointers */
 	if (str1 == str2)
@@ -114,8 +88,8 @@
 int th_strcasecmp(char * str1, char * str2)
 {
 	char *s1 = str1, *s2 = str2;
-	assert(str1);
-	assert(str2);
+	assert(str1 != NULL);
+	assert(str2 != NULL);
 
 	/* Check the string pointers */
 	if (str1 == str2)
@@ -134,8 +108,8 @@
 int th_strncasecmp(char * str1, char * str2, size_t n)
 {
 	char *s1 = str1, *s2 = str2;
-	assert(str1);
-	assert(str2);
+	assert(str1 != NULL);
+	assert(str2 != NULL);
 
 	/* Check the string pointers */
 	if (str1 == str2)
@@ -161,7 +135,7 @@
 void th_strip_ctrlchars(char * str)
 {
 	char *i, *j;
-	assert(str);
+	assert(str != NULL);
 
 	i = str;
 	j = str;
@@ -179,10 +153,10 @@
  */
 int th_pstrcpy(char ** result, char * str)
 {
-	assert(result);
+	assert(result != NULL);
 
 	/* Check the string pointers */
-	if (!str)
+	if (str == NULL)
 		return -1;
 
 	/* Allocate memory for destination */
@@ -202,10 +176,10 @@
  */
 int th_pstrcat(char ** result, char * str)
 {
-	assert(result);
+	assert(result != NULL);
 
 	/* Check the string pointers */
-	if (!str)
+	if (str == NULL)
 		return -1;
 
 	if (*result != NULL) {
@@ -232,7 +206,7 @@
  */
 char *th_findnext(char * str, size_t * iPos)
 {
-	assert(str);
+	assert(str != NULL);
 
 	/* Terminating NULL-character is not whitespace! */
 	while (th_isspace(str[*iPos]))
@@ -245,7 +219,7 @@
  */
 char *th_findsep(char * str, size_t * iPos, char chSep)
 {
-	assert(str);
+	assert(str != NULL);
 
 	/* Terminating NULL-character is not digit! */
 	while (str[*iPos] && (str[*iPos] != chSep))
@@ -258,7 +232,7 @@
  */
 char *th_findseporspace(char * str, size_t * iPos, char chSep)
 {
-	assert(str);
+	assert(str != NULL);
 
 	/* Terminating NULL-character is not digit! */
 	while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep))
@@ -274,21 +248,13 @@
  */
 BOOL th_strmatch(char * str, char * pattern)
 {
-	BOOL didMatch, isAnyMode, isEnd;
-	char *tmpPattern;
+	BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
+	char *tmpPattern = NULL;
 
 	/* Check given pattern and string */
-	if (!str)
-		return FALSE;
-	if (!pattern)
+	if (str == NULL || pattern == NULL)
 		return FALSE;
 
-	/* Initialize */
-	tmpPattern = NULL;
-	didMatch = TRUE;
-	isEnd = FALSE;
-	isAnyMode = FALSE;
-
 	/* Start comparision */
 	do {
 		didMatch = FALSE;
@@ -330,7 +296,7 @@
 			break;
 		default:
 			if (isAnyMode) {
-				if ((*pattern) == (*str)) {
+				if (*pattern == *str) {
 					isAnyMode = FALSE;
 					didMatch = TRUE;
 				} else {
@@ -340,7 +306,7 @@
 					}
 				}
 			} else {
-				if ((*pattern) == (*str)) {
+				if (*pattern == *str) {
 					didMatch = TRUE;
 					if (*pattern)
 						pattern++;
@@ -361,7 +327,7 @@
 
 		}		/* switch */
 
-	} while ((didMatch) && (!isEnd));
+	} while (didMatch && !isEnd);
 
 	return didMatch;
 }
@@ -371,21 +337,13 @@
  */
 BOOL th_strcasematch(char * str, char * pattern)
 {
-	BOOL didMatch, isAnyMode, isEnd;
-	char *tmpPattern;
+	BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
+	char *tmpPattern = NULL;
 
 	/* Check given pattern and string */
-	if (!str)
-		return FALSE;
-	if (!pattern)
+	if (str == NULL || pattern == NULL)
 		return FALSE;
 
-	/* Initialize */
-	tmpPattern = NULL;
-	didMatch = TRUE;
-	isEnd = FALSE;
-	isAnyMode = FALSE;
-
 	/* Start comparision */
 	do {
 		switch (*pattern) {
@@ -400,7 +358,7 @@
 
 		case '*':
 			pattern++;
-			if (!*pattern || (*pattern == '?'))
+			if (!*pattern || *pattern == '?')
 				isEnd = TRUE;
 			isAnyMode = TRUE;
 			tmpPattern = pattern;
@@ -455,7 +413,7 @@
 
 		}		/* switch */
 
-	} while ((didMatch) && (!isEnd));
+	} while (didMatch && !isEnd);
 
 	return didMatch;
 }
--- a/th_string.h	Sat Oct 18 08:49:26 2008 +0300
+++ b/th_string.h	Wed Nov 26 22:17:02 2008 +0200
@@ -43,8 +43,7 @@
  */
 char	*th_stralloc(size_t);
 char	*th_strrealloc(char *, size_t);
-char	*th_strdup(char *);
-char	*th_strncpy(char *, char *, size_t);
+char	*th_strncpy(char *, const char *, size_t);
 int	th_strcasecmp(char *, char *);
 int	th_strncasecmp(char *, char *, size_t);
 void	th_strip_ctrlchars(char *);
--- a/th_util.c	Sat Oct 18 08:49:26 2008 +0300
+++ b/th_util.c	Wed Nov 26 22:17:02 2008 +0200
@@ -13,7 +13,7 @@
 
 /* Default settings
  */
-BOOL	th_isInitialized = FALSE;
+static BOOL	th_initialized = FALSE;
 int	th_verbosityLevel = 2;
 char	*th_prog_name = NULL,
 	*th_prog_fullname = NULL,
@@ -41,7 +41,7 @@
 	else
 		th_prog_license = TH_PROG_LICENSE;
 
-	th_isInitialized = TRUE;
+	th_initialized = TRUE;
 }
 
 
@@ -51,7 +51,7 @@
 void THERR(const char *pcFormat, ...)
 {
 	va_list ap;
-	assert(th_isInitialized);
+	assert(th_initialized);
 
 	va_start(ap, pcFormat);
 	fprintf(stderr, "%s: ", th_prog_name);
@@ -63,7 +63,7 @@
 void THMSG(int verbLevel, const char *pcFormat, ...)
 {
 	va_list ap;
-	assert(th_isInitialized);
+	assert(th_initialized);
 
 	/* Check if the current verbosity level is enough */
 	if (th_verbosityLevel >= verbLevel) {
@@ -78,7 +78,7 @@
 void THPRINT(int verbLevel, const char *pcFormat, ...)
 {
 	va_list ap;
-	assert(th_isInitialized);
+	assert(th_initialized);
 
 	/* Check if the current verbosity level is enough */
 	if (th_verbosityLevel >= verbLevel) {