diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-08 20:19:05 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-08 20:19:05 +0000 |
| commit | da85e88878251f6a980fef95bd47109169a3cf7c (patch) | |
| tree | 83b8b61c4afad0f6ccbb7940176a03f3ad4d2dc5 /src/configFile.c | |
| parent | f8bc51b580a5b9a8445f38b614a87209c074143a (diff) | |
- hra si vytvara domovskt adresar
- moznost ulozit konfiguraciu
- lasserove streli boli lepsie posunute
git-svn-id: http://opensvn.csie.org/tuxanci_ng@12 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/configFile.c')
| -rw-r--r-- | src/configFile.c | 130 |
1 files changed, 108 insertions, 22 deletions
diff --git a/src/configFile.c b/src/configFile.c index 5bbbf97..1c19e3f 100644 --- a/src/configFile.c +++ b/src/configFile.c @@ -4,47 +4,133 @@ #include <string.h> #include <assert.h> +#include "main.h" #include "configFile.h" -char* getValueInConfigFile(textFile_t *p, char *s) +int isYesOrNO(char *s) { - int i; + if( s[0] == 'Y' || s[0] == 'y' )return 1; + return 0; +} + +char* getYesOrNo(int n) +{ + return ( n != 0 ? "YES" : "NO" ); +} + +int getValue(char *line, char *env, char *val, int len) +{ + char *offset_env; + char *offset_val_begin; + char *offset_val_end; + char clone_env[STR_SIZE]; + int val_len; - assert( s != NULL ); - assert( p != NULL ); + strcpy(clone_env, " "); + strcat(clone_env, env); - for( i = 0 ; i < p->text->count ; i++ ) - { - char *line; - char *offset; + offset_env = strstr(line, clone_env); + if( offset_env == NULL )return -1; + + offset_val_begin = strchr(offset_env, '"'); + if( offset_val_begin == NULL )return -1; + + offset_val_end = strchr(offset_val_begin+1, '"'); + if( offset_val_end == NULL )return -1; + + val_len = (int)(offset_val_end - ( offset_val_begin + 1) ); + if( val_len > len - 1 ) val_len = len - 1; + + memset(val, 0, len); + memcpy(val, offset_val_begin+1, val_len); + + return 0; +} + +char* setValue(char *line, char *env, char *val) +{ + char *offset_env; + char *offset_val_begin; + char *offset_val_end; + char clone_env[STR_SIZE]; + char clone[STR_SIZE]; + + //strcpy(clone_env, " "); + strcpy(clone_env, env); + + offset_env = strstr(line, clone_env); + if( offset_env == NULL )return NULL; + + offset_val_begin = strchr(offset_env, '"'); + if( offset_val_begin == NULL )return NULL; + + offset_val_end = strchr(offset_val_begin+1, '"'); + if( offset_val_end == NULL )return NULL; + + memset(clone, 0, STR_SIZE); + strncpy(clone, line, (int) ( (offset_val_begin - line) + 1 ) ); + strcat(clone, val); + strcat(clone, offset_val_end); - line = (char *) (p->text->list[i]); + return strdup( clone ); +} - if( ( offset = strstr(line, s) ) != NULL ) +int getValueInConfigFile(textFile_t *textFile, char *env, char *val, int len) +{ + int i; + char *line; + + for( i = 0 ; i < textFile->text->count ; i++ ) + { + line = (char *) (textFile->text->list[i]); + + if( strstr(line, env) != NULL ) { - return offset + ( strlen(s) + 1 ); + return getValue(line, env, val, len); } } - return NULL; + return -1; } -void delRemInConfigFile(textFile_t *p) +int setValueInConfigFile(textFile_t *textFile, char *env, char *val) { int i; + char *line; + char *ret; + + for( i = 0 ; i < textFile->text->count ; i++ ) + { + line = (char *) (textFile->text->list[i]); + + if( strstr(line, env) != NULL ) + { + ret = setValue(line, env, val); - assert( p != NULL ); + if( ret != NULL ) + { + delListItem(textFile->text, i, free); + insList(textFile->text, i, ret); - for( i = 0 ; i < p->text->count ; i++ ) + return 0; + } + } + } + + return -1; +} + +void loadValueFromConfigFile(textFile_t *textFile, char *env, char *val, int len, char *butVal) +{ + strcpy(val, butVal); + + if( getValueInConfigFile(textFile, env, val, len) != 0 ) { - char *line; - char *offset; + char line[STR_SIZE]; - line = (char *) (p->text->list[i]); + sprintf(line, " %s=\"%s\"", env, butVal); + addList(textFile->text, strdup(line) ); - if( ( offset = strchr(line, '#') ) != NULL ) - { - *offset = '\0'; - } + printf("Add line \"%s\" in config file.\n", line); } } |