changeset 3:5a327a2988fa

Breaking the API a bit, cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Mar 2008 05:03:16 +0200
parents efaa9e3a8446
children 67f4a4233372
files th_config.c th_config.h
diffstat 2 files changed, 83 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/th_config.c	Wed Mar 26 04:49:16 2008 +0200
+++ b/th_config.c	Wed Mar 26 05:03:16 2008 +0200
@@ -1,7 +1,7 @@
 /*
  * Very simple configuration handling functions
  * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2004-2007 Tecnic Software productions (TNSP)
+ * (C) Copyright 2004-2008 Tecnic Software productions (TNSP)
  *
  * Please read file 'COPYING' for information on license and distribution.
  */
@@ -14,8 +14,9 @@
 #include "th_util.h"
 #include "th_string.h"
 
-#define LPREV (pNode->pPrev)
-#define LNEXT (pNode->pNext)
+#define SET_MAX_BUF	(8192)
+#define LPREV		(pNode->prev)
+#define LNEXT		(pNode->next)
 
 
 void th_config_error(char_t * pcFilename, size_t lineNum,
@@ -31,11 +32,11 @@
 
 /* Create a new configuration
  */
-t_config *th_config_new(void)
+th_config_t *th_config_new(void)
 {
-	t_config *cfg;
+	th_config_t *cfg;
 
-	cfg = (t_config *) th_calloc(1, sizeof(t_config));
+	cfg = (th_config_t *) th_calloc(1, sizeof(th_config_t));
 	if (!cfg)
 		return NULL;
 
@@ -45,35 +46,35 @@
 
 /* Free a given configuration (the values are not free'd)
  */
-void th_config_free(t_config * cfg)
+void th_config_free(th_config_t * cfg)
 {
-	t_config_item *pCurr, *pNext;
+	th_cfgitem_t *curr, *next;
 
 	if (!cfg)
 		return;
 
-	pCurr = cfg->pItems;
-	while (pCurr) {
-		pNext = pCurr->pNext;
-		th_free(pCurr->itemName);
-		th_free(pCurr);
-		pCurr = pNext;
+	curr = cfg->items;
+	while (curr) {
+		next = curr->next;
+		th_free(curr->itemName);
+		th_free(curr);
+		curr = next;
 	}
 }
 
 
 /* Allocate and add new item to configuration
  */
-t_config_item *th_config_add(t_config * cfg, char_t * itemName, int itemType,
-			     BOOL(*itemValidate) (t_config_item *), void *itemData)
+th_cfgitem_t *th_config_add(th_config_t * cfg, char_t * itemName, int itemType,
+			     BOOL(*itemValidate) (th_cfgitem_t *), void *itemData)
 {
-	t_config_item *pNode;
+	th_cfgitem_t *pNode;
 
 	if (!cfg)
 		return NULL;
 
 	/* Allocate new item */
-	pNode = (t_config_item *) th_calloc(1, sizeof(t_config_item));
+	pNode = (th_cfgitem_t *) th_calloc(1, sizeof(th_cfgitem_t));
 	if (!pNode)
 		return NULL;
 
@@ -84,14 +85,14 @@
 	th_pstrcpy(&pNode->itemName, itemName);
 
 	/* Insert into linked list */
-	if (cfg->pItems) {
-		/* The first node's pPrev points to last node */
-		LPREV = cfg->pItems->pPrev;	/* New node's prev = Previous last node */
-		cfg->pItems->pPrev->pNext = pNode;	/* Previous last node's next = New node */
-		cfg->pItems->pPrev = pNode;	/* New last node = New node */
+	if (cfg->items) {
+		/* The first node's prev points to last node */
+		LPREV = cfg->items->prev;	/* New node's prev = Previous last node */
+		cfg->items->prev->next = pNode;	/* Previous last node's next = New node */
+		cfg->items->prev = pNode;	/* New last node = New node */
 		LNEXT = NULL;	/* But next is NULL! */
 	} else {
-		cfg->pItems = pNode;	/* First node ... */
+		cfg->items = pNode;	/* First node ... */
 		LPREV = pNode;	/* ... it's also last */
 		LNEXT = NULL;	/* But next is NULL! */
 	}
@@ -102,10 +103,10 @@
 
 /* Add integer type setting into give configuration
  */
-int th_config_add_int(t_config * cfg, char_t * itemName, BOOL(*itemValidate) (t_config_item *),
+int th_config_add_int(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		      int *itemData, int itemDef)
 {
-	t_config_item *pNode;
+	th_cfgitem_t *pNode;
 
 	pNode = th_config_add(cfg, itemName, ITEM_INT, itemValidate, (void *) itemData);
 	if (!pNode)
@@ -119,10 +120,10 @@
 
 /* Add unsigned integer type setting into give configuration
  */
-int th_config_add_uint(t_config * cfg, char_t * itemName, BOOL(*itemValidate) (t_config_item *),
+int th_config_add_uint(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		       unsigned int * itemData, unsigned int itemDef)
 {
-	t_config_item *pNode;
+	th_cfgitem_t *pNode;
 
 	pNode = th_config_add(cfg, itemName, ITEM_UINT, itemValidate, (void *) itemData);
 	if (!pNode)
@@ -136,10 +137,10 @@
 
 /* Add strint type setting into given configuration
  */
-int th_config_add_str(t_config * cfg, char_t * itemName, BOOL(*itemValidate) (t_config_item *),
+int th_config_add_str(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		      char_t ** itemData, char_t * itemDef)
 {
-	t_config_item *pNode;
+	th_cfgitem_t *pNode;
 
 	pNode = th_config_add(cfg, itemName, ITEM_STRING, itemValidate, (void *) itemData);
 	if (!pNode)
@@ -156,10 +157,10 @@
 
 /* Add boolean type setting into given configuration
  */
-int th_config_add_bool(t_config * cfg, char_t * itemName, BOOL(*itemValidate) (t_config_item *),
+int th_config_add_bool(th_config_t * cfg, char_t * itemName, BOOL(*itemValidate) (th_cfgitem_t *),
 		       BOOL * itemData, BOOL itemDef)
 {
-	t_config_item *pNode;
+	th_cfgitem_t *pNode;
 
 	pNode = th_config_add(cfg, itemName, ITEM_BOOL, itemValidate, (void *) itemData);
 	if (!pNode)
@@ -190,10 +191,10 @@
 #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, t_config * cfg)
+int th_config_read(char_t * pcFilename, th_config_t * cfg)
 {
 	FILE *inFile;
-	t_config_item *pItem;
+	th_cfgitem_t *item;
 	char_t tmpStr[SET_MAX_BUF + 1];
 	size_t lineNum, strPos;
 	int c, parseMode, prevMode, nextMode, tmpCh;
@@ -206,7 +207,7 @@
 		return -1;
 
 	/* Initialize values */
-	pItem = NULL;
+	item = NULL;
 	tmpCh = 0;
 	strPos = 0;
 	lineNum = 1;
@@ -301,18 +302,18 @@
 				/* Find key from configuration */
 				tmpStr[strPos] = 0;
 				isFound = FALSE;
-				pItem = cfg->pItems;
-				while (pItem && !isFound) {
-					if (strcmp(pItem->itemName, tmpStr) == 0)
+				item = cfg->items;
+				while (item && !isFound) {
+					if (strcmp(item->itemName, tmpStr) == 0)
 						isFound = TRUE;
 					else
-						pItem = pItem->pNext;
+						item = item->next;
 				}
 
 				/* Check if key was found */
 				if (isFound) {
 					/* Okay, set next mode */
-					switch (pItem->itemType) {
+					switch (item->itemType) {
 					case ITEM_STRING:
 						nextMode = PM_STRING;
 						break;
@@ -374,9 +375,9 @@
 			} else if (c == tmpCh) {
 				/* End of string, set the value */
 				tmpStr[strPos] = 0;
-				th_pstrcpy((char_t **) pItem->itemData, tmpStr);
-				if (pItem->itemValidate) {
-					if (!pItem->itemValidate(pItem))
+				th_pstrcpy((char_t **) item->itemData, tmpStr);
+				if (item->itemValidate) {
+					if (!item->itemValidate(item))
 						validError = TRUE;
 				}
 
@@ -400,7 +401,7 @@
 
 		case PM_INT:
 			/* Integer parsing mode */
-			if (isStart && (pItem->itemType == ITEM_UINT) && (c == '-')) {
+			if (isStart && (item->itemType == ITEM_UINT) && (c == '-')) {
 				/* Error! Negative values not allowed for unsigned ints */
 				th_config_error(pcFilename, lineNum,
 						"Negative value specified, unsigned value expected.");
@@ -416,17 +417,17 @@
 			} else if (VISEND(c)) {
 				/* End of integer parsing mode */
 				tmpStr[strPos] = 0;
-				switch (pItem->itemType) {
+				switch (item->itemType) {
 				case ITEM_INT:
-					*((int *) pItem->itemData) = atoi(tmpStr);
+					*((int *) item->itemData) = atoi(tmpStr);
 					break;
 
 				case ITEM_UINT:
-					*((unsigned int *) pItem->itemData) = atol(tmpStr);
+					*((unsigned int *) item->itemData) = atol(tmpStr);
 					break;
 				}
-				if (pItem->itemValidate) {
-					if (!pItem->itemValidate(pItem))
+				if (item->itemValidate) {
+					if (!item->itemValidate(item))
 						validError = TRUE;
 				}
 
@@ -473,9 +474,9 @@
 				}
 
 				/* Set the value */
-				*((BOOL *) pItem->itemData) = tmpBool;
-				if (pItem->itemValidate) {
-					if (!pItem->itemValidate(pItem))
+				*((BOOL *) item->itemData) = tmpBool;
+				if (item->itemValidate) {
+					if (!item->itemValidate(item))
 						validError = TRUE;
 				}
 
@@ -506,9 +507,9 @@
 
 /* Write a configuration into file
  */
-int th_config_write(FILE * outFile, t_config * cfg)
+int th_config_write(FILE * outFile, th_config_t * cfg)
 {
-	t_config_item *pItem;
+	th_cfgitem_t *item;
 
 	if (!cfg)
 		return -1;
@@ -516,14 +517,14 @@
 	fprintf(outFile, "# Configuration written by %s %s\n\n", 
 		th_prog_fullname, th_prog_version);
 
-	pItem = cfg->pItems;
-	while (pItem) {
-		if (!pItem->itemData || ((pItem->itemType == ITEM_STRING) &&
-			*(char_t **) pItem->itemData == NULL)) {
+	item = cfg->items;
+	while (item) {
+		if (!item->itemData || ((item->itemType == ITEM_STRING) &&
+			*(char_t **) item->itemData == NULL)) {
 			
-			fprintf(outFile, "#%s = ", pItem->itemName);
+			fprintf(outFile, "#%s = ", item->itemName);
 			
-			switch (pItem->itemType) {
+			switch (item->itemType) {
 			case ITEM_STRING:
 				fprintf(outFile, "\"string\"");
 				break;
@@ -542,33 +543,33 @@
 			}
 			
 		} else {
-			fprintf(outFile, "%s = ", pItem->itemName);
+			fprintf(outFile, "%s = ", item->itemName);
 			
-			switch (pItem->itemType) {
+			switch (item->itemType) {
 			case ITEM_STRING:
 				fprintf(outFile, "\"%s\"",
-					*((char_t **) pItem->itemData));
+					*((char_t **) item->itemData));
 				break;
 
 			case ITEM_INT:
 				fprintf(outFile, "%i",
-					*((int *) pItem->itemData));
+					*((int *) item->itemData));
 				break;
 
 			case ITEM_UINT:
 				fprintf(outFile, "%d",
-					*((unsigned int *) pItem->itemData));
+					*((unsigned int *) item->itemData));
 				break;
 
 			case ITEM_BOOL:
 				fprintf(outFile, "%s",
-					*((BOOL *) pItem->itemData) ? "yes" : "no");
+					*((BOOL *) item->itemData) ? "yes" : "no");
 				break;
 			}
 		}
 
 		fprintf(outFile, "\n\n");
-		pItem = pItem->pNext;
+		item = item->next;
 	}
 
 	return 0;
--- a/th_config.h	Wed Mar 26 04:49:16 2008 +0200
+++ b/th_config.h	Wed Mar 26 05:03:16 2008 +0200
@@ -1,7 +1,7 @@
 /*
  * Very simple configuration file parsing functions
  * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2004-2007 Tecnic Software productions (TNSP)
+ * (C) Copyright 2004-2008 Tecnic Software productions (TNSP)
  *
  * Please read file 'COPYING' for information on license and distribution.
  */
@@ -27,34 +27,34 @@
 };
 
 
-typedef struct tconfignode {
+typedef struct _th_cfgitem_t {
 	char	*itemName;	/* Config item name */
 	int	itemType;	/* Type of the item */
 	void	*itemData;	/* Data / value */
 
-	BOOL	(*itemValidate)(struct tconfignode *);
+	BOOL	(*itemValidate)(struct _th_cfgitem_t *);
 
-	struct tconfignode *pNext, *pPrev;
-} t_config_item;
+	struct _th_cfgitem_t *next, *prev;
+} th_cfgitem_t;
 
 
 typedef struct {
-	t_config_item	*pItems;
-} t_config;
+	th_cfgitem_t	*items;
+} th_config_t;
 
 
 /*
  * Functions
  */
-t_config * th_config_new(void);
-int	th_config_read(char *, t_config *);
-void	th_config_free(t_config *);
-int	th_config_write(FILE *, t_config *);
+th_config_t * th_config_new(void);
+int	th_config_read(char *, th_config_t *);
+void	th_config_free(th_config_t *);
+int	th_config_write(FILE *, th_config_t *);
 
-int th_config_add_int(t_config *cfg, char *itemName, BOOL (*itemValidate)(t_config_item *), int *itemData, int itemDef);
-int th_config_add_uint(t_config *cfg, char *itemName, BOOL (*itemValidate)(t_config_item *), unsigned int *itemData, unsigned int itemDef);
-int th_config_add_str(t_config *cfg, char *itemName, BOOL (*itemValidate)(t_config_item *), char **itemData, char *itemDef);
-int th_config_add_bool(t_config *cfg, char *itemName, BOOL (*itemValidate)(t_config_item *), BOOL *itemData, BOOL itemDef);
+int th_config_add_int(th_config_t *cfg, char *itemName, BOOL (*itemValidate)(th_cfgitem_t *), int *itemData, int itemDef);
+int th_config_add_uint(th_config_t *cfg, char *itemName, BOOL (*itemValidate)(th_cfgitem_t *), unsigned int *itemData, unsigned int itemDef);
+int th_config_add_str(th_config_t *cfg, char *itemName, BOOL (*itemValidate)(th_cfgitem_t *), char **itemData, char *itemDef);
+int th_config_add_bool(th_config_t *cfg, char *itemName, BOOL (*itemValidate)(th_cfgitem_t *), BOOL *itemData, BOOL itemDef);
 
 #ifdef __cplusplus
 }