ClockWork DB CoreAPI 1.0.48
Abstract Time Series and Storage/Management Library
Loading...
Searching...
No Matches
csv_stream_parser.hpp
1#ifndef HAVE_TOM__CSV_STREAM_PARSER_HPP
2#define HAVE_TOM__CSV_STREAM_PARSER_HPP
3
4#include <tom-util/defines.hpp>
5#include <tom-util/scalar.hpp>
6#include <boost/tokenizer.hpp>
7
8#include <iosfwd>
9#include <string>
10#include <iostream> // needed for debug stmt only
11#include <vector>
12#include <cctype>
13
14//TOM_NAMESPACE_BEGIN;
15namespace tom {
16
18 {
19 public:
20 typedef std::vector< tom::String> token_vector;
21 typedef token_vector::const_iterator const_iterator;
22 typedef token_vector::iterator iterator;
23
24 typedef boost::tokenizer< boost::escaped_list_separator<char> > escaped_list_tokenizer;
25 typedef boost::tokenizer< boost::char_separator<char> > char_tokenizer;
26 typedef boost::tokenizer< boost::offset_separator > offset_tokenizer;
27
28 csv_stream_parser( std::istream & , char delim = ',', char escape = '\\', char quote = '\"');
29 virtual ~csv_stream_parser() {}
30
31 void init();
32 void init(char delim);
33 void init(const std::vector<int> &offsets);
34
35 virtual bool
36 nextLine();
37
38 bool
39 eof() const { return m_stream.eof(); }
40
41 bool
42 good() const { return m_stream.good(); }
43
44 virtual const_iterator
45 begin() const { return m_tokens.begin(); }
46
47 virtual iterator
48 begin() { return m_tokens.begin(); }
49
50 virtual const_iterator
51 end() const { return m_tokens.end(); }
52
53 virtual iterator
54 end() { return m_tokens.end(); }
55
56 virtual size_t
57 size() { return m_tokens.size(); }
58
59 virtual const String &
60 operator[]( int i )
61 {
62 return m_tokens[i];
63 }
64
65 protected:
66 token_vector m_tokens;
67 std::istream &m_stream;
68 boost::escaped_list_separator<char> m_escapedListSeparator;
69 boost::offset_separator m_OffsetSeparator;
70 bool m_fixedLength;
71 bool m_Initialized;
72 char m_delim;
73 char m_escape;
74 char m_quote;
75
76 private:
77 static void
78 trimToken( std::string &s );
79
80 void
81 reset() { m_tokens.clear(); m_line.clear(); }
82
84 csv_stream_parser &operator=( const csv_stream_parser & );
85
86 std::string m_line;
87
88 struct whitespace {
89 whitespace() : m_stop(false) {}
90 bool operator()( const char & val )
91 {
92 if ( m_stop ) return false;
93 if ( std::isspace( val ) != 0 )
94 return true;
95 else
96 {
97 m_stop = true;
98 return false;
99 }
100 }
101 void reset() { m_stop = false; }
102 bool m_stop;
103 };
104
105 public:
106 bool
107 initialized() const { return m_Initialized; }
108 };
109
110//TOM_NAMESPACE_END;
111} // end tom namespace
112
113#endif
Definition string.hpp:15
Definition csv_stream_parser.hpp:18