TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Sortie.cpp
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Objet_U.h>
17#include <Sortie.h>
18#include <Separateur.h>
19#include <iostream>
20#include <iomanip>
21#include <cstring>
22
24const Separateur tspace(Separateur::SPACE);
25
27 AbstractIO(),
28 col_width_(-1)
29{
30 // Constructor does **not** instanciate ostream_ - typically done in derived classes
31}
32
33
34void Sortie::setf(IOS_FORMAT code)
35{
36 if(ostream_)
37 ostream_->setf(code);
38}
39
40void Sortie::precision(int pre)
41{
42 if(ostream_)
43 ostream_->precision(pre);
44}
45
46Sortie::Sortie(ostream& os) : Sortie()
47{
48 if(os.rdbuf())
49 ostream_ = std::make_unique<ostream>(os.rdbuf());
50 else
52}
53
55{
56 if (os.has_ostream())
57 {
58 Cerr<<"we try to copy a Sortie with a non-empty std::ostream!!!"<<finl;
59 abort();
60 }
61}
62
63// Assignment operators
65{
66 // Make a new ostream_:
67 ostream_ = std::make_unique<ostream>(os.rdbuf());
68 return *this;
69}
70
72{
73 if (os.has_ostream())
74 {
75 Cerr<<"we try to copy a Sortie with a non-empty std::stream!!!"<<finl;
76 abort();
77 }
78 else // We are copying from a void Sortie - make sure we clean our side:
79 ostream_ = nullptr;
80 return *this;
81}
82
83int Sortie::add_col(const double ob)
84{
85 if (bin_ or col_width_ == -1)
86 abort();
87 else
88 (*ostream_) << std::right << std::setw(col_width_) << ob;
89 return ostream_->good();
90}
91
92int Sortie::add_col(const char * ob)
93{
94 if (bin_ or col_width_ == -1)
95 abort();
96 else
97 (*ostream_) << std::right << std::setw(col_width_) << ob;
98 return ostream_->good();
99}
100
101int Sortie::put(const unsigned * ob, std::streamsize n, std::streamsize nb_col) { return put_template<unsigned>(ob,n,nb_col); }
102int Sortie::put(const int * ob, std::streamsize n, std::streamsize nb_col) { return put_template<int>(ob,n,nb_col); }
103int Sortie::put(const long * ob, std::streamsize n, std::streamsize nb_col) { return put_template<long>(ob,n,nb_col); }
104int Sortie::put(const long long * ob, std::streamsize n, std::streamsize nb_col) { return put_template<long long>(ob,n,nb_col); }
105int Sortie::put(const float * ob, std::streamsize n, std::streamsize nb_col) { return put_template<float>(ob,n,nb_col); }
106int Sortie::put(const double * ob, std::streamsize n, std::streamsize nb_col) { return put_template<double>(ob,n,nb_col); }
107
108Sortie& Sortie::operator<<(const unsigned ob) { return operator_template<unsigned>(ob); }
109Sortie& Sortie::operator<<(const int ob) { return operator_template<int>(ob); }
110Sortie& Sortie::operator<<(const float ob) { return operator_template<float>(ob); }
111Sortie& Sortie::operator<<(const double ob) { return operator_template<double>(ob); }
112Sortie& Sortie::operator<<(const long ob) { return operator_template<long>(ob); }
113Sortie& Sortie::operator<<(const long long ob) { return operator_template<long long>(ob); }
114Sortie& Sortie::operator<<(const unsigned long ob) { return operator_template<unsigned long>(ob); }
115
116Sortie& Sortie::operator <<(ostream& (*f)(ostream&))
117{
118 // This is not ideal, but it is needed to allow "<< endl"
119 // Problem: it does not work if ostream_ == 0.
120 if(ostream_ && !bin_)
121 (*f)(*ostream_);
122 return *this;
123}
124
126{
127 (*f)(*this);
128 return *this;
129}
130
132{
133 if (ostream_)
134 (*f)(*ostream_);
135 return *this;
136}
137
139{
140 ostream_->flush();
141 return *this;
142}
143
145{
146 if (bin_)
147 {
148 // In binary mode, separators are not written
149 }
150 else
151 {
152 switch (ob.get_type())
153 {
154 case Separateur::ENDL:
155 // endl = '\n' + flush
156#if defined(__CYGWIN__) || defined(MICROSOFT)
157 // GF issue under Windows with old line
158 (*ostream_)<<endl;
159#else
160 (*ostream_) << '\n';
161#endif
162 // Optional flush (can be overridden by Sortie_Fichier_base::flush())
163 flush();
164 break;
166 (*ostream_) << ' ';
167 break;
168 }
169 }
170 return *this;
171}
172
174{
175 Cerr<<"Sortie::lockfile() : we should not go through it !!"<<finl;
177 return *this;
178}
179
181{
182 Cerr<<"Sortie::unlockfile() : we should not go through it !!"<<finl;
184 return *this;
185}
186
188{
189 Cerr<<"Sortie::syncfile() : we should not go through it !!"<<finl;
191 return *this;
192}
193
194/*! @brief Writes an object or variable.
195 *
196 * In this implementation (and in most derived classes), ob.printOn is simply called
197 * (except in Sortie_Nulle). Note: if the stream must be usable in either ASCII or BINARY mode,
198 * "<< space <<" or "<< finl <<" must be inserted between objects.
199 *
200 */
202{
203 ob.printOn(*this);
204 return *this;
205}
206
207/*! @brief Writes a character string.
208 *
209 * Note: to correctly re-read the string in ASCII mode, it must not contain
210 * any separator (neither space nor newline, ...).
211 *
212 */
214{
215 if(bin_)
216 {
217 // This is unfortunate: in LIST there is "<< blanc <<"
218 // which forces this test:
219 // One should use "<< space <<" but that would change the binary .Zones files...
220 if (strcmp(ob, " "))
221 {
222 const int n = (int)strlen(ob) + 1;
223 ostream_->write((char *) ob, n * sizeof(char));
224 }
225 }
226 else
227 {
228 (*ostream_) << ob;
229 }
230 // B.Mathieu, 7/10/2004: removing the space after ecrire(ob).
231 // Warning, now one must do
232 // fichier << "chaine" << space << nombre;
233 // to be able to re-read the file transparently with
234 // fichier >> motcle >> nombre;
235 // Ancien code:
236 // if(!bin_)
237 // ecrire(" ");
238 return *this;
239}
240
241Sortie& Sortie::operator <<(const std::string& str) { return (*this) << str.c_str(); }
242
243
244/*! @brief Changes the write mode of the file.
245 *
246 * This method can be called at any time. However, for Ecrire_Fichier_Partage files,
247 * the change must be made only at the beginning of a block write,
248 * immediately after syncfile() (otherwise, newlines are incorrectly translated
249 * during the next syncfile() call).
250 *
251 */
252void Sortie::set_bin(bool bin)
253{
254 bin_ = bin;
255 if (ostream_)
256 {
257 Cerr<<"Error you cant change binary format after open "<<finl;
258 assert(0);
260 }
261}
262
263/*! @brief Low-level method to write an array of ints or floats to the stream.
264 *
265 * In the base class implementation, writes to ostream_.
266 * In binary mode uses ostream::write(); in ASCII mode uses ostream::operator<<().
267 * In ASCII mode, a newline is inserted after every "nb_col" values and at the end of the array.
268 * Return value: ostream_->good().
269 *
270 */
271template<typename _TYPE_>
272int Sortie::put_template(const _TYPE_ *ob, std::streamsize n, std::streamsize nb_col)
273{
274 assert(n >= 0);
275 if (bin_)
276 {
278 {
279 // Need to cast, use '>>' operator - see doc in operator_template<>
280 for (int i = 0; i < n; i++) (*this) << ob[i];
281 }
282 else
283 {
284 // In binary, optimized block writing:
285 std::streamsize sz = sizeof(_TYPE_);
286 sz *= n;
287 ostream_->write((const char*) ob, sz);
288 }
289 }
290 else
291 {
292 std::streamsize j = nb_col;
293 for (std::streamsize i = 0; i < n; i++)
294 {
295 (*ostream_) << (ob[i]) << (' ');
296 j--;
297 if (j <= 0)
298 {
299 (*ostream_) << (endl);
300 j = nb_col;
301 }
302 }
303 // If we did not end with a newline, add one
304 if (j != nb_col && n > 0) (*ostream_) << (endl);
305
306 ostream_->flush();
307 }
308 return ostream_->good();
309}
310
311// Explicit instanciations
312template int Sortie::put_template(const unsigned *ob, std::streamsize n, std::streamsize nb_col);
313template int Sortie::put_template(const int *ob, std::streamsize n, std::streamsize nb_col);
314template int Sortie::put_template(const long *ob, std::streamsize n, std::streamsize nb_col);
315template int Sortie::put_template(const long long *ob, std::streamsize n, std::streamsize nb_col);
316template int Sortie::put_template(const float *ob, std::streamsize n, std::streamsize nb_col);
317template int Sortie::put_template(const double *ob, std::streamsize n, std::streamsize nb_col);
318
319/*! @brief Low-level method to write an int or float to the stream.
320 *
321 * In the base class implementation, writes to ostream_.
322 * In binary mode uses ostream::write(); in ASCII mode uses ostream::operator<<().
323 *
324 */
325template<typename _TYPE_>
326Sortie& Sortie::operator_template(const _TYPE_ &ob)
327{
328 if (bin_)
329 {
330 if (this->must_convert<_TYPE_>())
331 {
332 trustIdType val = static_cast<trustIdType>(ob);
333 ostream_->write((char*) &val, sizeof(trustIdType));
334 }
335 else
336 ostream_->write((char*) &ob, sizeof(_TYPE_));
337 }
338 else
339 (*ostream_) << ob;
340 return *this;
341}
342
343// Explicit instanciations
344template Sortie& Sortie::operator_template(const unsigned& ob);
345template Sortie& Sortie::operator_template(const int& ob);
346template Sortie& Sortie::operator_template(const long& ob);
347template Sortie& Sortie::operator_template(const long long& ob);
348template Sortie& Sortie::operator_template(const float& ob);
349template Sortie& Sortie::operator_template(const double& ob);
350template Sortie& Sortie::operator_template(const unsigned long& ob);
bool bin_
Is this a binary flux?
Definition AbstractIO.h:50
bool must_convert() const
Whether to convert an int into a long when reading/writing out data.
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
Separator for output streams.
Definition Separateur.h:29
const Type & get_type() const
Definition Separateur.h:33
Base class for output streams.
Definition Sortie.h:52
bool has_ostream() const
Definition Sortie.h:108
virtual Sortie & flush()
Definition Sortie.cpp:138
Sortie()
Definition Sortie.cpp:26
virtual int put(const unsigned *ob, std::streamsize n, std::streamsize nb_colonnes=1)
Definition Sortie.cpp:101
void set_bin(bool bin) override
Changes the write mode of the file.
Definition Sortie.cpp:252
virtual Sortie & unlockfile()
Definition Sortie.cpp:180
Sortie & operator<<(ostream &(*f)(ostream &))
Definition Sortie.cpp:116
virtual Sortie & lockfile()
Definition Sortie.cpp:173
virtual void precision(int)
Definition Sortie.cpp:40
int col_width_
Definition Sortie.h:111
Sortie & operator=(ostream &os)
Definition Sortie.cpp:64
virtual int add_col(const double ob)
Definition Sortie.cpp:83
virtual void setf(IOS_FORMAT)
Definition Sortie.cpp:34
virtual Sortie & syncfile()
Definition Sortie.cpp:187
std::unique_ptr< ostream > ostream_
Definition Sortie.h:116