26 const bool KSParser::parser_debug_mode_ =
false;
29 const QList< QPair<QString, DataTypes> > &sequence,
31 : filename_(filename), comment_char_(comment_char),
32 name_type_sequence_(sequence), delimiter_(delimiter) {
34 kWarning() <<
"Unable to open file: "<< filename;
35 readFunctionPtr = &KSParser::DummyRow;
37 readFunctionPtr = &KSParser::ReadCSVRow;
38 kDebug() <<
"File opened: "<< filename;
43 const QList< QPair<QString, DataTypes> > &sequence,
45 : filename_(filename), comment_char_(comment_char),
46 name_type_sequence_(sequence), width_sequence_(widths) {
48 kWarning() <<
"Unable to open file: "<< filename;
49 readFunctionPtr = &KSParser::DummyRow;
51 readFunctionPtr = &KSParser::ReadFixedWidthRow;
52 kDebug() <<
"File opened: "<< filename;
57 return (this->*readFunctionPtr)();
60 QHash<QString, QVariant> KSParser::ReadCSVRow() {
66 bool read_success =
false;
68 QStringList separated;
69 QHash<QString, QVariant> newRow;
71 while (file_reader_.
hasMoreLines() && read_success ==
false) {
73 if (next_line.mid(0,1)[0] == comment_char_)
continue;
74 separated = next_line.split(delimiter_);
85 if (separated.length() == 1)
continue;
88 separated = CombineQuoteParts(separated);
94 if (separated.length() != name_type_sequence_.length())
97 for (
int i = 0; i < name_type_sequence_.length(); i++) {
99 newRow[name_type_sequence_[i].first] =
100 ConvertToQVariant(separated[i], name_type_sequence_[i].second, ok);
101 if (!ok && parser_debug_mode_) {
102 kDebug() << name_type_sequence_[i].second
103 <<
"Failed at field: "
104 << name_type_sequence_[i].first
105 <<
" & next_line : " << next_line;
116 if (file_reader_.
hasMoreLines() ==
false && newRow.size()<=1)
121 QHash<QString, QVariant> KSParser::ReadFixedWidthRow() {
122 if (name_type_sequence_.length() != (width_sequence_.length() + 1)) {
126 kWarning() <<
"Unequal fields and widths! Returning dummy row!";
136 bool read_success =
false;
138 QStringList separated;
139 QHash<QString, QVariant> newRow;
140 int total_min_length = 0;
142 foreach(
const int width_value, width_sequence_) {
143 total_min_length += width_value;
145 while (file_reader_.
hasMoreLines() && read_success ==
false) {
156 next_line = file_reader_.
readLine();
157 if (next_line.mid(0,1)[0] == comment_char_)
continue;
158 if (next_line.length() < total_min_length)
continue;
161 for (
int n_split = 0; n_split < width_sequence_.length(); n_split++) {
164 temp_split = next_line.mid(curr_width, width_sequence_[n_split]);
166 curr_width += width_sequence_[n_split];
167 separated.append(temp_split.trimmed());
169 separated.append(next_line.mid(curr_width).trimmed());
172 for (
int i = 0; i < name_type_sequence_.length(); ++i) {
174 newRow[name_type_sequence_[i].first] =
175 ConvertToQVariant(separated[i], name_type_sequence_[i].second, ok);
176 if (!ok && parser_debug_mode_) {
177 kDebug() << name_type_sequence_[i].second
178 <<
"Failed at field: "
179 << name_type_sequence_[i].first
180 <<
" & next_line : " << next_line;
191 if (file_reader_.
hasMoreLines() ==
false && newRow.size()<=1)
196 QHash<QString, QVariant> KSParser::DummyRow() {
197 kWarning() <<
"File named " << filename_ <<
" encountered an error while reading";
198 QHash<QString, QVariant> newRow;
199 for (
int i = 0; i < name_type_sequence_.length(); ++i) {
200 switch (name_type_sequence_[i].second) {
208 newRow[name_type_sequence_[i].first] =
EBROKEN_INT;
223 file_reader_.
setProgress(i18n(
"Loading NGC/IC objects"),
224 total_lines, step_size);
234 QStringList::const_iterator iter;
236 if (separated.length() == 0) {
237 kDebug() <<
"Cannot Combine empty list";
251 iter = separated.constBegin();
253 while (iter != separated.constEnd()) {
257 if (iter_string.indexOf(
"\"") == 0) {
258 iter_string = (iter_string).
remove(0,1);
259 while (iter_string.lastIndexOf(
'\"') != (iter_string.length()-1) &&
260 iter != separated.constEnd()) {
261 queue.append((iter_string));
266 queue.append(iter_string);
268 queue.append(iter_string);
272 foreach(
const QString &join, queue)
273 col_result += (join + delimiter_);
275 quoteCombined.append(col_result);
279 return quoteCombined;
282 QVariant KSParser::ConvertToQVariant(
const QString &input_string,
286 QVariant converted_object;
290 converted_object = input_string;
293 converted_object = input_string.trimmed().toDouble(&ok);
298 converted_object = input_string.trimmed().toInt(&ok);
303 converted_object = input_string.trimmed().toFloat(&ok);
308 return converted_object;
bool HasNextRow()
Returns True if there are more rows to be read.
static const double EBROKEN_DOUBLE
These are the values used in case of error in conversion.
static const QString EBROKEN_QSTRING
DataTypes
DataTypes for building sequence D_QSTRING QString Type D_INT Integer Type D_FLOAT Floating Point Type...
KSParser(const QString &filename, const char comment_char, const QList< QPair< QString, DataTypes > > &sequence, const char delimiter= ',')
Returns a CSV parsing instance of a KSParser type object.
void ShowProgress()
Wrapper function for KSFileReader showProgress.
static const int EBROKEN_INT
void SetProgress(QString msg, int total_lines, int step_size)
Wrapper function for KSFileReader setProgress.
static const float EBROKEN_FLOAT
void setProgress(QString label, unsigned int lastLine, unsigned int numUpdates=10)
bool openFullPath(const QString &fname)
QHash< QString, QVariant > ReadNextRow()
Generic function used to read the next row of a text file.