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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 log = Log::getInstance();
31 int Config::init(char* takeFileName)
33 if (initted) return 1;
35 pthread_mutex_init(&fileLock, NULL);
37 if (strlen(takeFileName) > (MAX_FILENAME_LENGTH - 1))
39 log->log("Config", Log::DEBUG, "Config error: Config filename too long");
43 strcpy(fileName, takeFileName);
44 strcpy(fileNameTemp, takeFileName);
45 strcat(fileNameTemp, ".tmp");
47 file = fopen(fileName, "r");
50 file = fopen(fileName, "w");
53 log->log("Config", Log::DEBUG, "Config error: Could not access config file");
60 log->log("Config", Log::DEBUG, "Opened config file: %s", fileName);
70 int Config::shutdown()
72 if (!initted) return 1;
74 pthread_mutex_lock(&fileLock);
76 pthread_mutex_unlock(&fileLock);
77 pthread_mutex_destroy(&fileLock);
82 int Config::openFile()
84 if (!initted) return 0;
85 if (pthread_mutex_lock(&fileLock))
87 log->log("Config", Log::DEBUG, "Config error: Could not get lock");
92 log->log("Config", Log::DEBUG, "Config error: Initted 0 after lock");
93 pthread_mutex_unlock(&fileLock);
97 file = fopen(fileName, "r");
100 log->log("Config", Log::DEBUG, "Config error: Could not open config file");
101 pthread_mutex_unlock(&fileLock);
107 void Config::closeFile()
109 if (!initted) return;
113 pthread_mutex_unlock(&fileLock);
116 int Config::readLine()
118 if (!initted || !file) { log->log("Config", Log::DEBUG, "1"); return 0; }
119 if (!fgets(buffer, BUFFER_LENGTH-1, file)) { /*log->log("Config", Log::DEBUG, "2");*/ return 0; }
120 lastLineLength = strlen(buffer);
121 // log->log("Config", Log::DEBUG, "buffer before trim: '%s'", buffer);
123 // log->log("Config", Log::DEBUG, "buffer after trim: '%s'", buffer);
129 FILE* Config::copyToHere(long position)
131 FILE* newFile = fopen(fileNameTemp, "w");
133 if (!newFile) return NULL;
138 while (newPos < position)
140 if (!fgets(buffer, BUFFER_LENGTH-1, file)) break;
141 fputs(buffer, newFile);
142 newPos += strlen(buffer);
147 int Config::copyRest(FILE* newFile)
151 while(fgets(buffer, BUFFER_LENGTH-1, file))
153 fputs(buffer, newFile);
161 if (newFile) rename(fileNameTemp, fileName);
163 pthread_mutex_unlock(&fileLock);
167 int Config::deleteValue(const char* section, char* key)
169 if (!initted) return 0;
170 if (!openFile()) return 0;
172 if (!findSection(section))
175 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
181 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
185 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
187 if ( fgets(buffer, BUFFER_LENGTH-1, file) );
189 return copyRest(newFile);
192 int Config::setValueLong(const char* section, char* key, long newValue)
195 sprintf(longBuffer, "%li", newValue);
196 return setValueString(section, key, longBuffer);
199 int Config::setValueLongLong(char* section, char* key, long long newValue)
202 sprintf(longBuffer, "%lli", newValue);
203 return setValueString(section, key, longBuffer);
206 int Config::setValueDouble(char* section, char* key, double newValue)
208 char doubleBuffer[50];
209 sprintf(doubleBuffer, "%f", newValue);
210 return setValueString(section, key, doubleBuffer);
213 int Config::setValueString(const char* section, const char* key, const char* newValue)
215 if (!initted) return 0;
216 if (!openFile()) return 0;
218 if (findSection(section))
222 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
226 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
230 if ( fgets(buffer, BUFFER_LENGTH-1, file) );
231 fprintf(newFile, "%s = %s\n", key, newValue);
232 return copyRest(newFile);
237 findSection(section);
238 FILE* newFile = copyToHere(ftell(file));
242 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
246 fprintf(newFile, "%s = %s\n", key, newValue);
247 return copyRest(newFile);
253 fseek(file, 0, SEEK_END);
254 FILE* newFile = copyToHere(ftell(file));
258 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
262 fprintf(newFile, "[%s]\n%s = %s\n", section, key, newValue);
263 return copyRest(newFile);
267 char* Config::getSectionKeyNames(const char* section, int& numberOfReturns, int& allKeysSize)
271 char* allKeys = NULL;
272 int allKeysIndex = 0;
276 if (!initted) return NULL;
277 if (!openFile()) return NULL;
278 if (!findSection(section)) return NULL;
280 char foundKey[BUFFER_LENGTH];
284 // Is this line a section header? if so, exit
285 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) break;
287 equalspos = strstr(buffer, "=");
288 if (!equalspos) continue; // if there is no = then it's not a key
289 memcpy(foundKey, buffer, equalspos-buffer);
290 foundKey[equalspos-buffer] = '\0';
292 keyLength = strlen(foundKey);
293 allKeysSize += keyLength + 1;
294 allKeys = (char*)realloc(allKeys, allKeysSize);
295 memcpy(&allKeys[allKeysIndex], foundKey, keyLength);
296 allKeysIndex += keyLength;
297 allKeys[allKeysIndex] = '\0';
309 int Config::findSection(const char* section)
311 if (!initted || !file) return 0;
312 if (strlen(section) > (BUFFER_LENGTH-2))
314 log->log("Config", Log::DEBUG, "Config error: Section given exceeds max length");
318 char toFind[BUFFER_LENGTH];
321 strcat(toFind, section);
326 // log->log("Config", Log::DEBUG, "to find '%s' this line '%s'", toFind, buffer);
327 if (!strcmp(toFind, buffer)) return 1;
332 int Config::findKey(const char* key)
334 if (!initted || !file) return 0;
336 if (strlen(key) > (BUFFER_LENGTH-1))
338 log->log("Config", Log::DEBUG, "Config error: Key given exceeds max length");
342 char prepForTest[BUFFER_LENGTH];
344 // do a rough search first, this could match substrings that we don't want
347 // Is this line a section header? if so, exit
348 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) return 0;
349 if (strstr(buffer, key))
351 // rough search found match
352 char* equalspos = strstr(buffer, "=");
353 if (!equalspos) continue;
354 memcpy(prepForTest, buffer, equalspos-buffer);
355 prepForTest[equalspos-buffer] = '\0';
358 if (!strcmp(key, prepForTest))
360 // in buffer, set all up to equals to space, then trim!
361 for(char* curPos = buffer; curPos <= equalspos; curPos++)
373 char* Config::getValueString(const char* section, const char* key)
375 if (!initted) return NULL;
376 if (!openFile()) return NULL;
378 if (!findSection(section))
381 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
387 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
391 char* returnString = new char[strlen(buffer)+1];
392 strcpy(returnString, buffer);
399 long Config::getValueLong(const char* section, const char* key, int* failure)
402 if (!initted) return 0;
403 if (!openFile()) return 0;
405 if (!findSection(section))
408 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
414 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
420 long retVal = strtol(buffer, &check, 10);
421 if ((retVal == 0) && (check == buffer)) *failure = 1;
427 long long Config::getValueLongLong(char* section, char* key, int* failure)
430 if (!initted) return 0;
431 if (!openFile()) return 0;
433 if (!findSection(section))
436 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
442 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
448 long long retVal = strtoll(buffer, &check, 10);
449 if ((retVal == 0) && (check == buffer)) *failure = 1;
455 double Config::getValueDouble(char* section, char* key, int* failure)
458 if (!initted) return 0;
459 if (!openFile()) return 0;
461 if (!findSection(section))
464 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
470 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
477 double retVal = strtod(buffer, &check);
478 if ((retVal == 0) && (check == buffer)) *failure = 1;
487 void Config::trim(char* str)
489 int pos, len, start, end;
493 for(pos = 0; pos < len; pos++)
495 if ((str[pos] == '#') || (str[pos] == ';'))
497 // Mod. If #/; is at start of line ok. Else, if it is after a space, ok.
499 if ((pos == 0) || (isspace(str[pos - 1])))
513 while(isspace(str[start])) start++;
514 while(isspace(str[end-1]))
523 for(pos = start; pos < end; pos++) str[pos - start] = str[pos];
524 str[end - start] = '\0';