FreeLing  4.0
configfile.h
Go to the documentation of this file.
00001 
00002 //
00003 //    FreeLing - Open Source Language Analyzers
00004 //
00005 //    Copyright (C) 2014   TALP Research Center
00006 //                         Universitat Politecnica de Catalunya
00007 //
00008 //    This library is free software; you can redistribute it and/or
00009 //    modify it under the terms of the GNU Affero General Public
00010 //    License as published by the Free Software Foundation; either
00011 //    version 3 of the License, or (at your option) any later version.
00012 //
00013 //    This library is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //    Affero General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU Affero General Public
00019 //    License along with this library; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021 //
00022 //    contact: Lluis Padro (padro@lsi.upc.es)
00023 //             TALP Research Center
00024 //             despatx C6.212 - Campus Nord UPC
00025 //             08034 Barcelona.  SPAIN
00026 //
00028 
00029 #ifndef _CONFIGFILE
00030 #define _CONFIGFILE
00031 
00032 #include <string>
00033 #include <fstream>
00034 #include <sstream>
00035 #include <map>
00036 #include <set>
00037 
00038 namespace freeling {
00039 
00046 
00047   class config_file {
00048 
00049   private:
00050     std::map<std::wstring,int> sectionsopen, sectionsclose;
00051     std::set<std::wstring> requiredsections;
00052 
00053     std::wifstream filestr;
00054     int section;
00055     static const int SECTION_NONE = -1;
00056     static const int SECTION_UNKNOWN = -2;
00057 
00058     std::wstring filename;  // for error messages and traces
00059 
00060     // true just after section begin
00061     bool section_start;
00062     // prefix that line must have to be considered a comment
00063     std::wstring comment_prefix;
00064     // current line num
00065     int line_num;
00066 
00067     // set at creation time, tells whether to complain about 
00068     // unknown sections or just skip them.
00069     bool skip_unknown_sections;
00070     std::wstring unk_name;
00071 
00072     bool isopensection(const std::wstring &s) const;
00073     bool isclosesection(const std::wstring &s) const;
00074     bool iscomment(const std::wstring &s) const;
00075 
00076   public:
00077     config_file(bool skip=false, const std::wstring &comment=L"##");
00078     ~config_file();
00079     void add_section(const std::wstring &key, int section, bool required=false);
00080     bool open(const std::wstring &fname);
00081     void close();
00082     bool get_content_line(std::wstring &line);
00083     int get_section() const;
00084     int get_line_num() const;
00085     bool at_section_start() const;
00086   };
00087 
00088 }
00089 
00090 #endif