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 why the collection class is a friend 00044 00045 \note 00046 - Text files are not entirely architecture-independent, For 00047 example, a larger integer will not be read correctly on small 00048 integer systems. 00049 - Long integer I/O is experimental 00050 */ 00051 class text_out_file : public out_file_format { 00052 public: 00053 00054 text_out_file(); 00055 00056 /** \brief Use output stream \c out_file for text output 00057 00058 This constructor assumes that the I/O properties of 00059 \c out_file have already been set. 00060 00061 Note that the stream \c out_file should not have 00062 been opened in binary mode, and errors will likely occur 00063 if this is the case. 00064 00065 \todo Ensure streams are not opened in binary mode 00066 for safety. 00067 */ 00068 text_out_file(std::ostream *out_file, int width=80, 00069 bool bracket_objs=true); 00070 00071 /** \brief Create an output file with name \c file_name 00072 00073 If \c prop is not 0 (i.e. not NULL), then the I/O properties 00074 (precision, fill, flags, etc) for the newly created file are 00075 taken to be the same as \c prop . 00076 00077 */ 00078 text_out_file(std::string file_name, std::ostream *prop=0, 00079 bool append=false, int width=80, 00080 bool bracket_objs=true); 00081 00082 virtual ~text_out_file(); 00083 00084 /** \brief Use stream \c out_file for text output 00085 */ 00086 virtual int open(std::ostream *out_file, int width=80, 00087 bool bracket_objs=true); 00088 00089 /** \brief Create an output file with name \c file_name 00090 */ 00091 virtual int open(std::string file_name, std::ostream *prop=0, 00092 bool append=false, int width=80, 00093 bool bracket_objs=true); 00094 00095 /// Close the file or stream 00096 virtual int close(); 00097 00098 /** \brief Output a bool variable 00099 */ 00100 virtual int bool_out(bool dat, std::string name=""); 00101 00102 /** \brief Output a char variable 00103 */ 00104 virtual int char_out(char dat, std::string name=""); 00105 00106 /** \brief Output a char variable 00107 */ 00108 virtual int char_out_internal(char dat, std::string name=""); 00109 00110 /** \brief Output a double variable 00111 */ 00112 virtual int double_out(double dat, std::string name=""); 00113 00114 /** \brief Output a float variable 00115 */ 00116 virtual int float_out(float dat, std::string name=""); 00117 00118 /** \brief Output an int variable 00119 */ 00120 virtual int int_out(int dat, std::string name=""); 00121 00122 /** \brief Output a long integer variable 00123 */ 00124 virtual int long_out(unsigned long int dat, std::string name=""); 00125 00126 /** \brief Output a string 00127 */ 00128 virtual int string_out(std::string dat, std::string name=""); 00129 00130 /** \brief Output a word 00131 */ 00132 virtual int word_out(std::string dat, std::string name=""); 00133 00134 /** \brief Start object output 00135 */ 00136 virtual int start_object(std::string type, std::string name); 00137 00138 /** \brief End object output 00139 */ 00140 virtual int end_object(); 00141 00142 /** \brief End line 00143 00144 This calls flush() in case the current string buffer is 00145 larger than the width, and then outputs the remaining 00146 characters and creates a new stringstream. 00147 */ 00148 virtual int end_line(); 00149 00150 /** \brief Output initialization */ 00151 virtual int init_file(); 00152 00153 /// Perform any final write commands before the end of file 00154 virtual int clean_up() { 00155 return 0; 00156 } 00157 00158 /** \brief Output a comment (only for text files) */ 00159 int comment_out(std::string comment); 00160 00161 /// Add brackets and replace carriage returns with spaces 00162 std::string reformat_string(std::string in); 00163 00164 #ifndef DOXYGEN_INTERNAL 00165 00166 protected: 00167 00168 friend class collection; 00169 00170 /// If true, add extra brackets 00171 bool extra_brackets; 00172 00173 /** \brief A list to indicate if the current object and subobjects 00174 are "hard-coded" 00175 */ 00176 std::stack<bool> hcs; 00177 00178 /// True if the constructor was called with a string, false otherwise 00179 bool from_string; 00180 00181 /// True if the file is to be compressed 00182 bool compressed; 00183 00184 /// True if the file is to be compressed with gzip 00185 bool gzip; 00186 00187 /// The width of the file 00188 int file_width; 00189 00190 /// The output stream 00191 std::ostream *outs; 00192 00193 /// A pointer to an output stream to define output properties 00194 std::ostream *props; 00195 00196 /// The temporary buffer as a stringstream 00197 std::ostringstream *strout; 00198 00199 /// The user-specified filename 00200 std::string user_filename; 00201 00202 /// The temporary filename used 00203 std::string temp_filename; 00204 00205 /// Flush the string buffer if the current string is long enough 00206 virtual int flush(); 00207 00208 /// If true, then \c type is a "hard-coded" type 00209 bool is_hc_type(std::string type); 00210 00211 #endif 00212 00213 }; 00214 00215 /** 00216 \brief An input text file 00217 00218 This class is experimental. 00219 00220 <b>Note:</b> Text files are not entirely architecture-independent, 00221 For example, a larger integer will not be read correctly on small 00222 integer systems. 00223 */ 00224 class text_in_file : public in_file_format { 00225 public: 00226 00227 /** \brief Use input stream \c in_file for text input */ 00228 text_in_file(std::istream *in_file); 00229 00230 /** \brief Read an input file with name \c file_name */ 00231 text_in_file(std::string file_name); 00232 00233 virtual ~text_in_file(); 00234 00235 /// Input a bool variable 00236 virtual int bool_in(bool &dat, std::string name=""); 00237 00238 /// Input a char variable 00239 virtual int char_in(char &dat, std::string name=""); 00240 00241 /// Input a double variable 00242 virtual int double_in(double &dat, std::string name=""); 00243 00244 /// Input a float variable 00245 virtual int float_in(float &dat, std::string name=""); 00246 00247 /// Input a integer variable 00248 virtual int int_in(int &dat, std::string name=""); 00249 00250 /// Input a long integer variable 00251 virtual int long_in(unsigned long int &dat, std::string name=""); 00252 00253 /// Input a string variable 00254 virtual int string_in(std::string &dat, std::string name=""); 00255 00256 /// Input a word variable 00257 virtual int word_in(std::string &dat, std::string name=""); 00258 00259 /// Start to read an object 00260 virtual int start_object(std::string &type, std::string &name); 00261 00262 /// Skip to the next object 00263 virtual int skip_object(); 00264 00265 /// Finish reading this object 00266 virtual int end_object(); 00267 00268 /** \brief Initialize file input */ 00269 virtual int init_file(); 00270 00271 /// Finish file input 00272 virtual int clean_up() { 00273 return 0; 00274 } 00275 00276 /// Add brackets and replace carriage returns with spaces 00277 std::string reformat_string(std::string in); 00278 00279 #ifndef DOXYGEN_INTERNAL 00280 00281 protected: 00282 00283 /// If true, then \c type is a "hard-coded" type 00284 bool is_hc_type(std::string type); 00285 00286 /** \brief A list to indicate if the current object and subobjects 00287 are "hard-coded" 00288 */ 00289 std::stack<bool> hcs; 00290 00291 /// The input stream 00292 std::istream *ins; 00293 00294 /// True if the string version of the constructor was called 00295 bool from_string; 00296 00297 /// A version of word_in() which doesn't call the error handler 00298 virtual int word_in_noerr(std::string &dat, std::string name="") { 00299 std::string tmp; 00300 if (name.length()>0) (*ins) >> tmp; 00301 if ((*ins) >> dat) { 00302 return 0; 00303 } 00304 return -1; 00305 } 00306 00307 #endif 00308 00309 }; 00310 00311 #ifndef DOXYGENP 00312 } 00313 #endif 00314 00315 #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