2 Copyright 2004-2005 Chris Tallon
3 Copyright 2004-2005 University Of Bradford
5 This file is part of VOMP.
7 VOMP is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 VOMP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with VOMP; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 int Config::init(char* takeFileName)
32 if (initted) return 1;
34 pthread_mutex_init(&fileLock, NULL);
36 if (strlen(takeFileName) > (MAX_FILENAME_LENGTH - 1))
38 printf("Config error: Config filename too long\n");
42 strcpy(fileName, takeFileName);
43 strcpy(fileNameTemp, takeFileName);
44 strcat(fileNameTemp, ".tmp");
46 file = fopen(fileName, "r");
49 file = fopen(fileName, "w");
52 printf("Config error: Could not access config file\n");
63 int Config::shutdown()
65 if (!initted) return 1;
67 pthread_mutex_lock(&fileLock);
69 pthread_mutex_unlock(&fileLock);
70 pthread_mutex_destroy(&fileLock);
75 int Config::openFile()
77 if (!initted) return 0;
78 if (pthread_mutex_lock(&fileLock))
80 printf("Config error: Could not get lock\n");
85 printf("Config error: Initted 0 after lock\n");
86 pthread_mutex_unlock(&fileLock);
90 file = fopen(fileName, "r");
93 printf("Config error: Could not open config file\n");
94 pthread_mutex_unlock(&fileLock);
100 void Config::closeFile()
102 if (!initted) return;
106 pthread_mutex_unlock(&fileLock);
109 int Config::readLine()
111 if (!initted || !file) { printf("1\n"); return 0; }
112 if (!fgets(buffer, BUFFER_LENGTH-1, file)) { printf("2\n"); return 0; }
113 lastLineLength = strlen(buffer);
114 printf("buffer before trim: '%s'\n", buffer);
116 printf("buffer after trim: '%s'\n", buffer);
122 FILE* Config::copyToHere(long position)
124 FILE* newFile = fopen(fileNameTemp, "w");
126 if (!newFile) return NULL;
131 while (newPos < position)
133 fgets(buffer, BUFFER_LENGTH-1, file);
134 fputs(buffer, newFile);
135 newPos += strlen(buffer);
140 int Config::copyRest(FILE* newFile)
144 while(fgets(buffer, BUFFER_LENGTH-1, file))
146 fputs(buffer, newFile);
154 if (newFile) rename(fileNameTemp, fileName);
156 pthread_mutex_unlock(&fileLock);
160 int Config::deleteValue(char* section, char* key)
162 if (!initted) return 0;
163 if (!openFile()) return 0;
165 if (!findSection(section))
168 printf("Config error: Section %s not found\n", section);
174 printf("Config error: Key %s not found\n", key);
178 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
179 fgets(buffer, BUFFER_LENGTH-1, file);
181 return copyRest(newFile);
184 int Config::setValueLong(char* section, char* key, long newValue)
187 sprintf(longBuffer, "%li", newValue);
188 return setValueString(section, key, longBuffer);
191 int Config::setValueLongLong(char* section, char* key, long long newValue)
194 sprintf(longBuffer, "%lli", newValue);
195 return setValueString(section, key, longBuffer);
198 int Config::setValueDouble(char* section, char* key, double newValue)
200 char doubleBuffer[50];
201 sprintf(doubleBuffer, "%f", newValue);
202 return setValueString(section, key, doubleBuffer);
205 int Config::setValueString(char* section, char* key, char* newValue)
207 if (!initted) return 0;
208 if (!openFile()) return 0;
210 if (findSection(section))
214 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
218 printf("Config error: Could not write temp config file\n");
222 fgets(buffer, BUFFER_LENGTH-1, file);
223 fprintf(newFile, "%s = %s\n", key, newValue);
224 return copyRest(newFile);
229 findSection(section);
230 FILE* newFile = copyToHere(ftell(file));
234 printf("Config error: Could not write temp config file\n");
238 fprintf(newFile, "%s = %s\n", key, newValue);
239 return copyRest(newFile);
245 fseek(file, 0, SEEK_END);
246 FILE* newFile = copyToHere(ftell(file));
250 printf("Config error: Could not write temp config file\n");
254 fprintf(newFile, "[%s]\n%s = %s\n", section, key, newValue);
255 return copyRest(newFile);
259 char* Config::getSectionKeyNames(char* section, int& numberOfReturns, int& allKeysSize)
263 char* allKeys = NULL;
264 int allKeysIndex = 0;
268 if (!initted) return NULL;
269 if (!openFile()) return NULL;
270 if (!findSection(section)) return NULL;
272 char foundKey[BUFFER_LENGTH];
276 // Is this line a section header? if so, exit
277 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) break;
279 equalspos = strstr(buffer, "=");
280 if (!equalspos) continue; // if there is no = then it's not a key
281 memcpy(foundKey, buffer, equalspos-buffer);
282 foundKey[equalspos-buffer] = '\0';
284 keyLength = strlen(foundKey);
285 allKeysSize += keyLength + 1;
286 allKeys = (char*)realloc(allKeys, allKeysSize);
287 memcpy(&allKeys[allKeysIndex], foundKey, keyLength);
288 allKeysIndex += keyLength;
289 allKeys[allKeysIndex] = '\0';
301 int Config::findSection(char* section)
303 if (!initted || !file) return 0;
304 if (strlen(section) > (BUFFER_LENGTH-2))
306 printf("Config error: Section given exceeds max length\n");
310 char toFind[BUFFER_LENGTH];
313 strcat(toFind, section);
318 printf("to find '%s' this line '%s'\n", toFind, buffer);
319 if (!strcmp(toFind, buffer)) return 1;
324 int Config::findKey(char* key)
326 if (!initted || !file) return 0;
328 if (strlen(key) > (BUFFER_LENGTH-1))
330 printf("Config error: Key given exceeds max length\n");
334 char prepForTest[BUFFER_LENGTH];
336 // do a rough search first, this could match substrings that we don't want
339 // Is this line a section header? if so, exit
340 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) return 0;
341 if (strstr(buffer, key))
343 // rough search found match
344 char* equalspos = strstr(buffer, "=");
345 if (!equalspos) continue;
346 memcpy(prepForTest, buffer, equalspos-buffer);
347 prepForTest[equalspos-buffer] = '\0';
350 if (!strcmp(key, prepForTest))
352 // in buffer, set all up to equals to space, then trim!
353 for(char* curPos = buffer; curPos <= equalspos; curPos++)
365 char* Config::getValueString(char* section, char* key)
367 if (!initted) return NULL;
368 if (!openFile()) return NULL;
370 if (!findSection(section))
373 printf("Config error: Section %s not found\n", section);
379 printf("Config error: Key %s not found\n", key);
383 char* returnString = new char[strlen(buffer)+1];
384 strcpy(returnString, buffer);
391 long Config::getValueLong(char* section, char* key, int* failure)
394 if (!initted) return 0;
395 if (!openFile()) return 0;
397 if (!findSection(section))
400 printf("Config error: Section %s not found\n", section);
406 printf("Config error: Key %s not found\n", key);
412 long retVal = strtol(buffer, &check, 10);
413 if ((retVal == 0) && (check == buffer)) *failure = 1;
419 long long Config::getValueLongLong(char* section, char* key, int* failure)
422 if (!initted) return 0;
423 if (!openFile()) return 0;
425 if (!findSection(section))
428 printf("Config error: Section %s not found\n", section);
434 printf("Config error: Key %s not found\n", key);
440 long long retVal = strtoll(buffer, &check, 10);
441 if ((retVal == 0) && (check == buffer)) *failure = 1;
447 double Config::getValueDouble(char* section, char* key, int* failure)
450 if (!initted) return 0;
451 if (!openFile()) return 0;
453 if (!findSection(section))
456 printf("Config error: Section %s not found\n", section);
462 printf("Config error: Key %s not found\n", key);
469 double retVal = strtod(buffer, &check);
470 if ((retVal == 0) && (check == buffer)) *failure = 1;
479 void Config::trim(char* str)
481 int pos, len, start, end;
485 for(pos = 0; pos < len; pos++)
487 if ((str[pos] == '#') || (str[pos] == ';'))
489 // Mod. If #/; is at start of line ok. Else, if it is after a space, ok.
491 if ((pos == 0) || (isspace(str[pos - 1])))
505 while(isspace(str[start])) start++;
506 while(isspace(str[end-1]))
515 for(pos = start; pos < end; pos++) str[pos - start] = str[pos];
516 str[end - start] = '\0';