source: mmcs/csv.cpp @ 6cc17c0

matrices
Last change on this file since 6cc17c0 was 44d1e60, checked in by Jose Ruiz <joseruiz@…>, 9 years ago

commit inicial

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include "csv.h"
2#include <QTextDecoder>
3
4CSV::CSV(QIODevice * device)
5{
6        m_device = device;
7        m_codec = QTextCodec::codecForLocale();
8        m_pos = 0;
9        m_rx = QRegExp("((?:(?:[^;\\n]*;?)|(?:\"[^\"]*\";?))*)\\n");
10}
11CSV::CSV(QString &string){     
12        m_device = NULL;
13        m_codec = QTextCodec::codecForLocale();
14        m_string = string;     
15        m_pos = 0;
16        m_rx = QRegExp("((?:(?:[^;\\n]*;?)|(?:\"[^\"]*\";?))*)\\n");
17}
18
19CSV::~CSV()
20{
21        //delete m_codec;
22}
23
24
25void CSV::setCodec(const char* codecName){
26        //delete m_codec;
27        m_codec = QTextCodec::codecForName(codecName);
28}
29
30QString CSV::readLine(){
31        QString line;
32
33        if(m_string.isNull()){
34                //READ DATA FROM DEVICE
35                if(m_device && m_device->isReadable()){
36                        QTextDecoder dec(m_codec);
37                        m_string = dec.toUnicode(m_device->readAll());
38                }else{
39                        return QString();
40                }
41        }
42
43        //PARSE
44        if((m_pos = m_rx.indexIn(m_string,m_pos)) != -1) {
45                line = m_rx.cap(1);             
46                m_pos += m_rx.matchedLength();
47        }
48        return line;
49       
50}
51QStringList CSV::parseLine(){
52        return parseLine(readLine());
53}
54QStringList CSV::parseLine(QString line){
55        QStringList list;
56        int pos2 = 0;
57        QRegExp rx2("(?:\"([^\"]*)\";?)|(?:([^;]*);?)");
58        if(line.size()<1){
59                list << "";             
60        }else while (line.size()>pos2 && (pos2 = rx2.indexIn(line, pos2)) != -1) {
61                QString col;
62                if(rx2.cap(1).size()>0)
63                        col = rx2.cap(1);
64                else if(rx2.cap(2).size()>0)
65                        col = rx2.cap(2);
66               
67                list << col;                   
68
69                if(col.size())
70                        pos2 += rx2.matchedLength();
71                else
72                        pos2++;
73        }
74        return list;
75}
Note: See TracBrowser for help on using the repository browser.