Main Page | User's Guide | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

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 namespace Talos
00024 {
00025 
00026   bool exists(string file_name);
00027   unsigned long file_size(string file_name);
00028   unsigned long stream_size(istream& stream);
00029   bool is_empty(istream& stream);
00030   bool has_element(istream& stream);
00031 
00033   class ExtStream: public ifstream
00034   {
00035   protected:
00037     string file_name_;
00039     string comments_;
00041     string delimiters_;
00042 
00044     string searching_;
00045 
00046   public:
00047     ExtStream();
00048     ExtStream(string file_name,
00049               string comments = "#%",
00050               string delimiters = " \t:=|\n,;\r\x0D\x0A");
00051 
00052     virtual ~ExtStream();
00053 
00054     bool Discard(string line) const;
00055     ExtStream& SkipDiscarded();
00056 
00057     void SetDelimiters(string delimiters);
00058     void SetComments(string comments);
00059 
00060     string GetDelimiters() const;
00061     string GetComments() const;
00062     string GetFileName() const;
00063 
00064     ExtStream& SkipDelimiters();
00065     string RemoveDelimiters(const string& str) const;
00066 
00067     ExtStream& Skip();
00068 
00069     void Open(string file_name, openmode mode = in);
00070     void Close();
00071     
00072     bool IsEmpty();
00073     
00074     ExtStream& Rewind();
00075 
00076     string GetFullLine();
00077     bool GetFullLine(string& line);
00078     string PeekFullLine();
00079     string PeekFullLine(streampos& position);
00080     bool PeekFullLine(string& line);
00081     void SkipFullLines(int nb);
00082 
00083     virtual string GetLine();
00084     virtual bool GetLine(string& line);
00085     virtual string PeekLine();
00086     virtual string PeekLine(streampos& position);
00087     virtual bool PeekLine(string& line);
00088     void SkipLines(int nb);
00089 
00090     bool Find(string element);
00091     bool FindFromBeginning(string element);
00092 
00093     virtual string GetElement();
00094     template <class T>
00095     bool GetElement(T& element);
00096     template <class T>
00097     bool GetRawElement(T& element);
00098     string PeekElement();
00099     template <class T>
00100     bool PeekElement(T& element);
00101     void SkipElements(int nb);
00102 
00103     double GetNumber();
00104     template <class T>
00105     bool GetNumber(T& number);
00106     double PeekNumber();
00107     template <class T>
00108     bool PeekNumber(T& number);
00109     void SkipNumbers(int nb);
00110 
00111     string GetValue(string name);
00112     string PeekValue(string name);
00113 
00114     template <class T>
00115     bool GetValue(string name, T& value);
00116     template <class T>
00117     bool PeekValue(string name, T& value);
00118 
00119     bool GetValue(string name, string& value);
00120     bool PeekValue(string name, string& value);
00121 
00122     bool GetValue(string name, bool& value);
00123     bool PeekValue(string name, bool& value);
00124   };
00125 
00127   class ConfigStream: public ExtStream
00128   {
00129   protected:
00130     string markup_tags_;
00131     string section_;
00132     
00133   public:
00134     ConfigStream();
00135     ConfigStream(string file_name,
00136                  string comments = "#%",
00137                  string delimiters = " \t:=|\n,;\r\x0D\x0A",
00138                  string markup_tags = "<>$");
00139 
00140     void NoSection();
00141     void SetSection(string section);
00142     string GetSection() const;
00143 
00144     void SetMarkupTags(string markup_tags);
00145     string GetMarkupTags() const;
00146 
00147     bool IsEmpty();
00148 
00149     bool Find(string element);
00150     bool FindFromBeginning(string element);
00151 
00152     virtual string GetElement();
00153 
00154     virtual string GetLine();
00155     virtual bool GetLine(string& line);
00156 
00157   private:
00158     bool IsSection(string str) const;
00159 
00160     friend class ConfigStreams;
00161   };
00162 
00164   class ConfigStreams
00165   {
00166   protected:
00167     vector<ConfigStream*> streams_;
00168     vector<ConfigStream*>::iterator current_;
00169 
00170     string section_;
00171 
00173     string searching_;
00174 
00175   public:
00176     ConfigStreams();
00177     ConfigStreams(const vector<string>& files);
00178     ConfigStreams(string file0);
00179     ConfigStreams(string file0, string file1);
00180     ConfigStreams(string file0, string file1, string file2);
00181 
00182     ~ConfigStreams();
00183 
00184     vector<ConfigStream*>& GetStreams();
00185     vector<ConfigStream*>::iterator GetCurrent();
00186 
00187     void AddFile(string file);
00188 
00189     void NoSection();
00190     void SetSection(string section);
00191     string GetSection() const;
00192 
00193     bool Discard(string line) const;
00194     ConfigStreams& SkipDiscarded();
00195 
00196     ConfigStreams& SkipDelimiters();
00197     string RemoveDelimiters(const string& str) const;
00198 
00199     ConfigStreams& Skip();
00200 
00201     bool IsEmpty();
00202     
00203     ConfigStreams& Rewind();
00204 
00205     string GetFullLine();
00206     bool GetFullLine(string& line);
00207     string PeekFullLine();
00208     string PeekFullLine(streampos& position);
00209     bool PeekFullLine(string& line);
00210     void SkipFullLines(int nb);
00211 
00212     string GetRawLine();
00213     string GetLine();
00214     bool GetLine(string& line);
00215     string PeekLine();
00216     string PeekLine(streampos& position);
00217     bool PeekLine(string& line);
00218     void SkipLines(int nb);
00219 
00220     bool Find(string element);
00221     bool FindFromBeginning(string element);
00222 
00223     string GetRawElement();
00224     string GetElement();
00225     template <class T>
00226     bool GetElement(T& element);
00227     string PeekElement();
00228     template <class T>
00229     bool PeekElement(T& element);
00230     void SkipElements(int nb);
00231 
00232     double GetNumber();
00233     template <class T>
00234     bool GetNumber(T& number);
00235     double PeekNumber();
00236     template <class T>
00237     bool PeekNumber(T& number);
00238     void SkipNumbers(int nb);
00239 
00240     string GetValue(string name);
00241     string PeekValue(string name);
00242 
00243     template <class T>
00244     bool GetValue(string name, T& value);
00245     template <class T>
00246     bool PeekValue(string name, T& value);
00247 
00248     bool GetValue(string name, string& value);
00249     bool PeekValue(string name, string& value);
00250 
00251     bool GetValue(string name, bool& value);
00252     bool PeekValue(string name, bool& value);
00253 
00254   private:
00255     bool IsSection(string str) const;
00256     string FileNames() const;
00257   };
00258 
00259 }  // namespace Talos.
00260 
00261 
00262 #define TALOS_FILE_FILES_HXX
00263 #endif

Generated on Tue Oct 23 14:06:14 2007 for Talos by  doxygen 1.4.2