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 A collection file is simply a text file containing a list of 00042 objects specially formatted for input and output. Each entry in 00043 the text file is of the form: 00044 00045 object_type object_name object_version word_number word1 word2 word3 ... 00046 00047 The type, name, and version are all strings that contain no 00048 whitespace. "a_name" is a valid name but, "a name" is not. 00049 00050 Parts of the object definition may be separated by any amount of 00051 whitespace, with the exception of 'strings'. 00052 00053 The collection file may contain comments, which are lines that begin 00054 with the '#' character. Comments may last more than one line (so long as 00055 every line begins with '#'), but they may not occur in the middle of 00056 an object definition. 00057 \verbatim 00058 # comment 1 00059 double a 4.0 00060 double[] c 4 00061 4.5 3.4 2.3 4.2 00062 # comment 2 00063 double b 5.0 00064 \endverbatim 00065 is acceptable, but 00066 \verbatim 00067 # comment 1 00068 double a 4.0 00069 double[] c 4 00070 4.5 3.4 2.3 4.2 00071 double b 00072 # comment 2 00073 5.0 00074 \endverbatim 00075 is not, since the second comment occurs in the middle of the definition 00076 for the object named 'b'. 00077 00078 Normal variable: 00079 type name version word data1 ... data2 00080 00081 Object containing pointer: 00082 type name version word data1 ... ptr_type ptr_name ... data2 00083 00084 where ptr_type is the type of the object being pointed to 00085 and ptr_name is the name of the object. If it's not in the 00086 list, then the object is assigned a unique name of the 00087 form ptrX where 'X' is an integer >= 0. 00088 00089 Static objects are the same, except they are preceeded by 00090 the keyword \c static and do not have a name associated with them. 00091 00092 This is useful for output to text files and to std::cout, i.e. 00093 text_out_file tf(&cout); 00094 00095 \todo Create a real "close" function so that files can be closed 00096 at the right time 00097 00098 <b>Note:</b> Text files are not entirely architecture-independent, 00099 For example, a larger integer will not be read correctly on small 00100 integer systems. 00101 */ 00102 class text_out_file : public out_file_format { 00103 public: 00104 00105 /** \brief Use output stream \c out_file for text output 00106 00107 This constructor assumes that the I/O properties of 00108 \c out_file have already been set. 00109 00110 Note that the stream \c out_file should not have 00111 been opened in binary mode, and errors will likely occur 00112 if this is the case. 00113 00114 \todo Ensure streams are not opened in binary mode 00115 for safety. 00116 */ 00117 text_out_file(std::ostream *out_file, int width=80); 00118 00119 /** \brief Create an output file with name \c file_name 00120 00121 If \c prop is not null, then the I/O properties 00122 (precision, fill, flags, etc) for the newly created file are 00123 taken to be the same as \c prop . 00124 00125 */ 00126 text_out_file(std::string file_name, std::ostream *prop=NULL, 00127 bool append=false, int width=80); 00128 00129 virtual ~text_out_file(); 00130 00131 /** \brief Output a bool variable 00132 */ 00133 virtual int bool_out(bool dat, std::string name=""); 00134 00135 /** \brief Output a char variable 00136 */ 00137 virtual int char_out(char dat, std::string name=""); 00138 00139 /** \brief Output a char variable 00140 */ 00141 virtual int char_out_internal(char dat, std::string name=""); 00142 00143 /** \brief Output a double variable 00144 */ 00145 virtual int double_out(double dat, std::string name=""); 00146 00147 /** \brief Output a float variable 00148 */ 00149 virtual int float_out(float dat, std::string name=""); 00150 00151 /** \brief Output an int variable 00152 */ 00153 virtual int int_out(int dat, std::string name=""); 00154 00155 /** \brief Output an long variable 00156 */ 00157 virtual int long_out(unsigned long int dat, std::string name=""); 00158 00159 /** \brief Output a string 00160 */ 00161 virtual int string_out(std::string dat, std::string name=""); 00162 00163 /** \brief Output a word 00164 */ 00165 virtual int word_out(std::string dat, std::string name=""); 00166 00167 /** \brief Start object output 00168 */ 00169 virtual int start_object(std::string type, std::string name); 00170 00171 /** \brief End object output 00172 */ 00173 virtual int end_object(); 00174 00175 /** \brief End line 00176 */ 00177 virtual int end_line(); 00178 00179 /** \brief Output initialization */ 00180 virtual int init_file(); 00181 00182 virtual int clean_up() { 00183 return 0; 00184 } 00185 00186 /** \brief Output a comment (only for text files) */ 00187 int comment_out(std::string comment); 00188 00189 /// Add brackets and replace carriage returns with spaces 00190 std::string reformat_string(std::string in); 00191 00192 #ifndef DOXYGEN_INTERNAL 00193 00194 protected: 00195 00196 friend class collection; 00197 00198 /** \brief A list to indicate if the current object and subobjects 00199 are "hard-coded" 00200 */ 00201 std::stack<bool> hcs; 00202 00203 /// True if the constructor was called with a string, false otherwise 00204 bool from_string; 00205 00206 /// True if the file is to be compressed 00207 bool compressed; 00208 00209 /// True if the file is to be compressed with gzip 00210 bool gzip; 00211 00212 /// The width of the file 00213 int file_width; 00214 00215 /// The output stream 00216 std::ostream *outs; 00217 00218 /// A pointer to an output stream to define output properties 00219 std::ostream *props; 00220 00221 /// The temporary buffer as a stringstream 00222 std::ostringstream *strout; 00223 00224 /// The user-specified filename 00225 std::string user_filename; 00226 00227 /// The temporary filename used 00228 std::string temp_filename; 00229 00230 /// Flush the string buffer 00231 virtual int flush(); 00232 00233 /// If true, then \c type is a "hard-coded" type 00234 bool is_hc_type(std::string type); 00235 00236 #endif 00237 00238 }; 00239 00240 /** 00241 \brief An input text file 00242 00243 This class is experimental. 00244 00245 <b>Note:</b> Text files are not entirely architecture-independent, 00246 For example, a larger integer will not be read correctly on small 00247 integer systems. 00248 */ 00249 class text_in_file : public in_file_format { 00250 public: 00251 00252 /** \brief Use input stream \c in_file for text input */ 00253 text_in_file(std::istream *in_file); 00254 00255 /** \brief Read an input file with name \c file_name */ 00256 text_in_file(std::string file_name); 00257 00258 virtual ~text_in_file(); 00259 00260 virtual int bool_in(bool &dat, std::string name=""); 00261 virtual int char_in(char &dat, std::string name=""); 00262 virtual int double_in(double &dat, std::string name=""); 00263 virtual int float_in(float &dat, std::string name=""); 00264 virtual int int_in(int &dat, std::string name=""); 00265 virtual int long_in(unsigned long int &dat, std::string name=""); 00266 virtual int string_in(std::string &dat, std::string name=""); 00267 virtual int word_in(std::string &dat, std::string name=""); 00268 00269 virtual int start_object(std::string &type, std::string &name); 00270 virtual int skip_object(); 00271 virtual int end_object(); 00272 00273 /** \brief Initialize file input */ 00274 virtual int init_file(); 00275 00276 /// Finish file input 00277 virtual int clean_up() { 00278 return 0; 00279 } 00280 00281 /// Add brackets and replace carriage returns with spaces 00282 std::string reformat_string(std::string in); 00283 00284 #ifndef DOXYGEN_INTERNAL 00285 00286 protected: 00287 00288 /// If true, then \c type is a "hard-coded" type 00289 bool is_hc_type(std::string type); 00290 00291 /** \brief A list to indicate if the current object and subobjects 00292 are "hard-coded" 00293 */ 00294 std::stack<bool> hcs; 00295 00296 /// The input stream 00297 std::istream *ins; 00298 00299 /// True if the string version of the constructor was called 00300 bool from_string; 00301 00302 /// A version of word_in() which doesn't call the error handler 00303 virtual int word_in_noerr(std::string &dat, std::string name="") { 00304 std::string tmp; 00305 if (name.length()>0) (*ins) >> tmp; 00306 if ((*ins) >> dat) { 00307 return 0; 00308 } 00309 return -1; 00310 } 00311 00312 #endif 00313 00314 }; 00315 00316 #ifndef DOXYGENP 00317 } 00318 #endif 00319 00320 #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