fgms 0.11.8
The
FlightGear MultiPlayer Server
project
fg_config.cxx
Go to the documentation of this file.
1 /**
2  * @file fg_config.cxx
3  * @author Oliver Schroeder
4  */
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // 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 Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, U$
19 //
20 // Copyright (C) 2005 Oliver Schroeder
21 //
22 
23 //////////////////////////////////////////////////////////////////////
24 //
25 // Simple configfile parser for fg_server
26 //
27 //////////////////////////////////////////////////////////////////////
28 
29 #include <cctype>
30 #include "fg_config.hxx"
31 
32 
33 
34 
35 //////////////////////////////////////////////////////////////////////
36 /** @brief Read in the config file
37  * @param ConfigName - the file to read
38  * @retval int 1 for error, 0 for success
39  */
40 int
42 (
43  const std::string &ConfigName
44 )
45 {
46  std::ifstream ConfigFile;
47  std::string ConfigLine;
48  int LineNumber;
49 
50  ConfigFile.open (ConfigName.c_str ());
51  if (!ConfigFile)
52  {
53  return (1);
54  }
55  LineNumber = 0;
56  while (ConfigFile)
57  {
58  getline (ConfigFile, ConfigLine);
59  LineNumber++;
60  if (ParseLine (ConfigLine))
61  {
62  std::cout << "error in line " << LineNumber
63  << " in file " << ConfigName
64  << std::endl;
65  }
66  }
67  ConfigFile.close ();
68  m_CurrentVar = m_VarList.begin ();
69  return (0);
70 } // FG_CONFIG::Read ()
71 //////////////////////////////////////////////////////////////////////
72 
73 //////////////////////////////////////////////////////////////////////
74 
75 /** @brief Find a variable in the internal list and return its value
76  * @param VarName - the variable to find
77  * @retval string - with contents of variable, or blank string if not found
78 */
79 std::string
81 (
82  const std::string &VarName
83 )
84 {
85  m_CurrentVar = m_VarList.begin();
86  while (m_CurrentVar != m_VarList.end())
87  {
88  if (m_CurrentVar->first == VarName)
89  {
90  return (m_CurrentVar->second);
91  }
92  m_CurrentVar++;
93  }
94  return ("");
95 } // FG_SERVER::Get ()
96 
97 
98 
99 //////////////////////////////////////////////////////////////////////
100 /**
101  * @brief Set internal pointer to the first variable in list
102  */
103 void
105 ()
106 {
107  m_CurrentVar = m_VarList.begin();
108 } // FG_CONFIG::SetStart ()
109 //////////////////////////////////////////////////////////////////////
110 
111 //////////////////////////////////////////////////////////////////////
112 /**
113  * @brief Set internal pointer to the next var.
114  * @retval int 1 for success, else 0
115  */
116 int
118 ()
119 {
120  if (m_CurrentVar == m_VarList.end())
121  {
122  return (0);
123  }
124  m_CurrentVar++;
125  if (m_CurrentVar == m_VarList.end())
126  {
127  return (0);
128  }
129  return (1);
130 } // FG_CONFIG::Next()
131 
132 
133 
134 //////////////////////////////////////////////////////////////////////
135 /**
136  * @brief Returns the complete name of the current variable.
137  * @retval string The variable name, or empty string if not found.
138  */
139 std::string
141 ()
142 {
143  if (m_CurrentVar != m_VarList.end())
144  {
145  return (m_CurrentVar->first);
146  }
147  return ("");
148 } // FG_CONFIG::GetName ()
149 
150 
151 
152 //////////////////////////////////////////////////////////////////////
153 /**
154  * \brief Returns the current variable's value
155  * \retval string The variable name, or empty string if not found.
156  */
157 std::string
159 ()
160 {
161  if (m_CurrentVar != m_VarList.end())
162  {
163  return (m_CurrentVar->second);
164  }
165  return ("");
166 } // FG_CONFIG::GetValue ()
167 
168 
169 
170 //////////////////////////////////////////////////////////////////////
171 /**
172  * @brief Set internal pointer to the first variable of a section
173  * @todo Sort the variable list
174  * @param SecName String with section name
175  * @retval int 1 on success, else 0
176  */
177 int
179 (
180  const std::string &SecName
181 )
182 {
183  SetStart ();
184  while (m_CurrentVar != m_VarList.end())
185  {
186  if (m_CurrentVar->first.compare(0,SecName.size(),SecName) == 0)
187  {
188  m_CurrentSection = SecName;
189  return (1);
190  }
191  m_CurrentVar++;
192  }
193  return (0);
194 } // FG_CONFIG::SetSection ( const std::string &SecName )
195 
196 
197 
198 //////////////////////////////////////////////////////////////////////
199 /**
200  * @brief Set internal pointer to next variable in a section.
201  * @retval int 1 on success, 0 else
202  */
203 int
205 ()
206 {
207  if (!Next())
208  {
209  return (0);
210  }
211  if (m_CurrentVar->first.compare (0,
212  m_CurrentSection.size(), m_CurrentSection) != 0)
213  {
214  return (0);
215  }
216  return (1);
217 } // FG_CONFIG::GetSecNextVar ()
218 
219 
220 //////////////////////////////////////////////////////////////////////
221 /**
222  * @brief Get the next variable name in current section
223  * @retval string The variable name, or empty string if not found
224  */
225 std::string
227 ()
228 {
229  if (!Next())
230  {
231  return ("");
232  }
233  if (m_CurrentVar->first.compare (0,
234  m_CurrentSection.size(), m_CurrentSection) != 0)
235  {
236  return ("");
237  }
238  return (m_CurrentVar->first);
239 } // FG_CONFIG::GetSecNextVar ()
240 
241 
242 
243 //////////////////////////////////////////////////////////////////////
244 /**
245  * @brief Return variable value of next variable in section
246  * @retval string The value or empty string if not found
247  */
248 std::string
250 ()
251 {
252  if (!Next())
253  {
254  return ("");
255  }
256  if (m_CurrentVar->first.compare (0,
257  m_CurrentSection.size(), m_CurrentSection) != 0)
258  {
259  return ("");
260  }
261  return (m_CurrentVar->second);
262 } // FG_CONFIG::GetSecNextVal ()
263 
264 
265 
266 //////////////////////////////////////////////////////////////////////
267 /**
268  * @brief Get the value of the next variable in list
269  * @retval string The value or empty string if not found
270  */
271 std::string
273 ()
274 {
275  if (!Next())
276  {
277  return ("");
278  }
279  return (m_CurrentVar->second);
280 } // FG_CONFIG::GetNext ()
281 
282 
283 
284 //////////////////////////////////////////////////////////////////////
285 /**
286  * @brief Just for debugging to cout
287  */
288 void
290 ()
291 {
292  m_CurrentVar = m_VarList.begin();
293  std::cout << std::endl;
294  std::cout << "dumping variables:" << std::endl;
295  while (m_CurrentVar != m_VarList.end())
296  {
297  std::cout << " Var: '" << m_CurrentVar->first << "'";
298  std::cout << " Val: '" << m_CurrentVar->second << "'";
299  std::cout << std::endl;
300  m_CurrentVar++;
301  }
302  std::cout << std::endl;
303  std::cout << "done." << std::endl;
304  std::cout << std::endl;
305 } // FG_SERVER::Dump ()
306 
307 
308 
309 //////////////////////////////////////////////////////////////////////
310 /**
311  * @brief Parse the given line, split it into name/value pairs
312  * and put in the internal list
313  * @param ConfigLine The line to parse
314  * @retval int
315  */
316 int
318 (
319  const std::string &ConfigLine
320 )
321 {
322  size_t nVarNameStart, nVarNameEnd;
323  size_t nVarValueStart, nVarValueEnd;
324  mT_VarValue NewVar;
325 
326  if (ConfigLine.size() < 2) // ignore dos/windows 0x0d only
327  {
328  return (0);
329  }
330  if (ConfigLine[0] == '#')
331  {
332  return (0);
333  }
334  nVarNameStart = ConfigLine.find_first_not_of (" \t\n");
335  nVarNameEnd = ConfigLine.find ('=');
336  if ((nVarNameStart == std::string::npos)
337  || (nVarNameEnd == std::string::npos))
338  {
339  return (1);
340  }
341  nVarValueStart = ConfigLine.find_first_not_of (" \t\n", nVarNameEnd+1);
342  nVarValueEnd = ConfigLine.size();
343  while (isspace (ConfigLine[nVarNameEnd-1]))
344  {
345  nVarNameEnd--;
346  }
347  while (isspace (ConfigLine[nVarValueEnd-1]))
348  {
349  nVarValueEnd--;
350  }
351  NewVar.first = ConfigLine.substr
352  (nVarNameStart, nVarNameEnd - nVarNameStart);
353  NewVar.second = ConfigLine.substr
354  (nVarValueStart, nVarValueEnd - nVarValueStart);
355  m_VarList.push_back (NewVar);
356  return (0);
357 } // FG_SERVER::ParseLine ()
358 //////////////////////////////////////////////////////////////////////
359 
int Read(const std::string &ConfigName)
Read in the config file.
Definition: fg_config.cxx:42
void Dump()
Just for debugging to cout.
Definition: fg_config.cxx:290
std::string GetSecNextVal()
Return variable value of next variable in section.
Definition: fg_config.cxx:250
int Next()
Set internal pointer to the next var.
Definition: fg_config.cxx:118
int SecNext()
Set internal pointer to next variable in a section.
Definition: fg_config.cxx:205
void SetStart()
Set internal pointer to the first variable in list.
Definition: fg_config.cxx:105
std::string GetSecNextVar()
Get the next variable name in current section.
Definition: fg_config.cxx:227
std::string GetValue()
Returns the current variable's value.
Definition: fg_config.cxx:159
std::pair< std::string, std::string > mT_VarValue
Definition: fg_config.hxx:62
int SetSection(const std::string &SecName)
Set internal pointer to the first variable of a section.
Definition: fg_config.cxx:179
std::string GetName()
Returns the complete name of the current variable.
Definition: fg_config.cxx:141
std::string GetNext()
Get the value of the next variable in list.
Definition: fg_config.cxx:273
std::string Get(const std::string &VarName)
Find a variable in the internal list and return its value.
Definition: fg_config.cxx:81
int ParseLine(const std::string &ConfigLine)
Parse the given line, split it into name/value pairs and put in the internal list.
Definition: fg_config.cxx:318