Mercurial > hg > th-libs
view th_config.c @ 7:41885619fc79
Updates.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 Nov 2008 22:17:02 +0200 |
parents | 5a327a2988fa |
children | adcbcac66125 |
line wrap: on
line source
/* * Very simple configuration handling functions * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2004-2008 Tecnic Software productions (TNSP) * * Please read file 'COPYING' for information on license and distribution. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <stdio.h> #include <stdarg.h> #include "th_config.h" #include "th_util.h" #include "th_string.h" #define SET_MAX_BUF (8192) #define LPREV (pNode->prev) #define LNEXT (pNode->next) 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, filename, lineNum); vfprintf(stderr, pcFormat, ap); va_end(ap); } /* Create a new configuration */ th_config_t *th_config_new(void) { th_config_t *cfg; cfg = (th_config_t *) th_calloc(1, sizeof(th_config_t)); if (!cfg) return NULL; return cfg; } /* Free a given configuration (the values are not free'd) */ void th_config_free(th_config_t * cfg) { th_cfgitem_t *curr, *next; if (!cfg) return; curr = cfg->items; while (curr) { next = curr->next; th_free(curr->itemName); th_free(curr); curr = next; } } /* Allocate and add new item to configuration */ 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; if (!cfg) return NULL; /* Allocate new item */ pNode = (th_cfgitem_t *) th_calloc(1, sizeof(th_cfgitem_t)); if (!pNode) return NULL; /* Set values */ pNode->itemType = itemType; pNode->itemData = itemData; pNode->itemValidate = itemValidate; th_pstrcpy(&pNode->itemName, itemName); /* Insert into linked list */ 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->items = pNode; /* First node ... */ LPREV = pNode; /* ... it's also last */ LNEXT = NULL; /* But next is NULL! */ } return pNode; } /* Add integer type setting into give configuration */ int th_config_add_int(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *), int *itemData, int itemDef) { th_cfgitem_t *pNode; pNode = th_config_add(cfg, itemName, ITEM_INT, itemValidate, (void *) itemData); if (!pNode) return -1; *itemData = itemDef; return 0; } /* Add unsigned integer type setting into give configuration */ 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; pNode = th_config_add(cfg, itemName, ITEM_UINT, itemValidate, (void *) itemData); if (!pNode) return -1; *itemData = itemDef; return 0; } /* Add strint type setting into given configuration */ int th_config_add_str(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *), char ** itemData, char * itemDef) { th_cfgitem_t *pNode; pNode = th_config_add(cfg, itemName, ITEM_STRING, itemValidate, (void *) itemData); if (!pNode) return -1; if (itemDef != NULL) *itemData = strdup(itemDef); else *itemData = NULL; return 0; } /* Add boolean type setting into given configuration */ int th_config_add_bool(th_config_t * cfg, char * itemName, BOOL(*itemValidate) (th_cfgitem_t *), BOOL * itemData, BOOL itemDef) { th_cfgitem_t *pNode; pNode = th_config_add(cfg, itemName, ITEM_BOOL, itemValidate, (void *) itemData); if (!pNode) return -1; *itemData = itemDef; return 0; } /* Read a given file into configuration structure and variables */ enum { PM_EOF, PM_ERROR, PM_NORMAL, PM_COMMENT, PM_NEXT, PM_KEYNAME, PM_KEYSET, PM_STRING, PM_INT, PM_BOOL }; #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 * filename, th_config_t * cfg) { FILE *inFile; th_cfgitem_t *item; char tmpStr[SET_MAX_BUF + 1]; size_t lineNum, strPos; int c, parseMode, prevMode, nextMode, tmpCh; BOOL isFound, isStart, tmpBool, isError, validError; assert(cfg); /* Open the file */ if ((inFile = fopen(filename, "rb")) == NULL) return -1; /* Initialize values */ item = NULL; tmpCh = 0; strPos = 0; lineNum = 1; c = -1; isFound = isStart = tmpBool = isError = validError = FALSE; nextMode = prevMode = parseMode = PM_NORMAL; /* Parse the configuration */ while ((parseMode != PM_EOF) && (parseMode != PM_ERROR)) { if (c == -1) { /* Get next character */ switch (c = fgetc(inFile)) { case EOF: if (parseMode != PM_NORMAL) { th_config_error(filename, lineNum, "Unexpected end of file.\n"); parseMode = PM_ERROR; } else parseMode = PM_EOF; break; case '\n': lineNum++; } } switch (parseMode) { case PM_COMMENT: /* Comment parsing mode */ if (c == '\n') { /* End of line, end of comment */ parseMode = prevMode; prevMode = PM_COMMENT; } c = -1; break; case PM_NORMAL: /* Normal parsing mode */ if (c == '#') { prevMode = parseMode; parseMode = PM_COMMENT; c = -1; } else if (VISEND(c)) { c = -1; } else if (th_isalpha(c)) { /* Start of key name found */ prevMode = parseMode; parseMode = PM_KEYNAME; strPos = 0; } else { /* Error! Invalid character found */ th_config_error(filename, lineNum, "Unexpected character '%c'\n", c); parseMode = PM_ERROR; } break; case PM_KEYNAME: /* Configuration KEY name parsing mode */ if (c == '#') { /* Start of comment */ prevMode = parseMode; parseMode = PM_COMMENT; c = -1; } else if (th_iscrlf(c) || th_isspace(c) || c == '=') { /* End of key name */ prevMode = parseMode; parseMode = PM_NEXT; nextMode = PM_KEYSET; } else if (th_isalnum(c) || (c == '_')) { /* Add to key name string */ VADDCH(c) else { /* Error! Key name string too long! */ th_config_error(filename, lineNum, "Config key name too long!"); parseMode = PM_ERROR; } c = -1; } else { /* Error! Invalid character found */ th_config_error(filename, lineNum, "Unexpected character '%c'\n", c); parseMode = PM_ERROR; } break; case PM_KEYSET: if (c == '=') { /* Find key from configuration */ tmpStr[strPos] = 0; isFound = FALSE; item = cfg->items; while (item && !isFound) { if (strcmp(item->itemName, tmpStr) == 0) isFound = TRUE; else item = item->next; } /* Check if key was found */ if (isFound) { /* Okay, set next mode */ switch (item->itemType) { case ITEM_STRING: nextMode = PM_STRING; break; case ITEM_INT: case ITEM_UINT: nextMode = PM_INT; break; case ITEM_BOOL: nextMode = PM_BOOL; break; } prevMode = parseMode; parseMode = PM_NEXT; isStart = TRUE; strPos = 0; } else { /* Error! No configuration key by this name found */ th_config_error(filename, lineNum, "No such configuration setting ('%s')\n", tmpStr); parseMode = PM_ERROR; } c = -1; } else { /* Error! '=' expected! */ th_config_error(filename, lineNum, "Unexpected character '%c', '=' expected.\n", c); parseMode = PM_ERROR; } break; case PM_NEXT: /* Search next item parsing mode */ if (c == '#') { /* Start of comment */ prevMode = parseMode; parseMode = PM_COMMENT; } else if (th_isspace(c) || th_iscrlf(c)) { /* Ignore whitespaces and linechanges */ c = -1; } else { /* Next item found */ prevMode = parseMode; parseMode = nextMode; } break; case PM_STRING: /* String parsing mode */ if (isStart) { /* Start of string, get delimiter */ tmpCh = c; isStart = FALSE; strPos = 0; } else if (c == tmpCh) { /* End of string, set the value */ tmpStr[strPos] = 0; th_pstrcpy((char **) item->itemData, tmpStr); if (item->itemValidate) { if (!item->itemValidate(item)) validError = TRUE; } prevMode = parseMode; parseMode = PM_NORMAL; } else { /* Add character to string */ VADDCH(c) else { /* Error! String too long! */ th_config_error(filename, lineNum, "String too long! Maximum is %d characters.", SET_MAX_BUF); parseMode = PM_ERROR; } } c = -1; break; case PM_INT: /* Integer parsing mode */ if (isStart && (item->itemType == ITEM_UINT) && (c == '-')) { /* Error! Negative values not allowed for unsigned ints */ th_config_error(filename, lineNum, "Negative value specified, unsigned value expected."); parseMode = PM_ERROR; } else if (isStart && (c == '-' || c == '+')) { VADDCH(c) else isError = TRUE; } else if (th_isdigit(c)) { VADDCH(c) else isError = TRUE; } else if (VISEND(c)) { /* End of integer parsing mode */ tmpStr[strPos] = 0; switch (item->itemType) { case ITEM_INT: *((int *) item->itemData) = atoi(tmpStr); break; case ITEM_UINT: *((unsigned int *) item->itemData) = atol(tmpStr); break; } if (item->itemValidate) { if (!item->itemValidate(item)) validError = TRUE; } prevMode = parseMode; parseMode = PM_NORMAL; } else { /* Error! Unexpected character. */ th_config_error(filename, lineNum, "Unexpected character, ", SET_MAX_BUF); parseMode = PM_ERROR; } if (isError) { /* Error! String too long! */ th_config_error(filename, lineNum, "String too long! Maximum is %d characters.", SET_MAX_BUF); parseMode = PM_ERROR; } isStart = FALSE; c = -1; break; case PM_BOOL: /* Boolean parsing mode */ if (isStart) { tmpCh = c; isStart = FALSE; } else if (VISEND(c)) { /* End of boolean parsing */ switch (tmpCh) { case 'Y': case 'y': case 'T': case 't': case '1': tmpBool = TRUE; break; default: tmpBool = FALSE; break; } /* Set the value */ *((BOOL *) item->itemData) = tmpBool; if (item->itemValidate) { if (!item->itemValidate(item)) validError = TRUE; } prevMode = parseMode; parseMode = PM_NORMAL; } c = -1; break; } } /* Close files */ fclose(inFile); /* Check for validation errors */ if (validError) return 1; /* Return result */ if (parseMode == PM_ERROR) return -2; else return 0; } /* Write a configuration into file */ int th_config_write(FILE * outFile, th_config_t * cfg) { th_cfgitem_t *item; if (!cfg) return -1; fprintf(outFile, "# Configuration written by %s %s\n\n", th_prog_fullname, th_prog_version); item = cfg->items; while (item) { if (!item->itemData || ((item->itemType == ITEM_STRING) && *(char **) item->itemData == NULL)) { fprintf(outFile, "#%s = ", item->itemName); switch (item->itemType) { case ITEM_STRING: fprintf(outFile, "\"string\""); break; case ITEM_INT: fprintf(outFile, "int"); break; case ITEM_UINT: fprintf(outFile, "uint"); break; case ITEM_BOOL: fprintf(outFile, "boolean"); break; } } else { fprintf(outFile, "%s = ", item->itemName); switch (item->itemType) { case ITEM_STRING: fprintf(outFile, "\"%s\"", *((char **) item->itemData)); break; case ITEM_INT: fprintf(outFile, "%i", *((int *) item->itemData)); break; case ITEM_UINT: fprintf(outFile, "%d", *((unsigned int *) item->itemData)); break; case ITEM_BOOL: fprintf(outFile, "%s", *((BOOL *) item->itemData) ? "yes" : "no"); break; } } fprintf(outFile, "\n\n"); item = item->next; } return 0; }