Include and link physfs properly.
[tuxanci.git] / src / client / configFile.c
blob9d6d65bf628c33e3233bdfad1c75c259db1d8877
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
6 #include "main.h"
7 #include "configFile.h"
9 int isYesOrNO(char *s)
11 if (s[0] == 'Y' || s[0] == 'y') {
12 return 1;
13 } else {
14 return 0;
18 char *getYesOrNo(int n)
20 return (n != 0 ? "YES" : "NO");
23 int getValue(char *line, char *env, char *val, int len)
25 char *offset_env;
26 char *offset_val_begin;
27 char *offset_val_end;
28 char clone_env[STR_SIZE];
29 int val_len;
31 strcpy(clone_env, env);
33 offset_env = strstr(line, clone_env);
35 if (offset_env == NULL) {
36 return -1;
39 offset_val_begin = strchr(offset_env, '"');
41 if (offset_val_begin == NULL) {
42 return -1;
45 offset_val_end = strchr(offset_val_begin + 1, '"');
47 if (offset_val_end == NULL) {
48 return -1;
51 val_len = (int) (offset_val_end - (offset_val_begin + 1));
53 if (val_len > len - 1) {
54 val_len = len - 1;
57 memset(val, 0, len);
58 memcpy(val, offset_val_begin + 1, val_len);
60 return 0;
63 char *setValue(char *line, char *env, char *val)
65 char *offset_env;
66 char *offset_val_begin;
67 char *offset_val_end;
68 char clone_env[STR_SIZE];
69 char clone[STR_SIZE];
71 /*strcpy(clone_env, " ");*/
72 strcpy(clone_env, env);
74 offset_env = strstr(line, clone_env);
76 if (offset_env == NULL) {
77 return NULL;
80 offset_val_begin = strchr(offset_env, '"');
82 if (offset_val_begin == NULL) {
83 return NULL;
86 offset_val_end = strchr(offset_val_begin + 1, '"');
88 if (offset_val_end == NULL) {
89 return NULL;
92 memset(clone, 0, STR_SIZE);
93 strncpy(clone, line, (int) ((offset_val_begin - line) + 1));
94 strcat(clone, val);
95 strcat(clone, offset_val_end);
97 return strdup(clone);
100 int getValueInConfigFile(textFile_t * textFile, char *env, char *val, int len)
102 int i;
103 char *line;
105 for (i = 0; i < textFile->text->count; i++) {
106 line = (char *) (textFile->text->list[i]);
108 if (strncmp(line, env, strlen(env)) == 0) {
109 return getValue(line, env, val, len);
113 return -1;
116 int setValueInConfigFile(textFile_t * textFile, char *env, char *val)
118 int i;
119 char *line;
120 char *ret;
122 for (i = 0; i < textFile->text->count; i++) {
123 line = (char *) (textFile->text->list[i]);
125 if (strncmp(line, env, strlen(env)) == 0) {
126 ret = setValue(line, env, val);
128 if (ret != NULL) {
129 list_del_item(textFile->text, i, free);
130 list_ins(textFile->text, i, ret);
132 return 0;
137 return -1;
140 void loadValueFromConfigFile(textFile_t * textFile, char *env, char *val, int len, char *butVal)
142 strcpy(val, butVal);
144 if (getValueInConfigFile(textFile, env, val, len) != 0) {
145 char line[STR_SIZE];
147 sprintf(line, "%s=\"%s\"", env, butVal);
148 list_add(textFile->text, strdup(line));
150 debug("Expanding config file [%s]", line);