Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TextWriter2.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/* TextWriter2.cc (C) 2000-2024 */
9/* */
10/* Ecrivain de types simples. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/TextWriter2.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/core/ArcaneException.h"
24
25#include <fstream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::impl
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
37{
38 public:
39
40 String m_filename;
41 std::ofstream m_ostream;
42 Ref<IDataCompressor> m_data_compressor;
43};
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48TextWriter2::
49TextWriter2(const String& filename)
50: m_p(new Impl())
51{
52 _open(filename);
53}
54
55/*---------------------------------------------------------------------------*/
56/*---------------------------------------------------------------------------*/
57
58TextWriter2::
59~TextWriter2()
60{
61 delete m_p;
62}
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67void TextWriter2::
68_open(const String& filename)
69{
70 m_p->m_filename = filename;
71 std::ios::openmode mode = std::ios::out | std::ios::binary;
72 m_p->m_ostream.open(filename.localstr(), mode);
73 if (!m_p->m_ostream)
74 ARCANE_THROW(ReaderWriterException, "Can not open file '{0}' for writing", filename);
75 m_p->m_ostream.precision(FloatInfo<Real>::maxDigit() + 2);
76}
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81void TextWriter2::
82write(Span<const std::byte> values)
83{
84 _binaryWrite(values);
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90String TextWriter2::
91fileName() const
92{
93 return m_p->m_filename;
94}
95
96void TextWriter2::
97setDataCompressor(Ref<IDataCompressor> dc)
98{
99 m_p->m_data_compressor = dc;
100}
101
102Ref<IDataCompressor> TextWriter2::
103dataCompressor() const
104{
105 return m_p->m_data_compressor;
106}
107
108Int64 TextWriter2::
109fileOffset()
110{
111 return m_p->m_ostream.tellp();
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117void TextWriter2::
118_binaryWrite(Span<const std::byte> values)
119{
120 std::ostream& o = m_p->m_ostream;
121 //cout << "** BINARY WRITE len=" << len << " deflater=" << m_data_compressor << '\n';
122 IDataCompressor* d = m_p->m_data_compressor.get();
123 if (d && values.size() > d->minCompressSize()) {
124 UniqueArray<std::byte> compressed_values;
125 m_p->m_data_compressor->compress(values, compressed_values);
126 Int64 compressed_size = compressed_values.largeSize();
127 binaryWrite(o, asBytes(SmallSpan<Int64>(&compressed_size, 1)));
128 binaryWrite(o, compressed_values);
129 //cout << "** BINARY WRITE len=" << len << " compressed_len=" << compressed_size << '\n';
130 }
131 else
132 binaryWrite(o, values);
133}
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138std::ostream& TextWriter2::
139stream()
140{
141 return m_p->m_ostream;
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147} // namespace Arcane::impl
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
Informations sur le type flottant.
Definition Limits.h:48
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< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets non modifiables.
Definition Span.h:881
void binaryWrite(std::ostream &ostr, const Span< const std::byte > &bytes)
Ecrit en binaire le contenu de bytes sur le flot ostr.
Definition ArrayView.cc:60