Arcane  v3.16.6.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
IosFile.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* IosFile.cc (C) 2000-2025 */
9/* */
10/* Routines des Lecture/Ecriture d'un fichier. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/IosFile.h"
15
16#include "arcane/utils/Iostream.h"
17#include "arcane/utils/IOException.h"
18#include "arcane/utils/ITraceMng.h"
19#include "arcane/utils/Real3.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30bool IosFile::
31isEnd()
32{
33 (*m_stream) >> ws;
34 return m_stream->eof();
35}
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40const char* IosFile::
41getNextLine(const char* comment_char)
42{
43 while (m_stream->good()) {
44 m_stream->getline(m_buf, sizeof(m_buf) - 1);
45 if (m_stream->eof())
46 break;
47 if (m_buf[0] == '\n' || m_buf[0] == '\r')
48 continue;
49 bool is_comment = true; // Comments are searched for by default
50 if (!comment_char)
51 is_comment = false; // If none has been set, just skip their track of it
52 // Regarde si un caractère de commentaire est présent
53 for (int i = 0; is_comment && i < IOS_BFR_SZE && m_buf[i] != '\0'; ++i) {
54 if (!isspace(m_buf[i])) {
55 is_comment = (m_buf[i] == *comment_char);
56 break;
57 }
58 }
59
60 if (!is_comment) {
61 // Supprime le '\n' ou '\r' final
62 for (int i = 0; i < IOS_BFR_SZE && m_buf[i] != '\0'; ++i) {
63 //cout << " V=" << m_buf[i] << " I=" << (int)m_buf[i] << "\n";
64 if (m_buf[i] == '\n' || m_buf[i] == '\r') {
65 m_buf[i] = '\0';
66 break;
67 }
68 }
69 return m_buf;
70 }
71 }
72 throw IOException("IosFile::getNexLine()", "Unexpected EndOfFile");
73}
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78const char* IosFile::
79getNextLine()
80{
81 return getNextLine(nullptr);
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
91{
92 while (m_stream->good()) {
93 char c = m_stream->peek();
94 if (std::isspace(c))
95 m_stream->get();
96 else
97 break;
98 }
99}
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
104Real IosFile::
105getReal()
106{
107 Real v = 0.;
108 (*m_stream) >> ws >> v;
109 if (m_stream->good())
110 return v;
111 throw IOException("IosFile::getReal()", "Bad Real");
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117Integer IosFile::
118getInteger()
119{
120 Integer v = 0;
121 (*m_stream) >> ws >> v;
122 if (m_stream->good())
123 return v;
124 throw IOException("IosFile::getInteger()", "Bad Integer");
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130Int64 IosFile::
131getInt64()
132{
133 Int64 v = 0;
134 (*m_stream) >> ws >> v;
135 if (m_stream->good())
136 return v;
137 throw IOException("IosFile::getInteger()", "Bad Int64");
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143bool IosFile::
144lookForString(const String& str)
145{
146 const char* bfr = getNextLine();
147 std::cout << "[IosFile::getString] Looking for '" << str << "' len=" << str.length() << "\n";
148 std::istringstream iline(bfr);
149 std::string got;
150 iline >> got;
151 std::cout << "[IosFile::getString] got='" << got << "' len=" << got.length() << "\n";
152 return isEqualString(got, str);
153}
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158void IosFile::
159checkString(const String& current_value, const String& expected_value)
160{
161 String current_value_low = current_value.lower();
162 String expected_value_low = expected_value.lower();
163
164 if (current_value_low != expected_value_low) {
165 String s = "Expecting chain '" + expected_value + "', found '" + current_value + "'";
166 throw IOException("IosFile::checkString()", s);
167 }
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173void IosFile::
174checkString(const String& current_value, const String& expected_value1, const String& expected_value2)
175{
176 String current_value_low = current_value.lower();
177 String expected_value1_low = expected_value1.lower();
178 String expected_value2_low = expected_value2.lower();
179
180 if (current_value_low != expected_value1_low && current_value_low != expected_value2_low) {
181 String s = "Expecting chain '" + expected_value1 + "' or '" + expected_value2 + "', found '" + current_value + "'";
182 throw IOException("IosFile::checkString()", s);
183 }
184}
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189bool IosFile::
190isEqualString(const String& current_value, const String& expected_value)
191{
192 String current_value_low = current_value.lower();
193 String expected_value_low = expected_value.lower();
194 return (current_value_low == expected_value_low);
195}
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200void IosFile::
201readBytes(SmallSpan<std::byte> bytes)
202{
203 m_stream->read(reinterpret_cast<char*>(bytes.data()),bytes.size());
204 if (!m_stream->good())
205 throw IOException("IosFile::readBytes()",
206 String::format("Can not read '{0}' bytes",bytes.size()));
207}
208
209/*---------------------------------------------------------------------------*/
210/*---------------------------------------------------------------------------*/
211
212void IosFile::
213binaryRead(SmallSpan<Int32> values)
214{
215 readBytes(asWritableBytes(values));
216}
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221void IosFile::
222binaryRead(SmallSpan<Int64> values)
223{
224 readBytes(asWritableBytes(values));
225}
226
227/*---------------------------------------------------------------------------*/
228/*---------------------------------------------------------------------------*/
229
230void IosFile::
231binaryRead(SmallSpan<double> values)
232{
233 readBytes(asWritableBytes(values));
234}
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239void IosFile::
240binaryRead(SmallSpan<Real3> values)
241{
242 readBytes(asWritableBytes(values));
243}
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248void IosFile::
249binaryRead(SmallSpan<Byte> values)
250{
251 readBytes(asWritableBytes(values));
252}
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257} // End namespace Arcane
258
259/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
Exception lorsqu'une erreur d'entrée/sortie est détectée.
Definition IOException.h:32
void goToEndOfLine(void)
Lit tous les caractères jusqu'à un caractère non blanc.
Definition IosFile.cc:90
Vue d'un tableau d'éléments de type T.
Definition Span.h:673
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.
Definition Span.h:919
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
double Real
Type représentant un réel.