TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Entree_Fichier_base.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 <Entree_Fichier_base.h>
17#include <Process.h>
18#include <Nom.h>
19#include <sys/stat.h>
20#ifndef LATATOOLS
21#include <EntreeSortie.h>
22#endif
23
24Implemente_base_sans_constructeur_ni_destructeur(Entree_Fichier_base,"Entree_Fichier_base",Objet_U);
25
27{
28 throw;
29}
30
32{
33 throw;
34}
39
40bool fileExists(const char* name)
41{
42 struct stat buffer;
43 if (stat(name, &buffer) == 0 && S_ISREG(buffer.st_mode))
44 return true;
45 else
46 return false;
47}
48Entree_Fichier_base::Entree_Fichier_base(const char* name,IOS_OPEN_MODE mode)
49{
50 ifstream_ = new ifstream(name, mode);
51 if(ifstream_->fail() || !fileExists(name))
52 {
53 Cerr << "Error while opening the file " << name << finl;
55 }
57}
58
60{
61 return *ifstream_;
62}
63
64Entree_Fichier_base::~Entree_Fichier_base()
65{
67}
68
69int Entree_Fichier_base::ouvrir(const char* name, IOS_OPEN_MODE mode)
70{
71 if(ifstream_)
72 delete ifstream_;
73 IOS_OPEN_MODE ios_mod=mode;
74 if (bin_)
75 {
76 ios_mod=ios_mod|ios::binary;
77 }
78 ifstream_ = new ifstream(name,ios_mod);
79 int ok = ifstream_->good() && fileExists(name);
81
82 if (bin_)
83 {
84 Nom test;
85 (*this) >> test;
86 if (test == "INT64")
87 {
88 is_64b_ = true;
89#ifndef INT_is_64_
90 Cerr<<"Opening " <<name<< " which is an int64 binary file..."<<finl;
91#endif
92 }
93 else
94 {
95 is_64b_ = false;
96#ifdef INT_is_64_
97 Cerr<<"Opening " <<name<< " which is an int32 binary file..."<<finl;
98#endif
99 // rewind, to go back at begining of file:
100 delete ifstream_;
101 ifstream_ = new ifstream(name,ios_mod);
102 ok = ifstream_->good() && fileExists(name);
104 }
105 }
106 return ok;
107}
108
110{
111 if(ifstream_)
112 {
113 ifstream_->close();
114 // do not destroy ifstream here, it will be done by Entree
115 // delete ifstream_;
116 }
117}
118
120{
121 if(ifstream_)
122 return ifstream_->eof();
123 else
124 return -1;
125}
126
128{
129 if(ifstream_)
130 return ifstream_->fail();
131 else
132 return -1;
133}
134
136{
137 if(ifstream_)
138 return ifstream_->good();
139 else
140 return -1;
141}
142
144{
145 if(ifstream_)
146 ifstream_->precision(pre);
147}
148
149void Entree_Fichier_base::setf(IOS_FORMAT code)
150{
151 if(ifstream_)
152 ifstream_->setf(code);
153}
154
155/*! @brief Return True if the file can be opened for reading, false otherwise.
156 *
157 */
158bool Entree_Fichier_base::Can_be_read(const char * name)
159{
160 std::ifstream ifs(name,ios::in);
161 return ifs.good();
162}
163
bool bin_
Is this a binary flux?
Definition AbstractIO.h:50
bool is_64b_
Will we be reading/writing in 64b? (Init in ctor to avoid including arch.h probably).
Definition AbstractIO.h:51
File for reading. This class is to the C++ ifstream class what the Entree class is to the.
static bool Can_be_read(const char *name)
Return True if the file can be opened for reading, false otherwise.
void setf(IOS_FORMAT code)
virtual int ouvrir(const char *name, IOS_OPEN_MODE mode=ios::in)
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
void set_istream(istream *is)
Definition Entree.cpp:50
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
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
Base class for output streams.
Definition Sortie.h:52