Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
IosFile.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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/* File Read/Write Routines. */
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 // Check if a comment character is present
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 // Remove the final '\n' or '\r'
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/*---------------------------------------------------------------------------*/
86
92{
93 while (m_stream->good()) {
94 int c = m_stream->peek();
95#ifdef _MSC_VER
96#if _MSC_VER < 1930
97 if (std::isspace(c, std::locale::classic()))
98#else
99 if (std::isspace(c))
100#endif
101#else
102 if (std::isspace(c))
103#endif
104 m_stream->get();
105 else
106 break;
107 }
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113Real IosFile::
114getReal()
115{
116 Real v = 0.;
117 (*m_stream) >> ws >> v;
118 if (m_stream->good())
119 return v;
120 throw IOException("IosFile::getReal()", "Bad Real");
121}
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126Integer IosFile::
127getInteger()
128{
129 Integer v = 0;
130 (*m_stream) >> ws >> v;
131 if (m_stream->good())
132 return v;
133 throw IOException("IosFile::getInteger()", "Bad Integer");
134}
135
136/*---------------------------------------------------------------------------*/
137/*---------------------------------------------------------------------------*/
138
139Int64 IosFile::
140getInt64()
141{
142 Int64 v = 0;
143 (*m_stream) >> ws >> v;
144 if (m_stream->good())
145 return v;
146 throw IOException("IosFile::getInteger()", "Bad Int64");
147}
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152bool IosFile::
153lookForString(const String& str)
154{
155 const char* bfr = getNextLine();
156 //std::cout << "[IosFile::getString] Looking for '" << str << "' len=" << str.length() << "\n";
157 std::istringstream iline(bfr);
158 std::string got;
159 iline >> got;
160 //std::cout << "[IosFile::getString] got='" << got << "' len=" << got.length() << "\n";
161 return isEqualString(got, str);
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167void IosFile::
168checkString(const String& current_value, const String& expected_value)
169{
170 String current_value_low = current_value.lower();
171 String expected_value_low = expected_value.lower();
172
173 if (current_value_low != expected_value_low) {
174 String s = "Expecting chain '" + expected_value + "', found '" + current_value + "'";
175 throw IOException("IosFile::checkString()", s);
176 }
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182void IosFile::
183checkString(const String& current_value, const String& expected_value1, const String& expected_value2)
184{
185 String current_value_low = current_value.lower();
186 String expected_value1_low = expected_value1.lower();
187 String expected_value2_low = expected_value2.lower();
188
189 if (current_value_low != expected_value1_low && current_value_low != expected_value2_low) {
190 String s = "Expecting chain '" + expected_value1 + "' or '" + expected_value2 + "', found '" + current_value + "'";
191 throw IOException("IosFile::checkString()", s);
192 }
193}
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
197
198bool IosFile::
199isEqualString(const String& current_value, const String& expected_value)
200{
201 String current_value_low = current_value.lower();
202 String expected_value_low = expected_value.lower();
203 return (current_value_low == expected_value_low);
204}
205
206/*---------------------------------------------------------------------------*/
207/*---------------------------------------------------------------------------*/
208
209void IosFile::
210readBytes(SmallSpan<std::byte> bytes)
211{
212 m_stream->read(reinterpret_cast<char*>(bytes.data()), bytes.size());
213 if (!m_stream->good())
214 throw IOException("IosFile::readBytes()",
215 String::format("Can not read '{0}' bytes", bytes.size()));
216}
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221void IosFile::
222binaryRead(SmallSpan<Int32> values)
223{
224 readBytes(asWritableBytes(values));
225}
226
227/*---------------------------------------------------------------------------*/
228/*---------------------------------------------------------------------------*/
229
230void IosFile::
231binaryRead(SmallSpan<Int64> values)
232{
233 readBytes(asWritableBytes(values));
234}
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239void IosFile::
240binaryRead(SmallSpan<double> values)
241{
242 readBytes(asWritableBytes(values));
243}
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248void IosFile::
249binaryRead(SmallSpan<Real3> values)
250{
251 readBytes(asWritableBytes(values));
252}
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257void IosFile::
258binaryRead(SmallSpan<Byte> values)
259{
260 readBytes(asWritableBytes(values));
261}
262
263/*---------------------------------------------------------------------------*/
264/*---------------------------------------------------------------------------*/
265
266} // End namespace Arcane
267
268/*---------------------------------------------------------------------------*/
269/*---------------------------------------------------------------------------*/
Exception when an input/output error is detected.
Definition IOException.h:34
void goToEndOfLine(void)
Reads all characters until a non-whitespace character.
Definition IosFile.cc:91
View of an array of elements of type T.
Definition Span.h:805
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of modifiable bytes.
Definition Span.h:1068