14#include "arcane/std/SimpleCsvReaderWriter.h"
16#include "arccore/base/Directory.h"
17#include "arcane/utils/Iostream.h"
33 std::ofstream ofile((dst.
file(file_name) +
"." +
fileType()).localstr());
51 if (!_openFile(stream, src, file_name +
"." +
fileType())) {
59 if (!std::getline(stream, line)) {
70 ligne.
split(tmp, m_separator);
72 if (tmp.
size() == 0) {
77 m_simple_table_internal->m_table_name = tmp[0];
83 if (!std::getline(stream, line)) {
90 m_simple_table_internal->m_values.resize(1, m_simple_table_internal->m_column_names.size());
97 m_simple_table_internal->m_values.resize(compt_line + 1);
102 ligne.
split(splitted_line, m_separator);
105 if (splitted_line.
size() == 0) {
111 if (splitted_line.
size() != m_simple_table_internal->m_column_names.size() + 1) {
117 m_simple_table_internal->m_row_names.add(splitted_line[0]);
120 for (
Integer i = 1; i < splitted_line.
size(); i++) {
121 std::string std_string = splitted_line[i].localstr();
122 std::size_t pos_comma = std_string.find(
',');
124 if (pos_comma != std::string::npos) {
125 std_string[pos_comma] =
'.';
128 m_simple_table_internal->m_values[compt_line][i - 1] = std::stod(std_string);
132 }
while (std::getline(stream, line));
138 m_simple_table_internal->m_row_sizes.resize(m_simple_table_internal->m_row_names.size());
139 m_simple_table_internal->m_row_sizes.fill(m_simple_table_internal->m_values.dim2Size());
141 m_simple_table_internal->m_column_sizes.resize(m_simple_table_internal->m_column_names.size());
142 m_simple_table_internal->m_column_sizes.fill(m_simple_table_internal->m_values.dim1Size());
150 m_simple_table_internal->clear();
165 return m_precision_print;
172 m_precision_print = 1;
173 else if (
precision > (std::numeric_limits<Real>::max_digits10))
174 m_precision_print = (std::numeric_limits<Real>::max_digits10);
182 return m_is_fixed_print;
188 m_is_fixed_print = fixed;
194 return m_scientific_notation;
200 m_scientific_notation = use_scientific;
209 return m_simple_table_internal;
215 if (simple_table_internal.
isNull())
216 ARCANE_FATAL(
"The reference passed as a parameter is Null.");
217 m_simple_table_internal = simple_table_internal;
223bool SimpleCsvReaderWriter::
224_openFile(std::ifstream& stream,
Directory directory,
const String& file)
226 stream.open(directory.
file(file).
localstr(), std::ifstream::in);
227 return stream.good();
230void SimpleCsvReaderWriter::
231_closeFile(std::ifstream& stream)
236void SimpleCsvReaderWriter::
237_print(std::ostream& stream)
240 std::ios_base::fmtflags save_flags = stream.flags();
241 std::streamsize save_prec = stream.precision();
243 if (m_is_fixed_print) {
244 stream << std::setiosflags(std::ios::fixed);
246 if (m_scientific_notation) {
247 stream << std::scientific;
249 stream << std::setprecision(m_precision_print);
251 stream << m_simple_table_internal->m_table_name << m_separator;
253 for (
Integer j = 0; j < m_simple_table_internal->m_column_names.size(); j++) {
254 stream << m_simple_table_internal->m_column_names[j] << m_separator;
258 for (
Integer i = 0; i < m_simple_table_internal->m_values.dim1Size(); i++) {
259 stream << m_simple_table_internal->m_row_names[i] << m_separator;
260 ConstArrayView<Real> view = m_simple_table_internal->m_values[i];
261 for (
Integer j = 0; j < m_simple_table_internal->m_values.dim2Size(); j++) {
262 stream << view[j] << m_separator;
267 stream.flags(save_flags);
268 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.