summaryrefslogtreecommitdiffstats
path: root/kbarcode/csvfile.cpp
blob: 5d138d84d2c6ac0b60fdb8dd4b8e00fbcf386811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
                          csvfile.h  -  description
                             -------------------
    begin                : Mon Mar 28 2005
    copyright            : (C) 2005 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "csvfile.h"

#include "printersettings.h"

#include <tqbuffer.h>
#include <tqtextcodec.h>

CSVFile::CSVFile( const TQString & filename )
{
    m_eof = false;
    m_csv = true;

    m_quote = PrinterSettings::getInstance()->getData()->quote;
    m_separator = PrinterSettings::getInstance()->getData()->separator;
    m_comment = PrinterSettings::getInstance()->getData()->comment;

    m_file.setName( filename );
    m_file.open( IO_ReadOnly );

    if( m_file.isOpen() )
	m_stream.setDevice( &m_file );
}

CSVFile::CSVFile( TQBuffer & buf )
{
    m_eof = false;
    m_csv = true;

    m_quote = PrinterSettings::getInstance()->getData()->quote;
    m_separator = PrinterSettings::getInstance()->getData()->separator;
    m_comment = PrinterSettings::getInstance()->getData()->comment;

    buf.open( IO_ReadOnly );

    if( buf.isOpen() )
	m_stream.setDevice( &buf );
}

CSVFile::~CSVFile()
{
    if( m_stream.device() && m_stream.device()->isOpen() )
	m_stream.device()->close();
}

TQStringList CSVFile::readNextLine()
{
    TQString line;

    if( !m_stream.device() )
	return TQStringList();

    if( !m_stream.device()->isOpen() )
	return TQStringList();

    // walk through the lines until a line containing valid data
    for( ;; )
    {
	line = m_stream.readLine();
	line = line.stripWhiteSpace();

	// check for eof
	if( line.isNull() )
	{
	    m_eof = true;
	    return TQStringList();
	}
	
	// ignore comments and empty lines
	if( (!m_comment.isEmpty() && line.startsWith( m_comment )) || line.isEmpty() ) 
	    continue;

	break;
    }

    return m_csv ? readCsvLine( line ) : readFixedLine( line );
}

TQStringList CSVFile::readCsvLine( const TQString & l )
{
    TQString     line( l );
    TQStringList sections;
    unsigned int start   = 0;
    unsigned int end     = 0;
    unsigned int len;
    bool quoted          = false;
    bool quote_empty;

    // if line does not end with separator, add one
    if( !line.endsWith( m_separator ) )
	line.append( m_separator );

    // we have to handle here the case, 
    // that the separator is included 
    // in a quoted field
    len = line.length(); // cache for better speed
    quote_empty = m_quote.isEmpty();
    while( end < len )
    {
        if( !quote_empty && line.right( len - end ).startsWith( m_quote ) ) 
            quoted = !quoted;
        else if( !quoted && line.right( len - end ).startsWith( m_separator ) ) 
        {
            sections << removeQuote( line.mid( start, end-start ) );

            start = end;
            ++start;
        }
        else if( line[end] == '\n' )
            break;
        
        ++end;
    }

    return sections;
}

TQStringList CSVFile::readFixedLine( const TQString & line )
{
    TQString     data( line );
    TQStringList list;

    for( unsigned int i=0;i<m_width.count();i++ )
    {
        list << data.left( m_width[i] );
        data = data.right( data.length() - m_width[i] );
    }

    return list;
}

TQString CSVFile::removeQuote( const TQString & text ) 
{
    TQString line = text.stripWhiteSpace();

    if( m_quote.isEmpty() )
        return text;

    if( line.startsWith( m_quote ) )
	line = line.right( line.length() - m_quote.length() );

    if( line.endsWith( m_quote ) )
	line = line.left( line.length() - m_quote.length() );

    return line;
}

void CSVFile::setEncoding( const TQString & enc )
{
    m_stream.setCodec( TQTextCodec::codecForName( enc.latin1() ) );
}