Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
IOMng.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/* IOMng.cc (C) 2000-2024 */
9/* */
10/* Input/Output Manager. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/utils/Iostream.h"
17#include "arcane/utils/Array.h"
18#include "arcane/utils/PlatformUtils.h"
19#include "arcane/utils/ITraceMng.h"
20#include "arcane/utils/CheckedConvert.h"
21
22#include "arcane/core/IApplication.h"
23#include "arcane/core/DomUtils.h"
24#include "arcane/core/XmlNode.h"
25#include "arcane/core/IIOMng.h"
26#include "arcane/core/IParallelSuperMng.h"
27#include "arcane/core/IParallelMng.h"
28#include "arcane/core/IXmlDocumentHolder.h"
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace Arcane
34{
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
42class IOMng
43: public IIOMng
44{
45 public:
46
47 IOMng(IParallelSuperMng* psm);
48 IOMng(IParallelMng* pm);
49 ~IOMng() override;
50
51 IXmlDocumentHolder* parseXmlFile(const String& filename, const String& schemaname = String()) override;
52 IXmlDocumentHolder* parseXmlFile(const String& filename, const String& schemaname, ByteConstArrayView schema_data) override;
53 IXmlDocumentHolder* parseXmlBuffer(Span<const Byte> buffer, const String& name) override;
55 IXmlDocumentHolder* parseXmlString(const String& str, const String& name) override;
56 bool writeXmlFile(IXmlDocumentHolder* doc, const String& filename, const bool indented) override;
57 bool collectiveRead(const String& filename, ByteArray& bytes) override
58 {
59 return collectiveRead(filename, bytes, true);
60 }
61 bool collectiveRead(const String& filename, ByteArray& bytes, bool is_binary) override;
62 bool localRead(const String& filename, ByteArray& bytes) override
63 {
64 return localRead(filename, bytes, true);
65 }
66 bool localRead(const String& filename, ByteArray& bytes, bool is_binary) override;
67
68 private:
69
70 IParallelMng* m_parallel_mng;
71 IParallelSuperMng* m_parallel_super_mng;
72 IThreadMng* m_thread_mng;
73 ITraceMng* m_trace_mng;
74
75 template <typename ParallelMngType> bool
76 _collectiveRead(ParallelMngType* pm, const String& filename, ByteArray& bytes, bool is_binary);
77};
78
79/*---------------------------------------------------------------------------*/
80/*---------------------------------------------------------------------------*/
81
82extern "C++" ARCANE_IMPL_EXPORT IIOMng*
83arcaneCreateIOMng(IParallelMng* pm)
84{
85 return new IOMng(pm);
86}
87
88extern "C++" ARCANE_IMPL_EXPORT IIOMng*
89arcaneCreateIOMng(IParallelSuperMng* psm)
90{
91 return new IOMng(psm);
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
97IOMng::
98IOMng(IParallelMng* pm)
99: m_parallel_mng(pm)
100, m_parallel_super_mng(nullptr)
101, m_thread_mng(pm->threadMng())
102, m_trace_mng(pm->traceMng())
103{
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109IOMng::
110IOMng(IParallelSuperMng* psm)
111: m_parallel_mng(nullptr)
112, m_parallel_super_mng(psm)
113, m_thread_mng(psm->threadMng())
114, m_trace_mng(psm->application()->traceMng())
115{
116}
117
118/*---------------------------------------------------------------------------*/
119/*---------------------------------------------------------------------------*/
120
121IOMng::
122~IOMng()
123{
124}
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
130writeXmlFile(IXmlDocumentHolder* doc, const String& filename, const bool to_indent)
131{
132 if (!doc)
133 return true;
134 std::ofstream ofile(filename.localstr());
135
136 // Check if stream is OK
137 if (!ofile.good())
138 return true;
139
140 Integer indented = to_indent ? 1 : -1;
141 domutils::saveDocument(ofile, doc->documentNode().domNode(), indented);
142 return false;
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
149parseXmlFile(const String& filename, const String& schemaname)
150{
151 return IXmlDocumentHolder::loadFromFile(filename, schemaname, m_trace_mng);
152}
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
158parseXmlFile(const String& filename,
159 const String& schemaname,
160 ByteConstArrayView schema_data)
161{
163 // Reading the file containing internal information.
164 return domimp._load(filename, m_trace_mng, schemaname, schema_data);
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
171parseXmlBuffer(Span<const Byte> buffer, const String& name)
172{
173 return IXmlDocumentHolder::loadFromBuffer(buffer, name, m_trace_mng);
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
181{
182 return IXmlDocumentHolder::loadFromBuffer(buffer, name, m_trace_mng);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
189parseXmlString(const String& str, const String& name)
190{
192 ByteConstArrayView utf8_buf(str.utf8());
193 ByteConstSpan buffer(reinterpret_cast<const std::byte*>(utf8_buf.data()), utf8_buf.size());
194 // Reading the file containing internal information.
195 return domimp._load(buffer, name, m_trace_mng);
196}
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
201template <typename ParallelMngType>
202bool IOMng::
203_collectiveRead(ParallelMngType* pm, const String& filename, ByteArray& bytes, bool is_binary)
204{
205 // The read requires two broadcasts: one for the file size and
206 // one for the file values.
207 //IParallelSuperMng* pm = m_application->parallelSuperMng();
208 Integer size = 0;
209 if (pm->commRank() == 0) {
210 if (!localRead(filename, bytes, is_binary)) {
211 // Prevents a bug if bytes was not cleared and there is a read problem
212 size = bytes.size();
213 }
214 }
215 {
216 IntegerArrayView bs(1, &size);
217 pm->broadcast(bs, 0);
218 //m_trace_mng->info() << "IO_SIZE=" << size;
219 }
220 bytes.resize(size);
221 if (size != 0)
222 pm->broadcast(bytes, 0);
223 return bytes.empty();
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
230collectiveRead(const String& filename, ByteArray& bytes, bool is_binary)
231{
232 if (m_parallel_mng)
233 return _collectiveRead(m_parallel_mng, filename, bytes, is_binary);
234 return _collectiveRead(m_parallel_super_mng, filename, bytes, is_binary);
235}
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
241localRead(const String& filename, ByteArray& bytes, bool is_binary)
242{
243 return platform::readAllFile(filename, is_binary, bytes);
244}
245
246/*---------------------------------------------------------------------------*/
247/*---------------------------------------------------------------------------*/
248
249} // namespace Arcane
250
251/*---------------------------------------------------------------------------*/
252/*---------------------------------------------------------------------------*/
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of the input/output manager.
Definition IIOMng.h:37
Input/Output Manager.
Definition IOMng.cc:44
IXmlDocumentHolder * parseXmlBuffer(Span< const Byte > buffer, const String &name) override
Reads and parses the XML file contained in the buffer buffer.
Definition IOMng.cc:171
bool localRead(const String &filename, ByteArray &bytes) override
Local reading of a file.
Definition IOMng.cc:62
IXmlDocumentHolder * parseXmlString(const String &str, const String &name) override
Reads and parses the XML file contained in the string str.
Definition IOMng.cc:189
bool collectiveRead(const String &filename, ByteArray &bytes) override
Collective reading of a file.
Definition IOMng.cc:57
bool writeXmlFile(IXmlDocumentHolder *doc, const String &filename, const bool indented) override
Writes the XML tree of the document doc to the file filename.
Definition IOMng.cc:130
IXmlDocumentHolder * parseXmlFile(const String &filename, const String &schemaname=String()) override
Reads and parses the XML file filename.
Definition IOMng.cc:149
Interface of the parallelism manager for a subdomain.
Abstract class of the parallelism supervisor.
Interface of a thread manager.
Definition IThreadMng.h:32
Manager of a DOM document.
static IXmlDocumentHolder * loadFromBuffer(Span< const Byte > buffer, const String &name, ITraceMng *tm)
Loads an XML document.
Definition DomUtils.cc:425
virtual XmlNode documentNode()=0
Document node. This node is null if the document does not exist.
static IXmlDocumentHolder * loadFromFile(const String &filename, ITraceMng *tm)
Loads an XML document.
Definition DomUtils.cc:444
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
ByteConstArrayView utf8() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:277
dom::Node domNode() const
Definition XmlNode.h:307
IXmlDocumentHolder * _load(const String &fname, ITraceMng *msg, const String &schemaname)
bool readAllFile(StringView filename, bool is_binary, ByteArray &out_bytes)
Reads the content of a file and stores it in out_bytes.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
Definition UtilsTypes.h:121
ArrayView< Integer > IntegerArrayView
C equivalent of a 1D array of integers.
Definition UtilsTypes.h:457
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:476