o_string_stream.h
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 00020 #ifndef LIBMESH_OSTRINGSTREAM_H 00021 #define LIBMESH_OSTRINGSTREAM_H 00022 00023 00024 00025 // Mesh configuration options 00026 #include "libmesh/libmesh_config.h" 00027 00028 00029 00030 #ifdef LIBMESH_HAVE_SSTREAM 00031 00032 // C++ includes 00033 #include <sstream> 00034 00035 // Local Includes 00036 #include "libmesh/libmesh_common.h" 00037 00038 00039 // Forward Declarations 00040 00041 00042 00043 /* 00044 * Some compilers, at least HP \p aCC do not even 00045 * accept empty classes derived from \p std::ostringstream. 00046 * Therefore, resort to preprocessor definitions. 00047 */ 00048 00049 #ifndef LIBMESH_BROKEN_IOSTREAM 00050 /* 00051 * --------------------------------------------------------------------------------- 00052 * Everything for a clean iostream 00053 */ 00054 00055 # include <iomanip> 00056 00057 /* 00058 * Outputs \p std::string \p d with width 00059 * \p v left-formatted to stream \p o. 00060 */ 00061 # define OSSStringleft(o,v,d) (o).width(v); (o) << std::left << (d) 00062 00063 /* 00064 * Outputs \p std::string \p d with width 00065 * \p v right-formatted to stream \p o. 00066 */ 00067 # define OSSStringright(o,v,d) (o).width(v); (o) << std::right << (d) 00068 00069 /* 00070 * Outputs \p Real \p d with width \p v and 00071 * precision \p p to stream \p o (padded with 00072 * whitespaces). 00073 */ 00074 # define OSSRealleft(o,v,p,d) (o).width(v); (o).precision(p); (o).fill(' '); (o) << (d) 00075 00076 /* 00077 * Outputs \p Real \p d with width \p v and 00078 * precision \p p to stream \p o (padded with 00079 * zeros). 00080 */ 00081 # define OSSRealzeroleft(o,v,p,d) (o).width(v); (o).precision(p); (o).fill('0'); (o) << std::showpoint << std::left << (d) 00082 00083 /* 00084 * Outputs \p Real \p d with width \p v and 00085 * precision \p p to stream \p o (padded with 00086 * whitespaces). 00087 */ 00088 # define OSSRealright(o,v,p,d) (o).width(v); (o).precision(p); (o).fill(' '); (o) << std::right << (d) 00089 00090 /* 00091 * Outputs \p Real \p d with width \p v and 00092 * precision \p p to stream \p o (padded with 00093 * zeros). 00094 */ 00095 # define OSSRealzeroright(o,v,p,d) (o).width(v); (o).precision(p); (o).fill('0'); (o) << std::right << (d) 00096 00097 /* 00098 * Outputs \p Real \p d with width \p v 00099 * in scientific format to stream \p o. 00100 */ 00101 # define OSSRealscientific(o,v,d) (o) << std::setw(v) << std::scientific << (d) 00102 00103 /* 00104 * Outputs \p int \p d with width 00105 * \p v to stream \p o. 00106 */ 00107 # define OSSInt(o,v,d) (o).width(v); (o) << (d) 00108 00109 /* 00110 * class alias 00111 */ 00112 //# define OStringStream std::ostringstream 00113 typedef std::ostringstream OStringStream; 00114 00115 00116 00117 00118 #else 00119 /* 00120 * --------------------------------------------------------------------------------- 00121 * Everything for broken iostream 00122 */ 00123 00124 # include <cstdio> 00125 00126 /* 00127 * Outputs \p std::string \p d with width 00128 * \p v left-formatted to stream \p o. 00129 */ 00130 # define OSSStringleft(o,v,d) (o).left( (v), (d) ) 00131 00132 /* 00133 * Outputs \p std::string \p d with width 00134 * \p v right-formatted to stream \p o. 00135 */ 00136 # define OSSStringright(o,v,d) (o).right( (v), (d) ) 00137 00138 /* 00139 * Outputs \p Real \p d with width \p v and 00140 * precision \p p to stream \p o (padded with 00141 * whitespaces). 00142 */ 00143 # define OSSRealleft(o,v,p,d) (o).left( (v), (p), (d) , ' ') 00144 00145 /* 00146 * Outputs \p Real \p d with width \p v and 00147 * precision \p p to stream \p o (padded with 00148 * zeros). 00149 */ 00150 # define OSSRealzeroleft(o,v,p,d) (o).left( (v), (p), (d) , '0') 00151 00152 /* 00153 * Outputs \p Real \p d with width \p v and 00154 * precision \p p to stream \p o (padded with 00155 * whitespaces). 00156 */ 00157 # define OSSRealright(o,v,p,d) (o).right( (v), (p), (d) ,' ') 00158 00159 /* 00160 * Outputs \p Real \p d with width \p v and 00161 * precision \p p to stream \p o (padded with 00162 * zeros). 00163 */ 00164 # define OSSRealzeroright(o,v,p,d) (o).right( (v), (p), (d) ,'0') 00165 00166 /* 00167 * Outputs \p Real \p d with width \p v 00168 * in scientific format to stream \p o. 00169 */ 00170 # define OSSRealscientific(o,v,d) (o).scientific( (v), (d) ) 00171 00172 /* 00173 * Outputs \p int \p d with width 00174 * \p v to stream \p o. 00175 */ 00176 # define OSSInt(o,v,d) (o).left( (v), (d) ) 00177 00178 // /* 00179 // * class alias 00180 // */ 00181 // # define OSSOStringStream OStringStream 00182 00183 00184 namespace libMesh 00185 { 00186 00187 00194 // ------------------------------------------------------------ 00195 // OStringStream class definition 00196 class OStringStream : public std::ostringstream 00197 { 00198 public: 00199 00203 OStringStream () : std::ostringstream() 00204 { 00205 // [JWP] TODO: I will remove this class at the next libmesh 00206 // release or on on March 21, 2013, whichever comes first. 00207 libmesh_deprecated(); 00208 } 00209 00213 ~OStringStream () {}; 00214 00218 typedef std::string::size_type sizetype; 00219 00225 void left (const sizetype w, 00226 const std::string& s); 00227 00233 void left (const sizetype w, 00234 const sizetype prec, 00235 const Real r, 00236 const char c = ' '); 00237 00244 void left (const sizetype w, 00245 const int n); 00246 00252 void right (const sizetype w, 00253 const std::string& s); 00254 00255 00262 void right (const sizetype w, 00263 const sizetype prec, 00264 const Real r, 00265 const char c = ' '); 00266 00272 void scientific (const sizetype w, 00273 const Real r); 00274 00275 protected: 00280 void print_ws (const sizetype n, 00281 const char c = ' '); 00282 00283 }; 00284 00285 00286 00287 // ------------------------------------------------------------ 00288 // OStringStream inline methods 00289 inline 00290 void OStringStream::left (const sizetype w, 00291 const std::string& s) 00292 { 00293 *this << s; 00294 // pad with whitespaces afterwards 00295 print_ws ((w-s.size())); 00296 } 00297 00298 00299 inline 00300 void OStringStream::left (const sizetype w, 00301 const sizetype prec, 00302 const Real r, 00303 const char c) 00304 { 00305 libmesh_assert_less (w, 30); 00306 char buf[30]; 00307 char format[8]; 00308 // form the format for r 00309 // ALTERNATIVE: sprintf (format, "%%%d.%df", int((w-prec)/2), prec); 00310 sprintf (format, "%%.%df", prec); 00311 // form string as desired 00312 sprintf (buf, format, r); 00313 *this << buf; 00314 // pad with whitespaces afterwards 00315 print_ws (w-std::string(buf).size(), c); 00316 // ALTERNATIVE: print_ws (w-int((w-prec)/2)); 00317 } 00318 00319 00320 inline 00321 void OStringStream::left (const sizetype w, 00322 const int n) 00323 { 00324 libmesh_assert_less (w, 30); 00325 char buf[30]; 00326 // form string as desired 00327 sprintf (buf, "%d", n); 00328 *this << buf; 00329 // pad with whitespaces afterwards 00330 print_ws (w-std::string(buf).size()); 00331 } 00332 00333 00334 inline 00335 void OStringStream::right (const sizetype w, 00336 const std::string& s) 00337 { 00338 // first pad with whitespaces 00339 print_ws ((w-s.size())); 00340 *this << s; 00341 } 00342 00343 00344 inline 00345 void OStringStream::right (const sizetype w, 00346 const sizetype prec, 00347 const Real r, 00348 const char c) 00349 { 00350 libmesh_assert_less (w, 30); 00351 char buf[30]; 00352 char format[8]; 00353 // form the format for r 00354 sprintf (format, "%%.%df", prec); 00355 // form string as desired 00356 sprintf (buf, format, r); 00357 // first pad with the user-defined char 00358 print_ws (w-std::string(buf).size(), c); 00359 // then print the float 00360 *this << buf; 00361 } 00362 00363 00364 inline 00365 void OStringStream::scientific (const sizetype w, 00366 const Real r) 00367 { 00368 libmesh_assert_less (w, 30); 00369 char buf[30]; 00370 char format[8]; 00371 // form the format for r 00372 sprintf (format, "%%%de", w); 00373 // form string as desired 00374 sprintf (buf, format, r); 00375 *this << buf; 00376 } 00377 00378 00379 inline 00380 void OStringStream::print_ws (const sizetype n, 00381 const char c) 00382 { 00383 for (sizetype i = 0; i < n; i++) 00384 *this << c; 00385 } 00386 00387 00388 } // namespace libMesh 00389 00390 #endif // ifndef ... else ... LIBMESH_BROKEN_IOSTREAM 00391 00392 00393 00394 00395 #endif // ifdef LIBMESH_HAVE_SSTREAM 00396 00397 #endif // LIBMESH_OSTRINGSTREAM_H 00398
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:48 UTC
Hosted By: