00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. 00006 \\/ M anipulation | 00007 ------------------------------------------------------------------------------- 00008 License 00009 This file is part of OpenFOAM. 00010 00011 OpenFOAM is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU General Public License as published by 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your option) any later version. 00015 00016 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00019 for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. 00023 00024 \*---------------------------------------------------------------------------*/ 00025 00026 #include <OpenFOAM/error.H> 00027 #include "OSstream.H" 00028 #include <OpenFOAM/token.H> 00029 00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00031 00032 Foam::Ostream& Foam::OSstream::write(const token&) 00033 { 00034 return *this; 00035 } 00036 00037 00038 Foam::Ostream& Foam::OSstream::write(const char c) 00039 { 00040 os_ << c; 00041 if (c == token::NL) 00042 { 00043 lineNumber_++; 00044 } 00045 setState(os_.rdstate()); 00046 return *this; 00047 } 00048 00049 00050 Foam::Ostream& Foam::OSstream::write(const char* str) 00051 { 00052 lineNumber_ += string(str).count(token::NL); 00053 os_ << str; 00054 setState(os_.rdstate()); 00055 return *this; 00056 } 00057 00058 00059 Foam::Ostream& Foam::OSstream::write(const word& str) 00060 { 00061 os_ << str; 00062 setState(os_.rdstate()); 00063 return *this; 00064 } 00065 00066 00067 Foam::Ostream& Foam::OSstream::write(const string& str) 00068 { 00069 os_ << token::BEGIN_STRING; 00070 00071 register int backslash = 0; 00072 for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter) 00073 { 00074 register char c = *iter; 00075 00076 if (c == '\\') 00077 { 00078 backslash++; 00079 // suppress output until we know if other characters follow 00080 continue; 00081 } 00082 else if (c == token::NL) 00083 { 00084 lineNumber_++; 00085 backslash++; // backslash escape for newline 00086 } 00087 else if (c == token::END_STRING) 00088 { 00089 backslash++; // backslash escape for quote 00090 } 00091 00092 // output pending backslashes 00093 while (backslash) 00094 { 00095 os_ << '\\'; 00096 backslash--; 00097 } 00098 00099 os_ << c; 00100 } 00101 00102 // silently drop any trailing backslashes 00103 // they would otherwise appear like an escaped end-quote 00104 00105 os_ << token::END_STRING; 00106 00107 setState(os_.rdstate()); 00108 return *this; 00109 } 00110 00111 00112 Foam::Ostream& Foam::OSstream::writeQuoted 00113 ( 00114 const std::string& str, 00115 const bool quoted 00116 ) 00117 { 00118 if (quoted) 00119 { 00120 os_ << token::BEGIN_STRING; 00121 00122 register int backslash = 0; 00123 for 00124 ( 00125 string::const_iterator iter = str.begin(); 00126 iter != str.end(); 00127 ++iter 00128 ) 00129 { 00130 register char c = *iter; 00131 00132 if (c == '\\') 00133 { 00134 backslash++; 00135 // suppress output until we know if other characters follow 00136 continue; 00137 } 00138 else if (c == token::NL) 00139 { 00140 lineNumber_++; 00141 backslash++; // backslash escape for newline 00142 } 00143 else if (c == token::END_STRING) 00144 { 00145 backslash++; // backslash escape for quote 00146 } 00147 00148 // output pending backslashes 00149 while (backslash) 00150 { 00151 os_ << '\\'; 00152 backslash--; 00153 } 00154 00155 os_ << c; 00156 } 00157 00158 // silently drop any trailing backslashes 00159 // they would otherwise appear like an escaped end-quote 00160 os_ << token::END_STRING; 00161 } 00162 else 00163 { 00164 // output unquoted string, only advance line number on newline 00165 lineNumber_ += string(str).count(token::NL); 00166 os_ << str; 00167 } 00168 00169 setState(os_.rdstate()); 00170 return *this; 00171 } 00172 00173 00174 Foam::Ostream& Foam::OSstream::write(const label val) 00175 { 00176 os_ << val; 00177 setState(os_.rdstate()); 00178 return *this; 00179 } 00180 00181 00182 Foam::Ostream& Foam::OSstream::write(const floatScalar val) 00183 { 00184 os_ << val; 00185 setState(os_.rdstate()); 00186 return *this; 00187 } 00188 00189 00190 Foam::Ostream& Foam::OSstream::write(const doubleScalar val) 00191 { 00192 os_ << val; 00193 setState(os_.rdstate()); 00194 return *this; 00195 } 00196 00197 00198 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count) 00199 { 00200 if (format() != BINARY) 00201 { 00202 FatalIOErrorIn("Ostream::write(const char*, std::streamsize)", *this) 00203 << "stream format not binary" 00204 << abort(FatalIOError); 00205 } 00206 00207 os_ << token::BEGIN_LIST; 00208 os_.write(buf, count); 00209 os_ << token::END_LIST; 00210 00211 setState(os_.rdstate()); 00212 00213 return *this; 00214 } 00215 00216 00217 void Foam::OSstream::indent() 00218 { 00219 for (register unsigned short i = 0; i < indentLevel_*indentSize_; i++) 00220 { 00221 os_ << ' '; 00222 } 00223 } 00224 00225 00226 void Foam::OSstream::flush() 00227 { 00228 os_.flush(); 00229 } 00230 00231 00232 // Add carriage return and flush stream 00233 void Foam::OSstream::endl() 00234 { 00235 write('\n'); 00236 os_.flush(); 00237 } 00238 00239 00240 // Get flags of output stream 00241 std::ios_base::fmtflags Foam::OSstream::flags() const 00242 { 00243 return os_.flags(); 00244 } 00245 00246 00247 // Set flags of output stream 00248 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f) 00249 { 00250 return os_.flags(f); 00251 } 00252 00253 00254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00255 00256 00257 // Get width of output field 00258 int Foam::OSstream::width() const 00259 { 00260 return os_.width(); 00261 } 00262 00263 // Set width of output field (and return old width) 00264 int Foam::OSstream::width(const int w) 00265 { 00266 return os_.width(w); 00267 } 00268 00269 // Get precision of output field 00270 int Foam::OSstream::precision() const 00271 { 00272 return os_.precision(); 00273 } 00274 00275 // Set precision of output field (and return old precision) 00276 int Foam::OSstream::precision(const int p) 00277 { 00278 return os_.precision(p); 00279 } 00280 00281 00282 // ************************ vim: set sw=4 sts=4 et: ************************ //