Arcane  v3.15.0.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-2024 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-2024 */
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/*---------------------------------------------------------------------------*/
86
87Real IosFile::
88getReal()
89{
90 Real v = 0.;
91 (*m_stream) >> ws >> v;
92 if (m_stream->good())
93 return v;
94 throw IOException("IosFile::getReal()", "Bad Real");
95}
96
97/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
100Integer IosFile::
101getInteger()
102{
103 Integer v = 0;
104 (*m_stream) >> ws >> v;
105 if (m_stream->good())
106 return v;
107 throw IOException("IosFile::getInteger()", "Bad Integer");
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113Int64 IosFile::
114getInt64()
115{
116 Int64 v = 0;
117 (*m_stream) >> ws >> v;
118 if (m_stream->good())
119 return v;
120 throw IOException("IosFile::getInteger()", "Bad Int64");
121}
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126bool IosFile::
127lookForString(const String& str)
128{
129 const char* bfr = getNextLine();
130 // ITraceMng::info() << "[IosFile::getString] Looking for " << str;
131 std::istringstream iline(bfr);
132 std::string got;
133 iline >> got;
134 // info() << "[IosFile::getString] got=" << got;
135 return isEqualString(got, str);
136}
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
141void IosFile::
142checkString(const String& current_value, const String& expected_value)
143{
144 String current_value_low = current_value.lower();
145 String expected_value_low = expected_value.lower();
146
147 if (current_value_low != expected_value_low) {
148 String s = "Expecting chain '" + expected_value + "', found '" + current_value + "'";
149 throw IOException("IosFile::checkString()", s);
150 }
151}
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156void IosFile::
157checkString(const String& current_value, const String& expected_value1, const String& expected_value2)
158{
159 String current_value_low = current_value.lower();
160 String expected_value1_low = expected_value1.lower();
161 String expected_value2_low = expected_value2.lower();
162
163 if (current_value_low != expected_value1_low && current_value_low != expected_value2_low) {
164 String s = "Expecting chain '" + expected_value1 + "' or '" + expected_value2 + "', found '" + current_value + "'";
165 throw IOException("IosFile::checkString()", s);
166 }
167}
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172bool IosFile::
173isEqualString(const String& current_value, const String& expected_value)
174{
175 String current_value_low = current_value.lower();
176 String expected_value_low = expected_value.lower();
177 return (current_value_low == expected_value_low);
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183void IosFile::
184readBytes(SmallSpan<std::byte> bytes)
185{
186 m_stream->read(reinterpret_cast<char*>(bytes.data()),bytes.size());
187 if (!m_stream->good())
188 throw IOException("IosFile::readBytes()",
189 String::format("Can not read '{0}' bytes",bytes.size()));
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
195void IosFile::
196binaryRead(SmallSpan<Int32> values)
197{
198 readBytes(asWritableBytes(values));
199}
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204void IosFile::
205binaryRead(SmallSpan<Int64> values)
206{
207 readBytes(asWritableBytes(values));
208}
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213void IosFile::
214binaryRead(SmallSpan<double> values)
215{
216 readBytes(asWritableBytes(values));
217}
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
221
222void IosFile::
223binaryRead(SmallSpan<Real3> values)
224{
225 readBytes(asWritableBytes(values));
226}
227
228/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230
231void IosFile::
232binaryRead(SmallSpan<Byte> values)
233{
234 readBytes(asWritableBytes(values));
235}
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240} // End namespace Arcane
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Int32 Integer
Type représentant un entier.
detail::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.
Definition Span.h:916