diff src/gtk/configfile.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gtk/configfile.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,204 @@
     1.4 +// -*- C++ -*-
     1.5 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
     1.6 +// Copyright (C) 1999-2003 Forgotten
     1.7 +// Copyright (C) 2004 Forgotten and the VBA development team
     1.8 +
     1.9 +// This program is free software; you can redistribute it and/or modify
    1.10 +// it under the terms of the GNU General Public License as published by
    1.11 +// the Free Software Foundation; either version 2, or(at your option)
    1.12 +// any later version.
    1.13 +//
    1.14 +// This program is distributed in the hope that it will be useful,
    1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +// GNU General Public License for more details.
    1.18 +//
    1.19 +// You should have received a copy of the GNU General Public License
    1.20 +// along with this program; if not, write to the Free Software Foundation,
    1.21 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.22 +
    1.23 +#ifndef __VBA_CONFIGFILE_H__
    1.24 +#define __VBA_CONFIGFILE_H__
    1.25 +
    1.26 +#include <list>
    1.27 +#include <string>
    1.28 +#include <sstream>
    1.29 +#include <ostream>
    1.30 +
    1.31 +namespace VBA
    1.32 +{
    1.33 +namespace Config
    1.34 +{
    1.35 +
    1.36 +class NotFound
    1.37 +{
    1.38 +public:
    1.39 +  virtual ~NotFound() {}
    1.40 +
    1.41 +protected:
    1.42 +  NotFound() {}
    1.43 +};
    1.44 +
    1.45 +class SectionNotFound : public NotFound
    1.46 +{
    1.47 +public:
    1.48 +  SectionNotFound(const std::string & _rsName) :
    1.49 +    m_sName(_rsName)
    1.50 +    {
    1.51 +    }
    1.52 +  virtual ~SectionNotFound() {}
    1.53 +
    1.54 +  inline std::string sGetName() const { return m_sName; }
    1.55 +
    1.56 +private:
    1.57 +  std::string m_sName;
    1.58 +};
    1.59 +
    1.60 +class KeyNotFound : public NotFound
    1.61 +{
    1.62 +public:
    1.63 +  KeyNotFound(const std::string & _rsSection, const std::string & _rsKey) :
    1.64 +    m_sSection(_rsSection),
    1.65 +    m_sKey(_rsKey)
    1.66 +    {
    1.67 +    }
    1.68 +  virtual ~KeyNotFound() {}
    1.69 +
    1.70 +  inline std::string sGetSection() const { return m_sSection; }
    1.71 +  inline std::string sGetKey() const { return m_sKey; }
    1.72 +
    1.73 +private:
    1.74 +  std::string m_sSection;
    1.75 +  std::string m_sKey;
    1.76 +};
    1.77 +
    1.78 +class Line
    1.79 +{
    1.80 +public:
    1.81 +  Line(const std::string & _rsKey, const std::string & _rsValue);
    1.82 +
    1.83 +  std::string m_sKey;
    1.84 +  std::string m_sValue;
    1.85 +};
    1.86 +
    1.87 +class Section : private std::list<Line>
    1.88 +{
    1.89 +public:
    1.90 +  explicit Section(const std::string & _rsName);
    1.91 +
    1.92 +  inline std::string sGetName() const { return m_sName; }
    1.93 +
    1.94 +  bool bKeyExists(const std::string & _rsKey);
    1.95 +  void vSetKey(const std::string & _rsKey, const std::string & _rsValue);
    1.96 +  std::string sGetKey(const std::string & _rsKey) const;
    1.97 +  void vRemoveKey(const std::string & _rsKey);
    1.98 +
    1.99 +  template<typename T>
   1.100 +  void vSetKey(const std::string & _rsKey, const T & _rValue);
   1.101 +
   1.102 +  template<typename T>
   1.103 +  T oGetKey(const std::string & _rsKey) const;
   1.104 +
   1.105 +  // read only
   1.106 +  typedef std::list<Line>::const_iterator const_iterator;
   1.107 +  inline const_iterator begin() const
   1.108 +    {
   1.109 +      return std::list<Line>::begin();
   1.110 +    }
   1.111 +  inline const_iterator end() const
   1.112 +    {
   1.113 +      return std::list<Line>::end();
   1.114 +    }
   1.115 +
   1.116 +private:
   1.117 +  inline iterator begin()
   1.118 +    {
   1.119 +      return std::list<Line>::begin();
   1.120 +    }
   1.121 +  inline iterator end()
   1.122 +    {
   1.123 +      return std::list<Line>::end();
   1.124 +    }
   1.125 +
   1.126 +  std::string m_sName;
   1.127 +};
   1.128 +
   1.129 +class File : private std::list<Section>
   1.130 +{
   1.131 +public:
   1.132 +  File();
   1.133 +  File(const std::string & _rsFile);
   1.134 +  virtual ~File();
   1.135 +
   1.136 +  bool bSectionExists(const std::string & _rsName);
   1.137 +  Section * poAddSection(const std::string & _rsName);
   1.138 +  Section * poGetSection(const std::string & _rsName);
   1.139 +  void vRemoveSection(const std::string & _rsName);
   1.140 +  void vLoad(const std::string & _rsFile,
   1.141 +             bool _bAddSection = true,
   1.142 +             bool _bAddKey = true);
   1.143 +  void vSave(const std::string & _rsFile);
   1.144 +  void vClear();
   1.145 +
   1.146 +  // read only
   1.147 +  typedef std::list<Section>::const_iterator const_iterator;
   1.148 +  inline const_iterator begin() const
   1.149 +    {
   1.150 +      return std::list<Section>::begin();
   1.151 +    }
   1.152 +  inline const_iterator end() const
   1.153 +    {
   1.154 +      return std::list<Section>::end();
   1.155 +    }
   1.156 +
   1.157 +private:
   1.158 +  inline iterator begin()
   1.159 +    {
   1.160 +      return std::list<Section>::begin();
   1.161 +    }
   1.162 +  inline iterator end()
   1.163 +    {
   1.164 +      return std::list<Section>::end();
   1.165 +    }
   1.166 +};
   1.167 +
   1.168 +// debug
   1.169 +std::ostream & operator<<(std::ostream & _roOut, const File & _roConfig);
   1.170 +
   1.171 +template<typename T>
   1.172 +void Section::vSetKey(const std::string & _rsKey, const T & _rValue)
   1.173 +{
   1.174 +  std::ostringstream oOut;
   1.175 +  oOut << _rValue;
   1.176 +  for (iterator it = begin(); it != end(); it++)
   1.177 +  {
   1.178 +    if (it->m_sKey == _rsKey)
   1.179 +    {
   1.180 +      it->m_sValue = oOut.str();
   1.181 +      return;
   1.182 +    }
   1.183 +  }
   1.184 +  push_back(Line(_rsKey, oOut.str()));
   1.185 +}
   1.186 +
   1.187 +template<typename T>
   1.188 +T Section::oGetKey(const std::string & _rsKey) const
   1.189 +{
   1.190 +  T oValue;
   1.191 +  for (const_iterator it = begin(); it != end(); it++)
   1.192 +  {
   1.193 +    if (it->m_sKey == _rsKey)
   1.194 +    {
   1.195 +      std::istringstream oIn(it->m_sValue);
   1.196 +      oIn >> oValue;
   1.197 +      return oValue;
   1.198 +    }
   1.199 +  }
   1.200 +  throw KeyNotFound(m_sName, _rsKey);
   1.201 +}
   1.202 +
   1.203 +} // namespace Config
   1.204 +} // namespace VBA
   1.205 +
   1.206 +
   1.207 +#endif // __VBA_CONFIGFILE_H__