Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TextWriter.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/* TextWriter.cc (C) 2000-2021 */
9/* */
10/* Ecrivain de types simples. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/TextWriter.h"
15
16#include "arcane/utils/ArrayView.h"
17#include "arcane/utils/CheckedConvert.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/FatalErrorException.h"
20#include "arcane/utils/Ref.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 String m_filename;
40 std::ofstream m_ostream;
41 Ref<IDataCompressor> m_data_compressor;
42};
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
46
47TextWriter::
48TextWriter(const String& filename)
49: m_p(new Impl())
50{
51 open(filename);
52}
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57TextWriter::
58TextWriter()
59: m_p(new Impl())
60{
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66TextWriter::
67~TextWriter()
68{
69 delete m_p;
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75void TextWriter::
76open(const String& filename)
77{
78 m_p->m_filename = filename;
79 std::ios::openmode mode = std::ios::out | std::ios::binary;
80 m_p->m_ostream.open(filename.localstr(),mode);
81 if (!m_p->m_ostream)
82 ARCANE_THROW(ReaderWriterException,"Can not open file '{0}' for writing", filename);
83 m_p->m_ostream.precision(FloatInfo<Real>::maxDigit() + 2);
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void TextWriter::
90write(Span<const Real> values)
91{
92 _binaryWrite(values.data(), values.size() * sizeof(Real));
93}
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98void TextWriter::
99write(Span<const Int16> values)
100{
101 _binaryWrite(values.data(), values.size() * sizeof(Int16));
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107void TextWriter::
108write(Span<const Int32> values)
109{
110 _binaryWrite(values.data(), values.size() * sizeof(Int32));
111}
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116void TextWriter::
117write(Span<const Int64> values)
118{
119 _binaryWrite(values.data(), values.size() * sizeof(Int64));
120}
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
125void TextWriter::
126write(Span<const Byte> values)
127{
128 _binaryWrite(values.data(), values.size());
129}
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
134void TextWriter::
135write(Span<const std::byte> values)
136{
137 _binaryWrite(values.data(), values.size());
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143String TextWriter::
144fileName() const
145{
146 return m_p->m_filename;
147}
148
149void TextWriter::
150setDataCompressor(Ref<IDataCompressor> dc)
151{
152 m_p->m_data_compressor = dc;
153}
154
155Ref<IDataCompressor> TextWriter::
156dataCompressor() const
157{
158 return m_p->m_data_compressor;
159}
160
161Int64 TextWriter::
162fileOffset()
163{
164 return m_p->m_ostream.tellp();
165}
166
167void TextWriter::
168_binaryWrite(const void* bytes,Int64 len)
169{
170 std::ostream& o = m_p->m_ostream;
171 //cout << "** BINARY WRITE len=" << len << " deflater=" << m_data_compressor << '\n';
172 IDataCompressor* d = m_p->m_data_compressor.get();
173 if (d && len > d->minCompressSize()) {
174 UniqueArray<std::byte> compressed_values;
175 m_p->m_data_compressor->compress(Span<const std::byte>((const std::byte*)bytes,len), compressed_values);
176 Int64 compressed_size = compressed_values.largeSize();
177 o.write((const char *) &compressed_size, sizeof(Int64));
178 o.write((const char *) compressed_values.data(), compressed_size);
179 //cout << "** BINARY WRITE len=" << len << " compressed_len=" << compressed_size << '\n';
180 }
181 else
182 o.write((const char *) bytes, len);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188std::ostream& TextWriter::
189stream()
190{
191 return m_p->m_ostream;
192}
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200} // End namespace Arcane::impl
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
#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
Chaîne de caractères unicode.