SpectMorph

lib/smmicroconf.hh

00001 /* MicroConf - minimal configuration framework
00002  * Copyright (C) 2010 Stefan Westerfeld
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General
00015  * Public License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 #include <string>
00020 #include <vector>
00021 
00022 #include <stdio.h>
00023 
00024 namespace SpectMorph
00025 {
00026 
00027 class MicroConf
00028 {
00029 public:
00030   enum NumberFormat {
00031     I18N,
00032     NO_I18N
00033   };
00034 private:
00035   FILE                    *cfg_file;
00036   std::string              current_line;
00037   int                      current_no;
00038   std::string              current_file;
00039   std::vector<std::string> tokens;
00040   bool                     tokenizer_error;
00041   NumberFormat             m_number_format;
00042 
00043   bool convert (const std::string& token, int& arg);
00044   bool convert (const std::string& token, double& arg);
00045   bool convert (const std::string& token, std::string& arg);
00046 
00047   bool tokenize();
00048 public:
00049   MicroConf (const std::string& filename);
00050   ~MicroConf();
00051 
00052   NumberFormat number_format();
00053   void set_number_format (NumberFormat new_number_format);
00054 
00055   bool open_ok();
00056   bool next();
00057   std::string line();
00058   void die_if_unknown();
00059 
00060   template<class T1>
00061   bool command (const std::string& cmd, T1& arg1)
00062   {
00063     if (tokenizer_error || tokens.size() != 2 || cmd != tokens[0])
00064       return false;
00065     return convert (tokens[1], arg1);
00066   }
00067   template<class T1, class T2>
00068   bool command (const std::string& cmd, T1& arg1, T2& arg2)
00069   {
00070     if (tokenizer_error || tokens.size() != 3 || cmd != tokens[0])
00071       return false;
00072     return convert (tokens[1], arg1) && convert (tokens[2], arg2);
00073   }
00074   template<class T1, class T2, class T3>
00075   bool command (const std::string& cmd, T1& arg1, T2& arg2, T3& arg3)
00076   {
00077     if (tokenizer_error || tokens.size() != 4 || cmd != tokens[0])
00078       return false;
00079     return convert (tokens[1], arg1) && convert (tokens[2], arg2) && convert (tokens[3], arg3);
00080   }
00081   template<class T1, class T2, class T3, class T4>
00082   bool command (const std::string& cmd, T1& arg1, T2& arg2, T3& arg3, T4& arg4)
00083   {
00084     if (tokenizer_error || tokens.size() != 5 || cmd != tokens[0])
00085       return false;
00086     return convert (tokens[1], arg1) && convert (tokens[2], arg2) && convert (tokens[3], arg3)
00087       &&   convert (tokens[4], arg4);
00088   }
00089   template<class T1, class T2, class T3, class T4, class T5>
00090   bool command (const std::string& cmd, T1& arg1, T2& arg2, T3& arg3, T4& arg4, T5& arg5)
00091   {
00092     if (tokenizer_error || tokens.size() != 6 || cmd != tokens[0])
00093       return false;
00094     return convert (tokens[1], arg1) && convert (tokens[2], arg2) && convert (tokens[3], arg3)
00095       &&   convert (tokens[4], arg4) && convert (tokens[5], arg5);
00096   }
00097   template<class T1, class T2, class T3, class T4, class T5, class T6>
00098   bool command (const std::string& cmd, T1& arg1, T2& arg2, T3& arg3, T4& arg4, T5& arg5, T6& arg6)
00099   {
00100     if (tokenizer_error || tokens.size() != 7 || cmd != tokens[0])
00101       return false;
00102     return convert (tokens[1], arg1) && convert (tokens[2], arg2) && convert (tokens[3], arg3)
00103       &&   convert (tokens[4], arg4) && convert (tokens[5], arg5) && convert (tokens[6], arg6);
00104   }
00105 };
00106 
00107 }
 All Classes Functions Variables Enumerations Enumerator