Subversion Repositories HomeAutomation

Rev

Rev 976 | Go to most recent revision | Blame | Compare with Previous | 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 <string>
  23.  
  24.  
  25. #include "settings.h"
  26.  
  27. string Settings::myFilename;
  28. map<string, string> Settings::mySettings;
  29.  
  30. bool Settings::read(string filename)
  31. {
  32.     SyslogStream &slog = SyslogStream::getInstance();
  33.  
  34.     myFilename = filename;
  35.  
  36.     ifstream file(myFilename.c_str());
  37.  
  38.     if (!file)
  39.     {
  40.         slog << "Could not find settings file: " << myFilename << "\n";
  41.         return false;
  42.     }
  43.  
  44.     slog << "Loading settings from " << filename << ".\n";
  45.  
  46.     while (!file.eof())
  47.     {
  48.         string line;
  49.         getline(file, line);
  50.  
  51.         trim(line);
  52.  
  53.         if (line.length() == 0)
  54.             continue;
  55.  
  56.         vector<string> parts = explode("=", line);
  57.  
  58.         if (parts.size() < 2)
  59.             continue;
  60.  
  61.         add(trim(parts[0]), trim(parts[1]));
  62.     }
  63.  
  64.     file.close();
  65.  
  66.     return true;
  67. }
  68.  
  69. void Settings::add(string name, string value)
  70. {
  71.     mySettings[name] = value;
  72. }
  73.  
  74. string Settings::get(string name)
  75. {
  76.     map<string, string>::const_iterator iterator = mySettings.find(name);
  77.  
  78.     if (iterator == mySettings.end())
  79.         return NULL;
  80.  
  81.     return iterator->second;
  82. }
  83.