wutil_config_store.cpp

00001 // wutil_config_store.cpp
00002 //
00003 // CConfigStore class implementation
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 
00025 #include "wgui_include_config.h"
00026 #include "wutil_config_store.h"
00027 #include "std_ex.h"
00028 #include "wutil_debug.h"
00029 #include <fstream>
00030 
00031 
00032 namespace wUtil
00033 {
00034 
00035 std::pair<bool, std::wstring> CConfigStore::GetStringEntry(std::wstring sKey) const
00036 {
00037      bool bSuccess = false;
00038      std::wstring sValue = L"";
00039      t_SettingsMap::const_iterator iter = m_SettingsMap.find(sKey);
00040      if (iter != m_SettingsMap.end())
00041      {
00042           bSuccess = true;
00043           sValue = iter->second;
00044      }
00045 
00046      return std::make_pair(bSuccess, sValue);
00047 }
00048 
00049 
00050 std::pair<bool, long int> CConfigStore::GetLongIntEntry(std::wstring sKey) const
00051 {
00052      bool bSuccess = false;
00053      long int lValue = 0;
00054      t_SettingsMap::const_iterator iter = m_SettingsMap.find(sKey);
00055      if (iter != m_SettingsMap.end())
00056      {
00057           bSuccess = true;
00058           lValue = stdex::atol(iter->second);
00059      }
00060 
00061      return std::make_pair(bSuccess, lValue);
00062 }
00063 
00064 
00065 std::pair<bool, double> CConfigStore::GetDoubleEntry(std::wstring sKey) const
00066 {
00067      bool bSuccess = false;
00068      double dValue = 0.0;
00069      t_SettingsMap::const_iterator iter = m_SettingsMap.find(sKey);
00070      if (iter != m_SettingsMap.end())
00071      {
00072           bSuccess = true;
00073           dValue = stdex::atod(iter->second);
00074      }
00075 
00076      return std::make_pair(bSuccess, dValue);
00077 }
00078 
00079 
00080 void CConfigStore::SetStringEntry(std::wstring sKey, std::wstring sValue)
00081 {
00082      m_SettingsMap[sKey] = sValue;
00083 }
00084 
00085 
00086 void CConfigStore::SetLongIntEntry(std::wstring sKey, long int lValue)
00087 {
00088      m_SettingsMap[sKey] = stdex::ltoa(lValue);
00089 }
00090 
00091 
00092 void CConfigStore::SetDoubleEntry(std::wstring sKey, double dValue)
00093 {
00094      m_SettingsMap[sKey] = stdex::dtoa(dValue);
00095 }
00096 
00097 
00098 void CConfigStore::RemoveEntry(std::wstring sKey)
00099 {
00100      m_SettingsMap.erase(sKey);
00101 }
00102 
00103 
00104 bool CConfigStore::EntryExists(std::wstring sKey) const
00105 {
00106      return (m_SettingsMap.find(sKey) != m_SettingsMap.end());
00107 }
00108 
00109 
00110 void CConfigStore::StoreToFile(std::wstring sFilename) const
00111 {
00112      std::wofstream File;
00113      File.open(stdex::ToMbString(sFilename).c_str(), std::ios::out | std::ios::trunc);
00114      
00115      if (File.is_open())
00116      {
00117           for (t_SettingsMap::const_iterator iter = m_SettingsMap.begin(); iter != m_SettingsMap.end(); ++iter)
00118           {
00119                File << iter->first << L" = " << iter->second << std::endl;
00120           }
00121           File.close();
00122      }
00123 }
00124 
00125 
00126 bool CConfigStore::ReadFromFile(std::wstring sFilename)
00127 {
00128      std::wifstream File;
00129      std::wstring sBuffer = L"";
00130      std::wstring sKey = L"";
00131      std::wstring sValue = L"";
00132      bool bSuccess = false;
00133 
00134      if (! sFilename.empty())
00135      {
00136           File.open(stdex::ToMbString(sFilename).c_str());
00137           if (File.is_open())
00138           {
00139                while (! File.eof())
00140                {
00141                     std::getline(File, sBuffer);
00142                     if (sBuffer[0] != '#')
00143                     {
00144                          std::wstring::size_type splitPoint = sBuffer.find_first_of(L"=");
00145                          sKey = stdex::TrimString(sBuffer.substr(0, splitPoint));
00146                          sValue = stdex::TrimString(sBuffer.substr(splitPoint + 1));
00147                          if (! sKey.empty())
00148                          {
00149                               m_SettingsMap[sKey] = sValue;
00150                          }
00151                     }
00152                }
00153                File.close();
00154                bSuccess = true;
00155           }
00156      }
00157 
00158      return bSuccess;
00159 }
00160 
00161 
00162 void CConfigStore::Clear(void)
00163 {
00164      m_SettingsMap.clear();
00165 }
00166 
00167 }
00168 

Generated on Wed May 16 23:11:26 2007 for wGui by  doxygen 1.5.1