Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TextReader.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/* TextReader.cc (C) 2000-2021 */
9/* */
10/* Simple reader. */
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/core/ArcaneException.h"
24
25#include <fstream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::impl
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
37{
38 public:
39
40 Impl(const String& filename)
41 : m_filename(filename)
42 {}
43
44 public:
45
46 String m_filename;
47 std::ifstream m_istream;
48 Integer m_current_line = 0;
49 Int64 m_file_length = 0;
50 Ref<IDataCompressor> m_data_compressor;
51};
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56TextReader::
57TextReader(const String& filename)
58: m_p(new Impl(filename))
59{
60 std::ios::openmode mode = std::ios::in | std::ios::binary;
61 m_p->m_istream.open(filename.localstr(), mode);
62 if (!m_p->m_istream)
63 ARCANE_THROW(ReaderWriterException, "Can not read file '{0}' for reading", filename);
64 m_p->m_file_length = platform::getFileLength(filename);
65}
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
70TextReader::
71~TextReader()
72{
73 delete m_p;
74}
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79void TextReader::
80readIntegers(Span<Integer> values)
81{
82 read(asWritableBytes(values));
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88void TextReader::
89_checkStream(const char* type, Int64 nb_read_value)
90{
91 std::istream& s = m_p->m_istream;
92 if (!s)
93 ARCANE_THROW(IOException, "Can not read '{0}' (nb_val={1} bad?={2} "
94 "fail?={3} eof?={4} pos={5}) file='{6}'",
95 type, nb_read_value, s.bad(), s.fail(),
96 s.eof(), s.tellg(), m_p->m_filename);
97}
98
99/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
101
102void TextReader::
103read(Span<std::byte> values)
104{
105 Int64 nb_value = values.size();
106 _binaryRead(values.data(), nb_value);
107 _checkStream("byte[]", nb_value);
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113void TextReader::
114read(Span<Byte> values)
115{
116 read(asWritableBytes(values));
117}
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
121
122void TextReader::
123read(Span<Int64> values)
124{
125 read(asWritableBytes(values));
126}
127
128/*---------------------------------------------------------------------------*/
129/*---------------------------------------------------------------------------*/
130
131void TextReader::
132read(Span<Int16> values)
133{
134 read(asWritableBytes(values));
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140void TextReader::
141read(Span<Int32> values)
142{
143 read(asWritableBytes(values));
144}
145
146/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
148
149void TextReader::
150read(Span<Real> values)
151{
152 read(asWritableBytes(values));
153}
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158void TextReader::
159_binaryRead(void* values, Int64 len)
160{
161 std::istream& s = m_p->m_istream;
162 IDataCompressor* d = m_p->m_data_compressor.get();
163 if (d && len > d->minCompressSize()) {
164 UniqueArray<std::byte> compressed_values;
165 Int64 compressed_size = 0;
166 s.read((char*)&compressed_size, sizeof(Int64));
167 compressed_values.resize(compressed_size);
168 s.read((char*)compressed_values.data(), compressed_size);
169 m_p->m_data_compressor->decompress(compressed_values, Span<std::byte>((std::byte*)values, len));
170 }
171 else {
172 s.read((char*)values, len);
173 }
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179String TextReader::
180fileName() const
181{
182 return m_p->m_filename;
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188void TextReader::
189setFileOffset(Int64 v)
190{
191 m_p->m_istream.seekg(v, std::ios::beg);
192}
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197void TextReader::
198setDataCompressor(Ref<IDataCompressor> dc)
199{
200 m_p->m_data_compressor = dc;
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206Ref<IDataCompressor> TextReader::
207dataCompressor() const
208{
209 return m_p->m_data_compressor;
210}
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
214
215std::ifstream& TextReader::
216stream()
217{
218 return m_p->m_istream;
219}
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
224Int64 TextReader::
225fileLength() const
226{
227 return m_p->m_file_length;
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233} // End namespace Arcane::impl
234
235/*---------------------------------------------------------------------------*/
236/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
Exception in a reader or writer.
Reference to an instance.
View of an array of elements of type T.
Definition Span.h:635
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:229
long unsigned int getFileLength(const String &filename)
Length of the file filename. If the file is not readable or does not exist, returns 0.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
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