Krita Source Code Documentation
Loading...
Searching...
No Matches
csv_read_line.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Laszlo Fazekas <mneko@freemail.hu>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "csv_read_line.h"
8
9#include <kis_debug.h>
10
11#include <QDebug>
12#include <QIODevice>
13
17
19 : m_separator(0)
20 , m_row(0)
21 , m_linebuf(0)
22 , m_pos(-1)
23{
24}
25
29
30// returns: 0 finished, + continue, - error
31int CSVReadLine::nextLine(QIODevice *io)
32{
33 int retval= 0;
34 m_pos= -1;
35
36 try {
37 m_linebuf= io->readLine();
38
39 if (!m_linebuf.size())
40 retval= 0; //finished
41 else {
42 if (!m_separator)
43 m_separator= ((m_linebuf.size() > 5) && (m_linebuf[5] == ';')) ?
44 ';' : ',';
45 m_pos= 0;
46 retval= 1;
47 }
49 warnKrita << "WARNING: CSV:" << e.what();
50 retval= -1; //error
51 }
52 return retval;
53}
54
56{
57 char strBuf[CSV_FIELD_MAX];
58 char *ptr;
59 char c;
60 int i,p,max;
61
62 p= m_pos;
63
64 if (p < 0) return QString();
65
66 ptr= strBuf;
67 max= m_linebuf.size();
68
69 do { if (p >= max) {
70 ptr[0]= 0;
71 m_pos= -1;
72 return QString(strBuf);
73 }
74 c= m_linebuf[p++];
75 } while((c == ' ') || (c == '\t'));
76
77 i= 0;
78
79 if (c == '\"') {
80 //quoted
81 while(p < max) {
82 c= m_linebuf[p++];
83
84 if (c == '\"') {
85
86 if (p >= max) break;
87
88 if (m_linebuf[p] != c) break;
89
90 //double quote escape sequence
91 ++p;
92 }
93 if (i < (CSV_FIELD_MAX - 1))
94 ptr[i++]= c;
95 }
96
97 while (p < max) {
98 c= m_linebuf[p++];
99 if (c == m_separator) break;
100 }
101 } else {
102 //without quotes
103 while (c != m_separator) {
104 if (i < (CSV_FIELD_MAX - 1))
105 ptr[i++]= c;
106
107 if (p >= max) break;
108
109 c= m_linebuf[p++];
110 }
111
112 while(i > 0) {
113 c= ptr[--i];
114 if ((c != ' ') && (c != '\t') &&
115 (c != '\r') && (c != '\n')) {
116 ++i;
117 break;
118 }
119 }
120 }
121 ptr[i]= 0;
122 m_pos= (p < max) ? p : -1;
123 return QString(strBuf);
124}
125
127{
128 m_pos= 0;
129}
const Params2D p
QString nextField()
int nextLine(QIODevice *io)
QByteArray m_linebuf
#define CSV_FIELD_MAX
#define warnKrita
Definition kis_debug.h:87