TrioCFD 1.9.8
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// Operateurs d'affectation
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 // Ca c'est pas genial, c'est pour permettre "<< endl"
119 // Probleme: ca ne marche pas si 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 // En binaire on n'ecrit pas les separateurs
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 pb sous windows avec ancienne ligne
158 (*ostream_)<<endl;
159#else
160 (*ostream_) << '\n';
161#endif
162 // Flush eventuel (surcharge possible par 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 Ecriture d'un objet ou d'une variable.
195 *
196 * Dans cette implementation (et dans la plupart des classes derivees)
197 * on appelle simplement ob.printOn (a l'exception de Sortie_Nulle)
198 * Attention, si on veut que le flux puisse etre indifferemment ASCII ou BINAIRE,
199 * il faut inserer "<< space <<" ou "<< finl <<" pour separer les objets.
200 *
201 */
203{
204 ob.printOn(*this);
205 return *this;
206}
207
208/*! @brief Ecriture d'une chaine de caracteres.
209 *
210 * Attention, pour pouvoir relire correctement la chaine en mode ascii, celle-ci ne doit
211 * pas contenir de separateur (ni espace, ni retour a la ligne, ...)
212 *
213 */
215{
216 if(bin_)
217 {
218 // Ca c'est dommage : dans LIST, il y a "<< blanc <<"
219 // qui oblige a mettre ce test :
220 // Il faudrait mettre "<< space <<" mais cela change les fichiers
221 // .Zones binaires...
222 if (strcmp(ob, " "))
223 {
224 const int n = (int)strlen(ob) + 1;
225 ostream_->write((char *) ob, n * sizeof(char));
226 }
227 }
228 else
229 {
230 (*ostream_) << ob;
231 }
232 // B.Mathieu, 7/10/2004 : je supprime l'espace apres ecrire(ob).
233 // Attention, maintenant, il faut faire
234 // fichier << "chaine" << space << nombre;
235 // pour pouvoir relire le fichier de facon transparente avec
236 // fichier >> motcle >> nombre;
237 // Ancien code:
238 // if(!bin_)
239 // ecrire(" ");
240 return *this;
241}
242
243Sortie& Sortie::operator <<(const std::string& str) { return (*this) << str.c_str(); }
244
245
246/*! @brief Change le mode d'ecriture du fichier.
247 *
248 * Cette methode peut etre appelee n'importe quand. Attention
249 * cependant pour les fichiers Ecrire_Fichier_Partage :
250 * il faut faire le changement uniquement au debut de l'ecriture
251 * d'un bloc, juste apres syncfile() (sinon, mauvaise traduction
252 * des retours a la ligne lors du syncfile suivant).
253 *
254 */
255void Sortie::set_bin(bool bin)
256{
257 bin_ = bin;
258 if (ostream_)
259 {
260 Cerr<<"Error you cant change binary format after open "<<finl;
261 assert(0);
263 }
264}
265
266/*! @brief Methode de bas niveau pour ecrire un tableau d'ints ou reels dans le stream.
267 *
268 * Dans l'implementation de la classe de base, on ecrit dans ostream_.
269 * En binaire on utilise ostream::write(), en ascii ostream::operato<<()
270 * En ascii, on revient a la ligne chaque fois qu'on a ecrit "nb_col" valeurs et a la fin du tableau.
271 * Valeur de retour : ostream_->good()
272 *
273 */
274template<typename _TYPE_>
275int Sortie::put_template(const _TYPE_ *ob, std::streamsize n, std::streamsize nb_col)
276{
277 assert(n >= 0);
278 if (bin_)
279 {
281 {
282 // Need to cast, use '>>' operator - see doc in operator_template<>
283 for (int i = 0; i < n; i++) (*this) << ob[i];
284 }
285 else
286 {
287 // In binary, optimized block writing:
288 std::streamsize sz = sizeof(_TYPE_);
289 sz *= n;
290 ostream_->write((const char*) ob, sz);
291 }
292 }
293 else
294 {
295 std::streamsize j = nb_col;
296 for (std::streamsize i = 0; i < n; i++)
297 {
298 (*ostream_) << (ob[i]) << (' ');
299 j--;
300 if (j <= 0)
301 {
302 (*ostream_) << (endl);
303 j = nb_col;
304 }
305 }
306 // Si on n'a pas fini pas un retour a la ligne, en ajouter un
307 if (j != nb_col && n > 0) (*ostream_) << (endl);
308
309 ostream_->flush();
310 }
311 return ostream_->good();
312}
313
314// Explicit instanciations
315template int Sortie::put_template(const unsigned *ob, std::streamsize n, std::streamsize nb_col);
316template int Sortie::put_template(const int *ob, std::streamsize n, std::streamsize nb_col);
317template int Sortie::put_template(const long *ob, std::streamsize n, std::streamsize nb_col);
318template int Sortie::put_template(const long long *ob, std::streamsize n, std::streamsize nb_col);
319template int Sortie::put_template(const float *ob, std::streamsize n, std::streamsize nb_col);
320template int Sortie::put_template(const double *ob, std::streamsize n, std::streamsize nb_col);
321
322/*! @brief Methode de bas niveau pour ecrire un int ou flottant dans le stream.
323 *
324 * Dans l'implementation de la classe de base, on ecrit dans ostream_.
325 * En binaire on utilise ostream::write(), en ascii ostream::operator<<()
326 *
327 */
328template<typename _TYPE_>
329Sortie& Sortie::operator_template(const _TYPE_ &ob)
330{
331 if (bin_)
332 {
333 if (this->must_convert<_TYPE_>())
334 {
335 trustIdType val = static_cast<trustIdType>(ob);
336 ostream_->write((char*) &val, sizeof(trustIdType));
337 }
338 else
339 ostream_->write((char*) &ob, sizeof(_TYPE_));
340 }
341 else
342 (*ostream_) << ob;
343 return *this;
344}
345
346// Explicit instanciations
347template Sortie& Sortie::operator_template(const unsigned& ob);
348template Sortie& Sortie::operator_template(const int& ob);
349template Sortie& Sortie::operator_template(const long& ob);
350template Sortie& Sortie::operator_template(const long long& ob);
351template Sortie& Sortie::operator_template(const float& ob);
352template Sortie& Sortie::operator_template(const double& ob);
353template 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.
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Separateur pour les fichiers.
Definition Separateur.h:30
const Type & get_type() const
Definition Separateur.h:34
Classe de base des flux de sortie.
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
Change le mode d'ecriture du fichier.
Definition Sortie.cpp:255
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