File indexing completed on 2024-12-22 04:15:37
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Laszlo Fazekas <mneko@freemail.hu> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "csv_read_line.h" 0008 0009 #include <kis_debug.h> 0010 0011 #include <QDebug> 0012 #include <QIODevice> 0013 0014 #include <asl/kis_offset_keeper.h> 0015 #include <asl/kis_asl_writer_utils.h> 0016 #include <asl/kis_asl_reader_utils.h> 0017 0018 CSVReadLine::CSVReadLine() 0019 : m_separator(0) 0020 , m_row(0) 0021 , m_linebuf(0) 0022 , m_pos(-1) 0023 { 0024 } 0025 0026 CSVReadLine::~CSVReadLine() 0027 { 0028 } 0029 0030 // returns: 0 finished, + continue, - error 0031 int CSVReadLine::nextLine(QIODevice *io) 0032 { 0033 int retval= 0; 0034 m_pos= -1; 0035 0036 try { 0037 m_linebuf= io->readLine(); 0038 0039 if (!m_linebuf.size()) 0040 retval= 0; //finished 0041 else { 0042 if (!m_separator) 0043 m_separator= ((m_linebuf.size() > 5) && (m_linebuf[5] == ';')) ? 0044 ';' : ','; 0045 m_pos= 0; 0046 retval= 1; 0047 } 0048 } catch(KisAslReaderUtils::ASLParseException &e) { 0049 warnKrita << "WARNING: CSV:" << e.what(); 0050 retval= -1; //error 0051 } 0052 return retval; 0053 } 0054 0055 QString CSVReadLine::nextField() 0056 { 0057 char strBuf[CSV_FIELD_MAX]; 0058 char *ptr; 0059 char c; 0060 int i,p,max; 0061 0062 p= m_pos; 0063 0064 if (p < 0) return QString(); 0065 0066 ptr= strBuf; 0067 max= m_linebuf.size(); 0068 0069 do { if (p >= max) { 0070 ptr[0]= 0; 0071 m_pos= -1; 0072 return QString(strBuf); 0073 } 0074 c= m_linebuf[p++]; 0075 } while((c == ' ') || (c == '\t')); 0076 0077 i= 0; 0078 0079 if (c == '\"') { 0080 //quoted 0081 while(p < max) { 0082 c= m_linebuf[p++]; 0083 0084 if (c == '\"') { 0085 0086 if (p >= max) break; 0087 0088 if (m_linebuf[p] != c) break; 0089 0090 //double quote escape sequence 0091 ++p; 0092 } 0093 if (i < (CSV_FIELD_MAX - 1)) 0094 ptr[i++]= c; 0095 } 0096 0097 while (p < max) { 0098 c= m_linebuf[p++]; 0099 if (c == m_separator) break; 0100 } 0101 } else { 0102 //without quotes 0103 while (c != m_separator) { 0104 if (i < (CSV_FIELD_MAX - 1)) 0105 ptr[i++]= c; 0106 0107 if (p >= max) break; 0108 0109 c= m_linebuf[p++]; 0110 } 0111 0112 while(i > 0) { 0113 c= ptr[--i]; 0114 if ((c != ' ') && (c != '\t') && 0115 (c != '\r') && (c != '\n')) { 0116 ++i; 0117 break; 0118 } 0119 } 0120 } 0121 ptr[i]= 0; 0122 m_pos= (p < max) ? p : -1; 0123 return QString(strBuf); 0124 } 0125 0126 void CSVReadLine::rewind() 0127 { 0128 m_pos= 0; 0129 }