00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl 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 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_TEXT_FILE_H 00024 #define O2SCL_TEXT_FILE_H 00025 00026 #include <iostream> 00027 #include <stack> 00028 #include <o2scl/err_hnd.h> 00029 #include <o2scl/file_format.h> 00030 #include <o2scl/misc.h> 00031 #include <o2scl/lib_settings.h> 00032 00033 #ifndef DOXYGENP 00034 namespace o2scl { 00035 #endif 00036 00037 /** \brief An output text file 00038 00039 This class is experimental. 00040 00041 \todo Test output with <'s and >'s and document this 00042 00043 \todo Document the difference between flush() and end_line() 00044 00045 <b>Note:</b> Text files are not entirely 00046 architecture-independent, For example, a larger integer will not 00047 be read correctly on small integer systems. 00048 */ 00049 class text_out_file : public out_file_format { 00050 public: 00051 00052 text_out_file(); 00053 00054 /** \brief Use output stream \c out_file for text output 00055 00056 This constructor assumes that the I/O properties of 00057 \c out_file have already been set. 00058 00059 Note that the stream \c out_file should not have 00060 been opened in binary mode, and errors will likely occur 00061 if this is the case. 00062 00063 \todo Ensure streams are not opened in binary mode 00064 for safety. 00065 */ 00066 text_out_file(std::ostream *out_file, int width=80, 00067 bool bracket_objs=true); 00068 00069 /** \brief Create an output file with name \c file_name 00070 00071 If \c prop is not NULL (i.e. nonzero), then the I/O properties 00072 (precision, fill, flags, etc) for the newly created file are 00073 taken to be the same as \c prop . 00074 00075 */ 00076 text_out_file(std::string file_name, std::ostream *prop=0, 00077 bool append=false, int width=80, 00078 bool bracket_objs=true); 00079 00080 /// Desc 00081 virtual int open(std::ostream *out_file, int width=80, 00082 bool bracket_objs=true); 00083 00084 /// Desc 00085 virtual int open(std::string file_name, std::ostream *prop=0, 00086 bool append=false, int width=80, 00087 bool bracket_objs=true); 00088 00089 /// Desc 00090 virtual int close(); 00091 00092 virtual ~text_out_file(); 00093 00094 /** \brief Output a bool variable 00095 */ 00096 virtual int bool_out(bool dat, std::string name=""); 00097 00098 /** \brief Output a char variable 00099 */ 00100 virtual int char_out(char dat, std::string name=""); 00101 00102 /** \brief Output a char variable 00103 */ 00104 virtual int char_out_internal(char dat, std::string name=""); 00105 00106 /** \brief Output a double variable 00107 */ 00108 virtual int double_out(double dat, std::string name=""); 00109 00110 /** \brief Output a float variable 00111 */ 00112 virtual int float_out(float dat, std::string name=""); 00113 00114 /** \brief Output an int variable 00115 */ 00116 virtual int int_out(int dat, std::string name=""); 00117 00118 /** \brief Output an long variable 00119 */ 00120 virtual int long_out(unsigned long int dat, std::string name=""); 00121 00122 /** \brief Output a string 00123 */ 00124 virtual int string_out(std::string dat, std::string name=""); 00125 00126 /** \brief Output a word 00127 */ 00128 virtual int word_out(std::string dat, std::string name=""); 00129 00130 /** \brief Start object output 00131 */ 00132 virtual int start_object(std::string type, std::string name); 00133 00134 /** \brief End object output 00135 */ 00136 virtual int end_object(); 00137 00138 /** \brief End line 00139 00140 This calls flush() in case the current string buffer is 00141 larger than the width, and then outputs the remaining 00142 characters and creates a new stringstream. 00143 */ 00144 virtual int end_line(); 00145 00146 /** \brief Output initialization */ 00147 virtual int init_file(); 00148 00149 /// Desc 00150 virtual int clean_up() { 00151 return 0; 00152 } 00153 00154 /** \brief Output a comment (only for text files) */ 00155 int comment_out(std::string comment); 00156 00157 /// Add brackets and replace carriage returns with spaces 00158 std::string reformat_string(std::string in); 00159 00160 #ifndef DOXYGEN_INTERNAL 00161 00162 protected: 00163 00164 friend class collection; 00165 00166 /// Desc 00167 bool extra_brackets; 00168 00169 /** \brief A list to indicate if the current object and subobjects 00170 are "hard-coded" 00171 */ 00172 std::stack<bool> hcs; 00173 00174 /// True if the constructor was called with a string, false otherwise 00175 bool from_string; 00176 00177 /// True if the file is to be compressed 00178 bool compressed; 00179 00180 /// True if the file is to be compressed with gzip 00181 bool gzip; 00182 00183 /// The width of the file 00184 int file_width; 00185 00186 /// The output stream 00187 std::ostream *outs; 00188 00189 /// A pointer to an output stream to define output properties 00190 std::ostream *props; 00191 00192 /// The temporary buffer as a stringstream 00193 std::ostringstream *strout; 00194 00195 /// The user-specified filename 00196 std::string user_filename; 00197 00198 /// The temporary filename used 00199 std::string temp_filename; 00200 00201 /// Flush the string buffer if the current string is long enough 00202 virtual int flush(); 00203 00204 /// If true, then \c type is a "hard-coded" type 00205 bool is_hc_type(std::string type); 00206 00207 #endif 00208 00209 }; 00210 00211 /** 00212 \brief An input text file 00213 00214 This class is experimental. 00215 00216 <b>Note:</b> Text files are not entirely architecture-independent, 00217 For example, a larger integer will not be read correctly on small 00218 integer systems. 00219 */ 00220 class text_in_file : public in_file_format { 00221 public: 00222 00223 /** \brief Use input stream \c in_file for text input */ 00224 text_in_file(std::istream *in_file); 00225 00226 /** \brief Read an input file with name \c file_name */ 00227 text_in_file(std::string file_name); 00228 00229 virtual ~text_in_file(); 00230 00231 virtual int bool_in(bool &dat, std::string name=""); 00232 virtual int char_in(char &dat, std::string name=""); 00233 virtual int double_in(double &dat, std::string name=""); 00234 virtual int float_in(float &dat, std::string name=""); 00235 virtual int int_in(int &dat, std::string name=""); 00236 virtual int long_in(unsigned long int &dat, std::string name=""); 00237 virtual int string_in(std::string &dat, std::string name=""); 00238 virtual int word_in(std::string &dat, std::string name=""); 00239 00240 virtual int start_object(std::string &type, std::string &name); 00241 virtual int skip_object(); 00242 virtual int end_object(); 00243 00244 /** \brief Initialize file input */ 00245 virtual int init_file(); 00246 00247 /// Finish file input 00248 virtual int clean_up() { 00249 return 0; 00250 } 00251 00252 /// Add brackets and replace carriage returns with spaces 00253 std::string reformat_string(std::string in); 00254 00255 #ifndef DOXYGEN_INTERNAL 00256 00257 protected: 00258 00259 /// If true, then \c type is a "hard-coded" type 00260 bool is_hc_type(std::string type); 00261 00262 /** \brief A list to indicate if the current object and subobjects 00263 are "hard-coded" 00264 */ 00265 std::stack<bool> hcs; 00266 00267 /// The input stream 00268 std::istream *ins; 00269 00270 /// True if the string version of the constructor was called 00271 bool from_string; 00272 00273 /// A version of word_in() which doesn't call the error handler 00274 virtual int word_in_noerr(std::string &dat, std::string name="") { 00275 std::string tmp; 00276 if (name.length()>0) (*ins) >> tmp; 00277 if ((*ins) >> dat) { 00278 return 0; 00279 } 00280 return -1; 00281 } 00282 00283 #endif 00284 00285 }; 00286 00287 #ifndef DOXYGENP 00288 } 00289 #endif 00290 00291 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page