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
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");
64 int Config::shutdown()
66 if (!initted) return 1;
68 pthread_mutex_lock(&fileLock);
70 pthread_mutex_unlock(&fileLock);
71 pthread_mutex_destroy(&fileLock);
76 int Config::openFile()
78 if (!initted) return 0;
79 if (pthread_mutex_lock(&fileLock))
81 log->log("Config", Log::DEBUG, "Config error: Could not get lock");
86 log->log("Config", Log::DEBUG, "Config error: Initted 0 after lock");
87 pthread_mutex_unlock(&fileLock);
91 file = fopen(fileName, "r");
94 log->log("Config", Log::DEBUG, "Config error: Could not open config file");
95 pthread_mutex_unlock(&fileLock);
101 void Config::closeFile()
103 if (!initted) return;
107 pthread_mutex_unlock(&fileLock);
110 int Config::readLine()
112 if (!initted || !file) { log->log("Config", Log::DEBUG, "1"); return 0; }
113 if (!fgets(buffer, BUFFER_LENGTH-1, file)) { log->log("Config", Log::DEBUG, "2"); return 0; }
114 lastLineLength = strlen(buffer);
115 log->log("Config", Log::DEBUG, "buffer before trim: '%s'", buffer);
117 log->log("Config", Log::DEBUG, "buffer after trim: '%s'", buffer);
123 FILE* Config::copyToHere(long position)
125 FILE* newFile = fopen(fileNameTemp, "w");
127 if (!newFile) return NULL;
132 while (newPos < position)
134 fgets(buffer, BUFFER_LENGTH-1, file);
135 fputs(buffer, newFile);
136 newPos += strlen(buffer);
141 int Config::copyRest(FILE* newFile)
145 while(fgets(buffer, BUFFER_LENGTH-1, file))
147 fputs(buffer, newFile);
155 if (newFile) rename(fileNameTemp, fileName);
157 pthread_mutex_unlock(&fileLock);
161 int Config::deleteValue(char* section, char* key)
163 if (!initted) return 0;
164 if (!openFile()) return 0;
166 if (!findSection(section))
169 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
175 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
179 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
180 fgets(buffer, BUFFER_LENGTH-1, file);
182 return copyRest(newFile);
185 int Config::setValueLong(char* section, char* key, long newValue)
188 sprintf(longBuffer, "%li", newValue);
189 return setValueString(section, key, longBuffer);
192 int Config::setValueLongLong(char* section, char* key, long long newValue)
195 sprintf(longBuffer, "%lli", newValue);
196 return setValueString(section, key, longBuffer);
199 int Config::setValueDouble(char* section, char* key, double newValue)
201 char doubleBuffer[50];
202 sprintf(doubleBuffer, "%f", newValue);
203 return setValueString(section, key, doubleBuffer);
206 int Config::setValueString(char* section, char* key, char* newValue)
208 if (!initted) return 0;
209 if (!openFile()) return 0;
211 if (findSection(section))
215 FILE* newFile = copyToHere(ftell(file) - lastLineLength);
219 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
223 fgets(buffer, BUFFER_LENGTH-1, file);
224 fprintf(newFile, "%s = %s\n", key, newValue);
225 return copyRest(newFile);
230 findSection(section);
231 FILE* newFile = copyToHere(ftell(file));
235 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
239 fprintf(newFile, "%s = %s\n", key, newValue);
240 return copyRest(newFile);
246 fseek(file, 0, SEEK_END);
247 FILE* newFile = copyToHere(ftell(file));
251 log->log("Config", Log::DEBUG, "Config error: Could not write temp config file");
255 fprintf(newFile, "[%s]\n%s = %s\n", section, key, newValue);
256 return copyRest(newFile);
260 char* Config::getSectionKeyNames(char* section, int& numberOfReturns, int& allKeysSize)
264 char* allKeys = NULL;
265 int allKeysIndex = 0;
269 if (!initted) return NULL;
270 if (!openFile()) return NULL;
271 if (!findSection(section)) return NULL;
273 char foundKey[BUFFER_LENGTH];
277 // Is this line a section header? if so, exit
278 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) break;
280 equalspos = strstr(buffer, "=");
281 if (!equalspos) continue; // if there is no = then it's not a key
282 memcpy(foundKey, buffer, equalspos-buffer);
283 foundKey[equalspos-buffer] = '\0';
285 keyLength = strlen(foundKey);
286 allKeysSize += keyLength + 1;
287 allKeys = (char*)realloc(allKeys, allKeysSize);
288 memcpy(&allKeys[allKeysIndex], foundKey, keyLength);
289 allKeysIndex += keyLength;
290 allKeys[allKeysIndex] = '\0';
302 int Config::findSection(char* section)
304 if (!initted || !file) return 0;
305 if (strlen(section) > (BUFFER_LENGTH-2))
307 log->log("Config", Log::DEBUG, "Config error: Section given exceeds max length");
311 char toFind[BUFFER_LENGTH];
314 strcat(toFind, section);
319 log->log("Config", Log::DEBUG, "to find '%s' this line '%s'", toFind, buffer);
320 if (!strcmp(toFind, buffer)) return 1;
325 int Config::findKey(char* key)
327 if (!initted || !file) return 0;
329 if (strlen(key) > (BUFFER_LENGTH-1))
331 log->log("Config", Log::DEBUG, "Config error: Key given exceeds max length");
335 char prepForTest[BUFFER_LENGTH];
337 // do a rough search first, this could match substrings that we don't want
340 // Is this line a section header? if so, exit
341 if ((buffer[0] == '[') && (buffer[strlen(buffer)-1] == ']')) return 0;
342 if (strstr(buffer, key))
344 // rough search found match
345 char* equalspos = strstr(buffer, "=");
346 if (!equalspos) continue;
347 memcpy(prepForTest, buffer, equalspos-buffer);
348 prepForTest[equalspos-buffer] = '\0';
351 if (!strcmp(key, prepForTest))
353 // in buffer, set all up to equals to space, then trim!
354 for(char* curPos = buffer; curPos <= equalspos; curPos++)
366 char* Config::getValueString(char* section, char* key)
368 if (!initted) return NULL;
369 if (!openFile()) return NULL;
371 if (!findSection(section))
374 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
380 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
384 char* returnString = new char[strlen(buffer)+1];
385 strcpy(returnString, buffer);
392 long Config::getValueLong(char* section, char* key, int* failure)
395 if (!initted) return 0;
396 if (!openFile()) return 0;
398 if (!findSection(section))
401 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
407 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
413 long retVal = strtol(buffer, &check, 10);
414 if ((retVal == 0) && (check == buffer)) *failure = 1;
420 long long Config::getValueLongLong(char* section, char* key, int* failure)
423 if (!initted) return 0;
424 if (!openFile()) return 0;
426 if (!findSection(section))
429 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
435 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
441 long long retVal = strtoll(buffer, &check, 10);
442 if ((retVal == 0) && (check == buffer)) *failure = 1;
448 double Config::getValueDouble(char* section, char* key, int* failure)
451 if (!initted) return 0;
452 if (!openFile()) return 0;
454 if (!findSection(section))
457 log->log("Config", Log::DEBUG, "Config error: Section %s not found", section);
463 log->log("Config", Log::DEBUG, "Config error: Key %s not found", key);
470 double retVal = strtod(buffer, &check);
471 if ((retVal == 0) && (check == buffer)) *failure = 1;
480 void Config::trim(char* str)
482 int pos, len, start, end;
486 for(pos = 0; pos < len; pos++)
488 if ((str[pos] == '#') || (str[pos] == ';'))
490 // Mod. If #/; is at start of line ok. Else, if it is after a space, ok.
492 if ((pos == 0) || (isspace(str[pos - 1])))
506 while(isspace(str[start])) start++;
507 while(isspace(str[end-1]))
516 for(pos = start; pos < end; pos++) str[pos - start] = str[pos];
517 str[end - start] = '\0';