Rev 1024 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 969 | runge | 1 | /*************************************************************************** |
| 2 | * Copyright (C) November 27, 2008 by Mattias Runge * |
||
| 3 | * mattias@runge.se * |
||
| 4 | * settings.cpp * |
||
| 5 | * * |
||
| 6 | * This program is free software; you can redistribute it and/or modify * |
||
| 7 | * it under the terms of the GNU General Public License as published by * |
||
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
||
| 9 | * (at your option) any later version. * |
||
| 10 | * * |
||
| 11 | * This program is distributed in the hope that it will be useful, * |
||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
| 14 | * GNU General Public License for more details. * |
||
| 15 | * * |
||
| 16 | * You should have received a copy of the GNU General Public License * |
||
| 17 | * along with this program; if not, write to the * |
||
| 18 | * Free Software Foundation, Inc., * |
||
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
| 20 | ***************************************************************************/ |
||
| 21 | |||
| 22 | #include "settings.h" |
||
| 23 | |||
| 24 | string Settings::myFilename; |
||
| 25 | map<string, string> Settings::mySettings; |
||
| 1024 | runge | 26 | Mutex Settings::myMutex; |
| 969 | runge | 27 | |
| 28 | bool Settings::read(string filename) |
||
| 29 | { |
||
| 1024 | runge | 30 | myMutex.lock(); |
| 969 | runge | 31 | |
| 1024 | runge | 32 | Logger &log = Logger::getInstance(); |
| 33 | |||
| 969 | runge | 34 | myFilename = filename; |
| 35 | |||
| 36 | ifstream file(myFilename.c_str()); |
||
| 37 | |||
| 38 | if (!file) |
||
| 39 | { |
||
| 1024 | runge | 40 | log.add("Could not find settings file: " + myFilename + ".\n"); |
| 41 | myMutex.unlock(); |
||
| 969 | runge | 42 | return false; |
| 43 | } |
||
| 44 | |||
| 1024 | runge | 45 | log.add("Loading settings from " + filename + ".\n"); |
| 969 | runge | 46 | |
| 47 | while (!file.eof()) |
||
| 48 | { |
||
| 49 | string line; |
||
| 50 | getline(file, line); |
||
| 51 | |||
| 52 | trim(line); |
||
| 53 | |||
| 54 | if (line.length() == 0) |
||
| 55 | continue; |
||
| 56 | |||
| 57 | vector<string> parts = explode("=", line); |
||
| 58 | |||
| 59 | if (parts.size() < 2) |
||
| 60 | continue; |
||
| 61 | |||
| 62 | add(trim(parts[0]), trim(parts[1])); |
||
| 63 | } |
||
| 64 | |||
| 65 | file.close(); |
||
| 66 | |||
| 1024 | runge | 67 | myMutex.unlock(); |
| 969 | runge | 68 | return true; |
| 69 | } |
||
| 70 | |||
| 71 | void Settings::add(string name, string value) |
||
| 72 | { |
||
| 73 | mySettings[name] = value; |
||
| 74 | } |
||
| 75 | |||
| 76 | string Settings::get(string name) |
||
| 77 | { |
||
| 1024 | runge | 78 | myMutex.lock(); |
| 79 | |||
| 969 | runge | 80 | map<string, string>::const_iterator iterator = mySettings.find(name); |
| 81 | |||
| 1024 | runge | 82 | string value = ""; |
| 969 | runge | 83 | |
| 1024 | runge | 84 | if (iterator != mySettings.end()) |
| 85 | { |
||
| 86 | value = iterator->second; |
||
| 87 | } |
||
| 88 | |||
| 89 | myMutex.unlock(); |
||
| 90 | |||
| 91 | return value; |
||
| 969 | runge | 92 | } |