14#include "arcane/std/SimpleCsvReaderWriter.h"
16#include "arcane/utils/Iostream.h"
18#include "arcane/core/Directory.h"
32 std::ofstream ofile((dst.
file(file_name) +
"." +
fileType()).localstr());
50 if (!_openFile(stream, src, file_name +
"." +
fileType())) {
58 if (!std::getline(stream, line)) {
69 ligne.
split(tmp, m_separator);
71 if (tmp.
size() == 0) {
76 m_simple_table_internal->m_table_name = tmp[0];
82 if (!std::getline(stream, line)) {
89 m_simple_table_internal->m_values.resize(1, m_simple_table_internal->m_column_names.size());
96 m_simple_table_internal->m_values.resize(compt_line + 1);
101 ligne.
split(splitted_line, m_separator);
104 if (splitted_line.
size() == 0) {
110 if (splitted_line.
size() != m_simple_table_internal->m_column_names.size() + 1) {
116 m_simple_table_internal->m_row_names.add(splitted_line[0]);
119 for (
Integer i = 1; i < splitted_line.
size(); i++) {
120 std::string std_string = splitted_line[i].localstr();
121 std::size_t pos_comma = std_string.find(
',');
123 if (pos_comma != std::string::npos) {
124 std_string[pos_comma] =
'.';
127 m_simple_table_internal->m_values[compt_line][i - 1] = std::stod(std_string);
131 }
while (std::getline(stream, line));
137 m_simple_table_internal->m_row_sizes.resize(m_simple_table_internal->m_row_names.size());
138 m_simple_table_internal->m_row_sizes.fill(m_simple_table_internal->m_values.dim2Size());
140 m_simple_table_internal->m_column_sizes.resize(m_simple_table_internal->m_column_names.size());
141 m_simple_table_internal->m_column_sizes.fill(m_simple_table_internal->m_values.dim1Size());
149 m_simple_table_internal->clear();
164 return m_precision_print;
171 m_precision_print = 1;
172 else if (
precision > (std::numeric_limits<Real>::max_digits10))
173 m_precision_print = (std::numeric_limits<Real>::max_digits10);
181 return m_is_fixed_print;
187 m_is_fixed_print = fixed;
193 return m_scientific_notation;
199 m_scientific_notation = use_scientific;
208 return m_simple_table_internal;
214 if (simple_table_internal.
isNull())
215 ARCANE_FATAL(
"The reference passed as a parameter is Null.");
216 m_simple_table_internal = simple_table_internal;
222bool SimpleCsvReaderWriter::
223_openFile(std::ifstream& stream,
Directory directory,
const String& file)
225 stream.open(directory.
file(file).
localstr(), std::ifstream::in);
226 return stream.good();
229void SimpleCsvReaderWriter::
230_closeFile(std::ifstream& stream)
235void SimpleCsvReaderWriter::
236_print(std::ostream& stream)
239 std::ios_base::fmtflags save_flags = stream.flags();
240 std::streamsize save_prec = stream.precision();
242 if (m_is_fixed_print) {
243 stream << std::setiosflags(std::ios::fixed);
245 if (m_scientific_notation) {
246 stream << std::scientific;
248 stream << std::setprecision(m_precision_print);
250 stream << m_simple_table_internal->m_table_name << m_separator;
252 for (
Integer j = 0; j < m_simple_table_internal->m_column_names.size(); j++) {
253 stream << m_simple_table_internal->m_column_names[j] << m_separator;
257 for (
Integer i = 0; i < m_simple_table_internal->m_values.dim1Size(); i++) {
258 stream << m_simple_table_internal->m_row_names[i] << m_separator;
259 ConstArrayView<Real> view = m_simple_table_internal->m_values[i];
260 for (
Integer j = 0; j < m_simple_table_internal->m_values.dim2Size(); j++) {
261 stream << view[j] << m_separator;
266 stream.flags(save_flags);
267 stream.precision(save_prec);
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer size() const
Number of elements in the vector.
ConstArrayView< T > subConstView(Int64 abegin, Int32 asize) const
Sub-view starting from element abegin and containing asize elements.
Class managing a directory.
String file(const String &file_name) const override
Returns the full path of the file file_name in the directory.
bool isNull() const
Indicates if the counter references a non-null instance.
Reference to an instance.
void setFixed(bool fixed) override
Method allowing setting or unsetting the 'std::fixed' flag.
void setInternal(const Ref< SimpleTableInternal > &simple_table_internal) override
Method allowing setting a reference to a SimpleTableInternal.
bool isForcedToUseScientificNotation() override
Method allowing checking if the 'std::scientific' flag is active or not for writing values.
void clearInternal() override
Method allowing clearing the content of the SimpleTableInternal object.
void setForcedToUseScientificNotation(bool use_scientific) override
Method allowing setting or unsetting the 'std::scientific' flag.
bool readTable(const Directory &src, const String &file_name) override
Method allowing reading a file containing a simple table.
String fileType() override
Method allowing retrieval of the file type that will be written by the implementation....
void print() override
Method allowing writing the table to the standard output.
bool isFixed() override
Method allowing checking if the 'std::fixed' flag is active or not for writing values.
Ref< SimpleTableInternal > internal() override
Method allowing retrieval of a reference to the SimpleTableInternal object used.
bool writeTable(const Directory &dst, const String &file_name) override
Method allowing writing a simple table to a file.
void setPrecision(Integer precision) override
Method allowing modification of the print precision.
Integer precision() override
Method allowing retrieval of the precision currently used for writing values.
Unicode character string.
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
void split(StringContainer &str_array, char c) const
Splits the string based on the character c.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
UniqueArray< String > StringUniqueArray
Dynamic 1D array of strings.