Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TextReader.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* TextReader.cc (C) 2000-2021 */
9/* */
10/* Lecteur simple. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/TextReader.h"
15
16#include "arcane/utils/IOException.h"
17#include "arcane/utils/Array.h"
18#include "arcane/utils/PlatformUtils.h"
19#include "arcane/utils/Ref.h"
20#include "arcane/utils/FatalErrorException.h"
21#include "arcane/utils/IDataCompressor.h"
22
23#include "arcane/ArcaneException.h"
24
25#include <fstream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::impl
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
37{
38 public:
39 Impl(const String& filename)
40 : m_filename(filename) {}
41 public:
42 String m_filename;
43 std::ifstream m_istream;
44 Integer m_current_line = 0;
45 Int64 m_file_length = 0;
46 Ref<IDataCompressor> m_data_compressor;
47};
48
49/*---------------------------------------------------------------------------*/
50/*---------------------------------------------------------------------------*/
51
52TextReader::
53TextReader(const String& filename)
54: m_p(new Impl(filename))
55{
56 std::ios::openmode mode = std::ios::in | std::ios::binary;
57 m_p->m_istream.open(filename.localstr(),mode);
58 if (!m_p->m_istream)
59 ARCANE_THROW(ReaderWriterException, "Can not read file '{0}' for reading", filename);
60 m_p->m_file_length = platform::getFileLength(filename);
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66TextReader::
67~TextReader()
68{
69 delete m_p;
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75void TextReader::
76readIntegers(Span<Integer> values)
77{
78 read(asWritableBytes(values));
79}
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84void TextReader::
85_checkStream(const char* type, Int64 nb_read_value)
86{
87 std::istream& s = m_p->m_istream;
88 if (!s)
89 ARCANE_THROW(IOException, "Can not read '{0}' (nb_val={1} bad?={2} "
90 "fail?={3} eof?={4} pos={5}) file='{6}'",
91 type, nb_read_value, s.bad(), s.fail(),
92 s.eof(), s.tellg(), m_p->m_filename);
93}
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98void TextReader::
99read(Span<std::byte> values)
100{
101 Int64 nb_value = values.size();
102 _binaryRead(values.data(), nb_value);
103 _checkStream("byte[]", nb_value);
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109void TextReader::
110read(Span<Byte> values)
111{
112 read(asWritableBytes(values));
113}
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
117
118void TextReader::
119read(Span<Int64> values)
120{
121 read(asWritableBytes(values));
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127void TextReader::
128read(Span<Int16> values)
129{
130 read(asWritableBytes(values));
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136void TextReader::
137read(Span<Int32> values)
138{
139 read(asWritableBytes(values));
140}
141
142/*---------------------------------------------------------------------------*/
143/*---------------------------------------------------------------------------*/
144
145void TextReader::
146read(Span<Real> values)
147{
148 read(asWritableBytes(values));
149}
150
151/*---------------------------------------------------------------------------*/
152/*---------------------------------------------------------------------------*/
153
154void TextReader::
155_binaryRead(void* values, Int64 len)
156{
157 std::istream& s = m_p->m_istream;
158 IDataCompressor* d = m_p->m_data_compressor.get();
159 if (d && len > d->minCompressSize()) {
160 UniqueArray<std::byte> compressed_values;
161 Int64 compressed_size = 0;
162 s.read((char*)&compressed_size, sizeof(Int64));
163 compressed_values.resize(compressed_size);
164 s.read((char*)compressed_values.data(), compressed_size);
165 m_p->m_data_compressor->decompress(compressed_values, Span<std::byte>((std::byte*)values,len));
166 }
167 else {
168 s.read((char*)values, len);
169 }
170}
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175String TextReader::
176fileName() const
177{
178 return m_p->m_filename;
179}
180
181/*---------------------------------------------------------------------------*/
182/*---------------------------------------------------------------------------*/
183
184void TextReader::
185setFileOffset(Int64 v)
186{
187 m_p->m_istream.seekg(v,std::ios::beg);
188}
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
193void TextReader::
194setDataCompressor(Ref<IDataCompressor> dc)
195{
196 m_p->m_data_compressor = dc;
197}
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202Ref<IDataCompressor> TextReader::
203dataCompressor() const
204{
205 return m_p->m_data_compressor;
206}
207
208/*---------------------------------------------------------------------------*/
209/*---------------------------------------------------------------------------*/
210
211std::ifstream& TextReader::
212stream()
213{
214 return m_p->m_istream;
215}
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
220Int64 TextReader::
221fileLength() const
222{
223 return m_p->m_file_length;
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
231
232} // End namespace Arcane::impl
233
234/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Exception dans un lecteur ou écrivain.
Chaîne de caractères unicode.
const char * localstr() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
Definition String.cc:227
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