summaryrefslogtreecommitdiffstats
path: root/kbarcode/csvfile.h
blob: 9a0c82965750dda358851f0a24e464da64fdc1a4 (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
/***************************************************************************
                          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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef CSVFILE_H
#define CSVFILE_H

#include <tqfile.h>
#include <tqstringlist.h>
#include <tqtextstream.h>

class TQBuffer;

/** This class makes it very easy to parse a comma separated value file
 */
class CSVFile {
    public:
       CSVFile( const TQString & filename );
       CSVFile( TQBuffer & buf );
       ~CSVFile();

       /** reads the next line from the CSV file and returns 
	 * the line split into sections. Comments are ignored by this
	 * line. 
	 */
       TQStringList readNextLine();

       /** returns true when the CSVFile object is valid.
	*  returns false incase of an error
	*/
       inline bool isValid() const;

       /** returns true when the end of file was reached
	*/
       inline bool isEof() const;

       /** set the encoding to use for the data
        */
       void setEncoding( const TQString & enc );

       /** set the separator to use for the data
        */
       inline void setSeparator( const TQString & sep );

       /** set the quoting character
        */
       inline void setQuote( const TQString & quote );

       /** set the comment character
        */
       inline void setComment( const TQString & comment );

       /** set the field widths for fixed field width files
        */
       inline void setFieldWidth( const TQValueList<int> & width );

       /** sets wether this is a CSV file or 
        *  a file with fixed field width.
        *  \param b if true this is a CSV file (default)
        */
       inline void setCSVFile( bool b );

       /**
        * \returns true if this is a CSV file
        */
       inline bool isCSVFile() const;

    private:
       /** removes quoting characters as defined in lpdata from the 
	* string @p text
	*/
       TQString removeQuote( const TQString & text );

       TQStringList readCsvLine( const TQString & line );

       TQStringList readFixedLine( const TQString & line );

    private:
       /** the filehandle which is used to access the file
	*/
       TQFile       m_file;
       TQTextStream m_stream;

       TQString m_quote;
       TQString m_separator;
       TQString m_comment;

       TQValueList<int> m_width;

       bool m_csv;
       bool m_eof;
};

bool CSVFile::isValid() const
{
    if( !m_stream.tqdevice() )
	return false;

    return m_stream.tqdevice()->isOpen();
}

bool CSVFile::isEof() const
{
    return m_eof;
}

void CSVFile::setCSVFile( bool b ) 
{
    m_csv = b;
}

bool CSVFile::isCSVFile() const 
{
    return m_csv;
}

void CSVFile::setSeparator( const TQString & sep )
{
    m_separator = sep;
}

void CSVFile::setQuote( const TQString & quote )
{
    m_quote = quote;
}

void CSVFile::setComment( const TQString & comment )
{
    m_comment = comment;
}

void CSVFile::setFieldWidth( const TQValueList<int> & width )
{
    m_width = width;
}


#endif // CSVFILE_H