csv.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_CSV_H
30 #define CXXTOOLS_CSV_H
31 
32 #include <cxxtools/csvserializer.h>
34 #include <iostream>
35 
36 namespace cxxtools
37 {
58  template <typename ObjectType>
59  class CsvOObject
60  {
61  const ObjectType& _object;
62  Char _delimiter;
63  Char _quote;
64  String _lineEnding;
65 
66  public:
68  explicit CsvOObject(const ObjectType& object)
69  : _object(object),
70  _delimiter(0),
71  _quote(0)
72  { }
73 
75  { _delimiter = delimiter; return *this; }
76 
78  { _quote = quote; return *this; }
79 
81  { _lineEnding = lineEnding; return *this; }
82 
83  Char delimiter() const
84  { return _delimiter; }
85 
86  Char quote() const
87  { return _quote; }
88 
89  const String& lineEnding() const
90  { return _lineEnding; }
91 
92  const ObjectType& object() const
93  { return _object; }
94  };
95 
97  template <typename CharType, typename ObjectType>
98  std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& out, const CsvOObject<ObjectType>& object)
99  {
100  try
101  {
102  CsvSerializer serializer(out);
103 
104  if (object.delimiter() != CsvParser::autoDelimiter)
105  serializer.delimiter(object.delimiter());
106  if (object.quote() != Char(0))
107  serializer.quote(object.quote());
108  if (!object.lineEnding().empty())
109  serializer.lineEnding(object.lineEnding());
110 
111  serializer.serialize(object.object());
112  }
113  catch (const std::exception&)
114  {
115  out.setstate(std::ios::failbit);
116  throw;
117  }
118 
119  return out;
120  }
121 
125  template <typename ObjectType>
126  CsvOObject<ObjectType> Csv(const ObjectType& object)
127  {
128  return CsvOObject<ObjectType>(object);
129  }
130 
131  template <typename ObjectType>
132  class CsvIOObject : public CsvOObject<ObjectType>
133  {
134  ObjectType& _object;
135  bool _readTitle;
136 
137  public:
138  explicit CsvIOObject(ObjectType& object)
139  : CsvOObject<ObjectType>(object),
140  _object(object),
141  _readTitle(true)
142  { }
143 
145  { CsvOObject<ObjectType>::delimiter(delimiter); return *this; }
146 
148  { _readTitle = sw; return *this; }
149 
150  Char delimiter() const
152 
153  bool readTitle() const
154  { return _readTitle; }
155 
156  ObjectType& object()
157  { return _object; }
158  };
159 
161  template <typename CharType, typename ObjectType>
162  std::basic_istream<CharType>& operator>> (std::basic_istream<CharType>& in, CsvIOObject<ObjectType> object)
163  {
164  try
165  {
166  CsvDeserializer deserializer;
167  if (object.delimiter() != CsvParser::autoDelimiter)
168  deserializer.delimiter(object.delimiter());
169  deserializer.readTitle(object.readTitle());
170 
171  deserializer.read(in);
172 
173  deserializer.deserialize(object.object());
174  }
175  catch (const std::exception&)
176  {
177  in.setstate(std::ios::failbit);
178  throw;
179  }
180 
181  return in;
182  }
183 
184  template <typename ObjectType>
185  CsvIOObject<ObjectType> Csv(ObjectType& object)
186  {
187  return CsvIOObject<ObjectType>(object);
188  }
189 
190 }
191 
192 #endif // CXXTOOLS_CSV_H
193