Files.hxx

00001 // Copyright (C) 2004-2007, Vivien Mallet
00002 //
00003 // This file is part of Talos library, which provides miscellaneous tools to
00004 // make up for C++ lacks and to ease C++ programming.
00005 //
00006 // Talos is free software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 2 of the License, or (at your option) any later
00009 // version.
00010 //
00011 // Talos is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00014 // details.
00015 //
00016 // For more information, visit the Talos home page:
00017 //     http://vivienmallet.net/lib/talos/
00018 
00019 
00020 #ifndef TALOS_FILE_FILES_HXX
00021 
00022 
00023 #include <iostream>
00024 #include <algorithm>
00025 #include <string>
00026 #include <fstream>
00027 #include <sstream>
00028 #include <vector>
00029 #include <stdexcept>
00030 
00031 
00032 namespace Talos
00033 {
00034 
00035   using namespace std;
00036 
00037   bool exists(string file_name);
00038   unsigned long file_size(string file_name);
00039 #ifndef SWIG
00040   unsigned long stream_size(istream& stream);
00041   bool is_empty(istream& stream);
00042   bool has_element(istream& stream);
00043 #endif
00044 
00045   template <class T>
00046   bool satisfies_constraint(T value, string constraint);
00047   string show_constraint(string constraint);
00048 
00050 #ifndef SWIG
00051   class ExtStream: public ifstream
00052 #else
00053   class ExtStream
00054 #endif
00055   {
00056   protected:
00058     string file_name_;
00060     string comments_;
00062     string delimiters_;
00063 
00065     string searching_;
00066 
00067   public:
00068     ExtStream();
00069     ExtStream(string file_name,
00070               string comments = "#%",
00071               string delimiters = " \t:=|\n,;\r\x0D\x0A");
00072 
00073     virtual ~ExtStream();
00074 
00075     bool Discard(string line) const;
00076     ExtStream& SkipDiscarded();
00077 
00078     void SetDelimiters(string delimiters);
00079     void SetComments(string comments);
00080 
00081     string GetDelimiters() const;
00082     string GetComments() const;
00083     string GetFileName() const;
00084 
00085     ExtStream& SkipDelimiters();
00086     string RemoveDelimiters(const string& str) const;
00087 
00088     ExtStream& Skip();
00089 
00090 #ifndef SWIG
00091     void Open(string file_name, openmode mode = in);
00092 #endif
00093     void Close();
00094     
00095     bool IsEmpty();
00096     
00097     ExtStream& Rewind();
00098 
00099     string GetFullLine();
00100     bool GetFullLine(string& line);
00101     string PeekFullLine();
00102 #ifndef SWIG
00103     string PeekFullLine(streampos& position);
00104 #endif
00105     bool PeekFullLine(string& line);
00106     void SkipFullLines(int nb);
00107 
00108     virtual string GetLine();
00109     virtual bool GetLine(string& line);
00110     virtual string PeekLine();
00111 #ifndef SWIG
00112     virtual string PeekLine(streampos& position);
00113 #endif
00114     virtual bool PeekLine(string& line);
00115     void SkipLines(int nb);
00116 
00117     bool Find(string element);
00118     bool FindFromBeginning(string element);
00119 
00120     virtual string GetElement();
00121     template <class T>
00122     bool GetElement(T& element);
00123     template <class T>
00124     bool GetRawElement(T& element);
00125     string PeekElement();
00126     template <class T>
00127     bool PeekElement(T& element);
00128     void SkipElements(int nb);
00129 
00130     double GetNumber();
00131     template <class T>
00132     bool GetNumber(T& number);
00133     double PeekNumber();
00134     template <class T>
00135     bool PeekNumber(T& number);
00136     void SkipNumbers(int nb);
00137 
00138     string GetValue(string name);
00139     string PeekValue(string name);
00140 
00141     template <class T>
00142     void GetValue(string name, T& value);
00143     void GetValue(string name, int& value);
00144     template <class T>
00145     void PeekValue(string name, T& value);
00146     template <class T>
00147     void GetValue(string name, T min, T max, T& value);
00148     template <class T>
00149     void PeekValue(string name, T min, T max, T& value);
00150     template <class T>
00151     void GetValue(string name, string constraints, T& value);
00152     template <class T>
00153     void PeekValue(string name, string constraints, T& value);
00154 
00155     void GetValue(string name, string& value);
00156     void PeekValue(string name, string& value);
00157     void GetValue(string name, string accepted, string& value,
00158                   string delimiter);
00159     void PeekValue(string name, string accepted, string& value,
00160                    string delimiter);
00161 
00162     void GetValue(string name, bool& value);
00163     void PeekValue(string name, bool& value);
00164 
00165   protected:
00166     void CheckAccepted(string name, string value, string accepted,
00167                        string delimiter) const;
00168   };
00169 
00171   class ConfigStream: public ExtStream
00172   {
00173   protected:
00174     string markup_tags_;
00175     string section_;
00176     
00177   public:
00178     ConfigStream();
00179     ConfigStream(string file_name,
00180                  string comments = "#%",
00181                  string delimiters = " \t:=|\n,;\r\x0D\x0A",
00182                  string markup_tags = "<>$");
00183 
00184     void NoSection();
00185     void SetSection(string section);
00186     string GetSection() const;
00187 
00188     void SetMarkupTags(string markup_tags);
00189     string GetMarkupTags() const;
00190 
00191     bool IsEmpty();
00192 
00193     bool Find(string element);
00194     bool FindFromBeginning(string element);
00195 
00196     using ExtStream::GetElement;
00197     virtual string GetElement();
00198 
00199     virtual string GetLine();
00200     virtual bool GetLine(string& line);
00201 
00202   private:
00203     bool IsSection(string str) const;
00204 
00205     friend class ConfigStreams;
00206   };
00207 
00209   class ConfigStreams
00210   {
00211   protected:
00212     vector<ConfigStream*> streams_;
00213     vector<ConfigStream*>::iterator current_;
00214 
00215     string section_;
00216 
00218     string searching_;
00219 
00220   public:
00221     ConfigStreams();
00222     ConfigStreams(const vector<string>& files);
00223     ConfigStreams(string file0);
00224     ConfigStreams(string file0, string file1);
00225     ConfigStreams(string file0, string file1, string file2);
00226 
00227     ~ConfigStreams();
00228 
00229     vector<ConfigStream*>& GetStreams();
00230     vector<ConfigStream*>::iterator GetCurrent();
00231 
00232     void AddFile(string file);
00233 
00234     void NoSection();
00235     void SetSection(string section);
00236     string GetSection() const;
00237 
00238     bool Discard(string line) const;
00239     ConfigStreams& SkipDiscarded();
00240 
00241     ConfigStreams& SkipDelimiters();
00242     string RemoveDelimiters(const string& str) const;
00243 
00244     ConfigStreams& Skip();
00245 
00246     bool IsEmpty();
00247     
00248     ConfigStreams& Rewind();
00249 
00250     string GetFullLine();
00251     bool GetFullLine(string& line);
00252     string PeekFullLine();
00253 #ifndef SWIG
00254     string PeekFullLine(streampos& position);
00255 #endif
00256     bool PeekFullLine(string& line);
00257     void SkipFullLines(int nb);
00258 
00259     string GetRawLine();
00260     string GetLine();
00261     bool GetLine(string& line);
00262     string PeekLine();
00263 #ifndef SWIG
00264     string PeekLine(streampos& position);
00265 #endif
00266     bool PeekLine(string& line);
00267     void SkipLines(int nb);
00268 
00269     bool Find(string element);
00270     bool FindFromBeginning(string element);
00271 
00272     string GetRawElement();
00273     string GetElement();
00274     template <class T>
00275     bool GetElement(T& element);
00276     string PeekElement();
00277     template <class T>
00278     bool PeekElement(T& element);
00279     void SkipElements(int nb);
00280 
00281     double GetNumber();
00282     template <class T>
00283     bool GetNumber(T& number);
00284     double PeekNumber();
00285     template <class T>
00286     bool PeekNumber(T& number);
00287     void SkipNumbers(int nb);
00288 
00289     string GetValue(string name);
00290     string PeekValue(string name);
00291 
00292     template <class T>
00293     void GetValue(string name, T& value);
00294     void GetValue(string name, int& value);
00295     template <class T>
00296     void PeekValue(string name, T& value);
00297     template <class T>
00298     void GetValue(string name, T min, T max, T& value);
00299     template <class T>
00300     void PeekValue(string name, T min, T max, T& value);
00301     template <class T>
00302     void GetValue(string name, string constraints, T& value);
00303     template <class T>
00304     void PeekValue(string name, string constraints, T& value);
00305 
00306     void GetValue(string name, string& value);
00307     void PeekValue(string name, string& value);
00308     void GetValue(string name, string accepted, string& value,
00309                   string delimiter);
00310     void PeekValue(string name, string accepted, string& value,
00311                    string delimiter);
00312 
00313     void GetValue(string name, bool& value);
00314     void PeekValue(string name, bool& value);
00315 
00316   private:
00317     bool IsSection(string str) const;
00318     string FileNames() const;
00319     void CheckAccepted(string name, string value, string accepted,
00320                        string delimiter) const;
00321   };
00322 
00323 }  // namespace Talos.
00324 
00325 
00326 #define TALOS_FILE_FILES_HXX
00327 #endif

Generated on Mon Nov 17 18:20:41 2008 for Talos by  doxygen 1.5.1