File: tsv.h

package info (click to toggle)
librime 1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,608 kB
  • ctags: 25,041
  • sloc: cpp: 119,202; sh: 21,794; ansic: 7,346; python: 4,372; makefile: 863; perl: 288; ruby: 50
file content (77 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download
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
//
// Copyleft RIME Developers
// License: GPLv3
//
// 2013-04-16 GONG Chen <chen.sst@gmail.com>
//
#ifndef RIME_TSV_H_
#define RIME_TSV_H_

#include <string>
#include <vector>
#include <boost/function.hpp>

namespace rime {

typedef std::vector<std::string> Tsv;

typedef boost::function<bool (const Tsv& row,
                              std::string* key,
                              std::string* value)> TsvParser;

typedef boost::function<bool (const std::string& key,
                              const std::string& value,
                              Tsv* row)> TsvFormatter;

class Sink;
class Source;

class TsvReader {
 public:
  TsvReader(const std::string& path, TsvParser parser)
      : path_(path), parser_(parser) {
  }
  // return number of records read
  int operator() (Sink* sink);
 protected:
  std::string path_;
  TsvParser parser_;
};

class TsvWriter {
 public:
  TsvWriter(const std::string& path, TsvFormatter formatter)
      : path_(path), formatter_(formatter) {
  }
  // return number of records written
  int operator() (Source* source);
 protected:
  std::string path_;
  TsvFormatter formatter_;
 public:
  std::string file_description;
};

template <class SinkType>
int operator<< (SinkType& sink, TsvReader& reader) {
  return reader(&sink);
}

template <class SinkType>
int operator>> (TsvReader& reader, SinkType& sink) {
  return reader(&sink);
}

template <class SourceType>
int operator<< (TsvWriter& writer, SourceType& source) {
  return writer(&source);
}

template <class SourceType>
int operator>> (SourceType& source, TsvWriter& writer) {
  return writer(&source);
}

}  // namespace rime

#endif  // RIME_TSV_H_