MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textsettingsfile.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _resourcetextsettingsfile_cpp
41 #define _resourcetextsettingsfile_cpp
42 
43 #include "Resource/textsettingsfile.h"
44 #include "stringtool.h"
45 #include "exception.h"
46 
47 #include <OgreResourceGroupManager.h>
48 
49 namespace Mezzanine
50 {
51  namespace Resource
52  {
54  {
55  }
56 
58  {
59  Clear();
60  }
61 
62  void TextSettingsFile::Load(const String& Filename, const String& Group, const String& Separators, bool TrimWhitespace)
63  {
64  /// @todo Once the resource system has been updated to be independant of Ogre's, this function needs to be updated to access our methods instead of theirs.
65  Clear();
66 
67  FileName = Filename;
68  Ogre::DataStreamPtr Stream = Ogre::ResourceGroupManager::getSingletonPtr()->openResource(Filename,Group);
69  String CurrentSectionName("");
70  SettingsMap* CurrentSection = new SettingsMap();
71  Sections[CurrentSectionName] = CurrentSection;
72 
73  String Line, SetName, SetVal;
74  while(!Stream->eof())
75  {
76  Line = Stream->getLine();
77  if (Line.length() > 0 && Line.at(0) != '#' && Line.at(0) != '@')
78  {
79  if (Line.at(0) == '[' && Line.at(Line.length()-1) == ']')
80  {
81  CurrentSectionName = Line.substr(1, Line.length() - 2);
82  SectionIterator SecIt = Sections.find(CurrentSectionName);
83  if (SecIt == Sections.end())
84  {
85  CurrentSection = new SettingsMap();
86  Sections[CurrentSectionName] = CurrentSection;
87  }else{
88  CurrentSection = SecIt->second;
89  }
90  }else{
91  String::size_type SeparatorPos = Line.find_first_of(Separators,0);
92  if(SeparatorPos != String::npos)
93  {
94  SetName = Line.substr(0,SeparatorPos);
95  String::size_type NonSeparatorPos = Line.find_first_not_of(Separators,SeparatorPos);
96  SetVal = (NonSeparatorPos == String::npos) ? "" : Line.substr(NonSeparatorPos);
97  if (TrimWhitespace)
98  {
99  StringTools::Trim(SetVal);
100  StringTools::Trim(SetName);
101  }
102  CurrentSection->insert(SettingsMap::value_type(SetName,SetVal));
103  }
104  }
105  }
106  }
107  }
108 
110  {
111  if(Sections.empty())
112  return;
113  for( TextSettingsFile::SectionIterator SecIt = Sections.begin() ; SecIt != Sections.end() ; ++SecIt )
114  {
115  delete (*SecIt).second;
116  }
117  Sections.clear();
118  FileName.clear();
119  }
120 
121  String TextSettingsFile::GetSetting(const String& Key, const String& Section)
122  {
123  TextSettingsFile::SectionIterator SecIt = Sections.find(Section);
124  if(SecIt == Sections.end())
125  {
126  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Attempting to access section \"" + Section + "\" in file \"" + FileName + "\" which does not exist.");
127  }else{
128  TextSettingsFile::SettingsIterator SetIt = (*SecIt).second->find(Key);
129  if(SetIt == (*SecIt).second->end())
130  {
131  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Attempting to access setting \"" + Key + " in section \"" + Section + "\" in file \"" + FileName + "\" which does not exist.");
132  }else{
133  return (*SetIt).second;
134  }
135  }
136  }
137 
138  std::vector<String> TextSettingsFile::GetMultiSetting(const String& Key, const String& Section)
139  {
140  std::vector<String> Ret;
141 
142  TextSettingsFile::SectionIterator SecIt = Sections.find(Section);
143  if(SecIt != Sections.end())
144  {
145  TextSettingsFile::SettingsIterator SetIt = (*SecIt).second->find(Key);
146  while(SetIt != SecIt->second->end() && SetIt->first == Key)
147  {
148  Ret.push_back(SetIt->second);
149  ++SetIt;
150  }
151  }
152  return Ret;
153  }
154 
155  TextSettingsFile::SectionIterator TextSettingsFile::GetSectionBegin()
156  {
157  return Sections.begin();
158  }
159 
160  TextSettingsFile::SectionIterator TextSettingsFile::GetSectionEnd()
161  {
162  return Sections.end();
163  }
164 
165  TextSettingsFile::SettingsIterator TextSettingsFile::GetSettingsBegin(const String& Section)
166  {
167  TextSettingsFile::SectionIterator SecIt = Sections.find(Section);
168  if(SecIt == Sections.end())
169  {
170  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Attempting to access section \"" + Section + "\" in file \"" + FileName + "\" which does not exist.");
171  }else{
172  return (*SecIt).second->begin();
173  }
174  }
175 
176  TextSettingsFile::SettingsIterator TextSettingsFile::GetSettingsEnd(const String& Section)
177  {
178  TextSettingsFile::SectionIterator SecIt = Sections.find(Section);
179  if(SecIt == Sections.end())
180  {
181  MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Attempting to access section \"" + Section + "\" in file \"" + FileName + "\" which does not exist.");
182  }else{
183  return (*SecIt).second->end();
184  }
185  }
186  }//Resource
187 }//Mezzanine
188 
189 #endif