Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TextReader2.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/* TextReader2.cc (C) 2000-2024 */
9/* */
10/* Lecteur simple. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/TextReader2.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#include "arcane/utils/FixedArray.h"
23
24#include "arcane/core/ArcaneException.h"
25
26#include <fstream>
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane::impl
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
38{
39 public:
40
41 Impl(const String& filename)
42 : m_filename(filename)
43 {}
44
45 public:
46
47 String m_filename;
48 std::ifstream m_istream;
49 Integer m_current_line = 0;
50 Int64 m_file_length = 0;
51 Ref<IDataCompressor> m_data_compressor;
52};
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57TextReader2::
58TextReader2(const String& filename)
59: m_p(new Impl(filename))
60{
61 std::ios::openmode mode = std::ios::in | std::ios::binary;
62 m_p->m_istream.open(filename.localstr(), mode);
63 if (!m_p->m_istream)
64 ARCANE_THROW(ReaderWriterException, "Can not read file '{0}' for reading", filename);
65 m_p->m_file_length = platform::getFileLength(filename);
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71TextReader2::
72~TextReader2()
73{
74 delete m_p;
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
80void TextReader2::
81readIntegers(Span<Integer> values)
82{
83 read(asWritableBytes(values));
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void TextReader2::
90_checkStream(const char* type, Int64 nb_read_value)
91{
92 std::istream& s = m_p->m_istream;
93 if (!s)
94 ARCANE_THROW(IOException, "Can not read '{0}' (nb_val={1} bad?={2} "
95 "fail?={3} eof?={4} pos={5}) file='{6}'",
96 type, nb_read_value, s.bad(), s.fail(),
97 s.eof(), s.tellg(), m_p->m_filename);
98}
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103void TextReader2::
104read(Span<std::byte> values)
105{
106 Int64 nb_value = values.size();
107 _binaryRead(values);
108 _checkStream("byte[]", nb_value);
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114void TextReader2::
115_binaryRead(Span<std::byte> values)
116{
117 std::istream& s = m_p->m_istream;
118 IDataCompressor* d = m_p->m_data_compressor.get();
119 if (d && values.size() > d->minCompressSize()) {
120 UniqueArray<std::byte> compressed_values;
121 FixedArray<Int64, 1> compressed_size;
122 binaryRead(s, asWritableBytes(compressed_size.span()));
123 compressed_values.resize(compressed_size[0]);
124 binaryRead(s, asWritableBytes(compressed_values));
125 m_p->m_data_compressor->decompress(compressed_values, values);
126 }
127 else {
128 binaryRead(s, values);
129 }
130}
131
132/*---------------------------------------------------------------------------*/
133/*---------------------------------------------------------------------------*/
134
135String TextReader2::
136fileName() const
137{
138 return m_p->m_filename;
139}
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144void TextReader2::
145setFileOffset(Int64 v)
146{
147 m_p->m_istream.seekg(v, std::ios::beg);
148}
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153void TextReader2::
154setDataCompressor(Ref<IDataCompressor> dc)
155{
156 m_p->m_data_compressor = dc;
157}
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162Ref<IDataCompressor> TextReader2::
163dataCompressor() const
164{
165 return m_p->m_data_compressor;
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171std::istream& TextReader2::
172stream()
173{
174 return m_p->m_istream;
175}
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180Int64 TextReader2::
181fileLength() const
182{
183 return m_p->m_file_length;
184}
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189} // namespace Arcane::impl
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
#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
void binaryRead(std::istream &istr, const Span< std::byte > &bytes)
Lit en binaire le contenu de bytes depuis le flot istr.
Definition ArrayView.cc:70
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