Subversion Repositories HomeAutomation

Rev

Rev 985 | Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  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;
  26. Mutex Settings::myMutex;
  27.  
  28. bool Settings::read(string filename)
  29. {
  30.     myMutex.lock();
  31.  
  32.     Logger &log = Logger::getInstance();
  33.  
  34.     myFilename = filename;
  35.  
  36.     ifstream file(myFilename.c_str());
  37.  
  38.     if (!file)
  39.     {
  40.         log.add("Could not find settings file: " + myFilename + ".\n");
  41.         myMutex.unlock();
  42.         return false;
  43.     }
  44.  
  45.     log.add("Loading settings from " + filename + ".\n");
  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.  
  67.     myMutex.unlock();
  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. {
  78.     myMutex.lock();
  79.  
  80.     map<string, string>::const_iterator iterator = mySettings.find(name);
  81.  
  82.     string value = "";
  83.  
  84.     if (iterator != mySettings.end())
  85.     {
  86.         value = iterator->second;
  87.     }
  88.  
  89.     myMutex.unlock();
  90.  
  91.     return value;
  92. }
  93.