scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

AsciiTableImportFilter.cpp
Go to the documentation of this file.
1 
11 
31 #include "table/future_Table.h"
32 #include "lib/IntervalAttribute.h"
33 #include "core/column/Column.h"
35 
36 #include <QTextStream>
37 #include <QStringList>
38 #include <QLocale>
39 
40 #include <vector>
41 #include <iostream>
42 using namespace std;
43 
45 {
46  return QStringList() << "txt" << "csv" << "dat";
47 }
48 
49 namespace
50 {
51  // redirect to QIODevice's readLine so that we can override it to handle '\r' line terminators
53  {
54  QIODevice& input;
55  bool good;
56  operator bool() const {return good;}
57  enum {none, simplify, trim} whiteSpaceTreatment;
58  QString separator;
59  SciDaVisTextStream(QIODevice& inp, const QString& sep):
60  input(inp), good(true), whiteSpaceTreatment(none),
61  separator(sep) {}
62 
63  QStringList readRow()
64  {
65  char c;
66  QString r;
67 
68  while ((good=input.getChar(&c)))
69  switch (c)
70  {
71  case '\r':
72  if (input.getChar(&c) && c!='\n') // eat \n following \r
73  input.ungetChar(c);
74  goto breakLoop;
75  case '\n':
76  goto breakLoop;
77  default:
78  r+=c;
79  };
80  breakLoop:
81  switch (whiteSpaceTreatment)
82  {
83  case none:
84  return r.split(separator);
85  case simplify:
86  return r.simplified().split(separator);
87  case trim:
88  return r.trimmed().split(separator);
89  default:
90  return QStringList();
91  }
92  }
93  };
94 
95  template <class C> C conv(const QString& x);
96  template <> QString conv<QString>(const QString& x) {return x;}
97  template <> qreal conv<qreal>(const QString& x) {return (qreal)(QLocale().toDouble(x));}
98 
99  template <class T>
100  struct AP: public std::unique_ptr<T>
101  {
102  AP(): std::unique_ptr<T>(new T) {}
103  AP(const AP& x): std::unique_ptr<T>(new T(*x)) {}
104  };
105 
106  template <class C>
107  void readCols(QList<Column*>& cols, SciDaVisTextStream& stream,
108  bool readColNames)
109  {
110  QStringList row, column_names;
111  int i;
112 
113  // read first row
114  row = stream.readRow();
115 
116  int dataSize=row.size();
117  vector<AP<C> > data(dataSize);
118  vector<IntervalAttribute<bool> > invalid_cells(row.size());
119 
120  if (readColNames)
121  column_names = row;
122  else
123  for (i=0; i<row.size(); ++i)
124  {
125  column_names << QString::number(i+1);
126  *data[i] << conv<typename C::value_type>(row[i]);
127  }
128 
129  // read rest of data
130  while (stream)
131  {
132  row = stream.readRow();
133  if (stream || (row != QStringList(""))) {
134  for (i=0; i<row.size() && i<dataSize; ++i)
135  *data[i] << conv<typename C::value_type>(row[i]);
136  // some rows might have too few columns (re-use value of i from above loop)
137  for (; i<dataSize; ++i) {
138  invalid_cells[i].setValue(data[i]->size(), true);
139  *data[i] << conv<typename C::value_type>("");
140  }
141  }
142  }
143 
144  for (i=0; i<dataSize; ++i)
145  {
146  cols << new Column(std::move(column_names[i]), unique_ptr<C>(std::move(data[i])),
147  std::move(invalid_cells[i]));
148  if (i == 0)
149  cols.back()->setPlotDesignation(SciDAVis::X);
150  else
151  cols.back()->setPlotDesignation(SciDAVis::Y);
152  }
153 
154  }
155 
156 }
157 
159 {
160  SciDaVisTextStream stream(input, d_separator);
161  if (d_simplify_whitespace)
162  stream.whiteSpaceTreatment=SciDaVisTextStream::simplify;
163  else if (d_trim_whitespace)
164  stream.whiteSpaceTreatment=SciDaVisTextStream::trim;
165 
166  // skip ignored lines
167  for (int i=0; i<d_ignored_lines; i++)
168  stream.readRow();
169 
170  // build a Table from the gathered data
171  QList<Column*> cols;
172  if (d_convert_to_numeric)
173  readCols<QVector<qreal> >(cols, stream, d_first_row_names_columns);
174  else
175  readCols<QStringList>(cols, stream, d_first_row_names_columns);
176 
177  // renaming will be done by the kernel
178  future::Table * result = new future::Table(0, 0, tr("Table"));
179  result->appendColumns(cols);
180  return result;
181 }
182 
AsciiTableImportFilter::fileExtensions
virtual QStringList fileExtensions() const
The file extension(s) typically associated with the handled format.
Definition: AsciiTableImportFilter.cpp:44
Column.h
data
Definition: exp_saturation.c:37
SciDAVis::Y
@ Y
y values
Definition: globals.h:58
future::Table
Aspect providing a spreadsheet table with column logic.
Definition: future_Table.h:86
AsciiTableImportFilter::importAspect
virtual AbstractAspect * importAspect(QIODevice &input)
Import an object from the specified device and convert it to an Aspect.
Definition: AsciiTableImportFilter.cpp:158
anonymous_namespace{AsciiTableImportFilter.cpp}::conv
C conv(const QString &x)
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream
Definition: AsciiTableImportFilter.cpp:53
anonymous_namespace{AsciiTableImportFilter.cpp}::conv< QString >
QString conv< QString >(const QString &x)
Definition: AsciiTableImportFilter.cpp:96
future_Table.h
anonymous_namespace{AsciiTableImportFilter.cpp}::AP
Definition: AsciiTableImportFilter.cpp:101
AsciiTableImportFilter.h
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream::readRow
QStringList readRow()
Definition: AsciiTableImportFilter.cpp:63
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream::good
bool good
Definition: AsciiTableImportFilter.cpp:55
IntervalAttribute.h
anonymous_namespace{AsciiTableImportFilter.cpp}::readCols
void readCols(QList< Column * > &cols, SciDaVisTextStream &stream, bool readColNames)
Definition: AsciiTableImportFilter.cpp:107
anonymous_namespace{AsciiTableImportFilter.cpp}::AP::AP
AP()
Definition: AsciiTableImportFilter.cpp:102
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream::separator
QString separator
Definition: AsciiTableImportFilter.cpp:58
future::Table::appendColumns
void appendColumns(QList< Column * > new_cols)
Append columns.
Definition: future_Table.h:132
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream::SciDaVisTextStream
SciDaVisTextStream(QIODevice &inp, const QString &sep)
Definition: AsciiTableImportFilter.cpp:59
anonymous_namespace{AsciiTableImportFilter.cpp}::AP::AP
AP(const AP &x)
Definition: AsciiTableImportFilter.cpp:103
String2DoubleFilter.h
anonymous_namespace{AsciiTableImportFilter.cpp}::conv< qreal >
qreal conv< qreal >(const QString &x)
Definition: AsciiTableImportFilter.cpp:97
SciDAVis::X
@ X
x values
Definition: globals.h:57
anonymous_namespace{AsciiTableImportFilter.cpp}::SciDaVisTextStream::input
QIODevice & input
Definition: AsciiTableImportFilter.cpp:54
AbstractAspect
Base class of all persistent objects in a Project.
Definition: AbstractAspect.h:86