File: stream_wrapper.h

package info (click to toggle)
zinnia 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,368 kB
  • ctags: 2,156
  • sloc: cpp: 13,195; sh: 9,667; perl: 223; python: 200; makefile: 193; ansic: 70; ruby: 47
file content (63 lines) | stat: -rw-r--r-- 1,398 bytes parent folder | download | duplicates (7)
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
//
//  Zinnia: Online hand recognition system with machine learning
//
//  $Id: stream_wrapper.h 17 2009-04-05 11:40:32Z taku-ku $;
//
//  Copyright(C) 2008 Taku Kudo <taku@chasen.org>
//
//
//  CRF++ -- Yet Another CRF toolkit
//
//  $Id: stream_wrapper.h 17 2009-04-05 11:40:32Z taku-ku $;
//
//  Copyright(C) 2005-2007 Taku Kudo <taku@chasen.org>
//
#ifndef zinnia_STREAM_WRAPPER_H_
#define zinnia_STREAM_WRAPPER_H_

#include <iostream>
#include <fstream>
#include <cstring>

namespace zinnia {

class istream_wrapper {
 private:
  std::istream* is;
 public:
  std::istream &operator*() const  { return *is; }
  std::istream *operator->() const { return is;  }
  std::istream *get() { return is; }
  explicit istream_wrapper(const char* filename): is(0) {
    if (std::strcmp(filename, "-") == 0)
      is = &std::cin;
    else
      is = new std::ifstream(filename);
  }

  ~istream_wrapper() {
    if (is != &std::cin) delete is;
  }
};

class ostream_wrapper {
 private:
  std::ostream* os;
 public:
  std::ostream &operator*() const  { return *os; }
  std::ostream *operator->() const { return os;  }
  std::ostream *get() { return os; }
  explicit ostream_wrapper(const char* filename): os(0) {
    if (std::strcmp(filename, "-") == 0)
      os = &std::cout;
    else
      os = new std::ofstream(filename);
  }

  ~ostream_wrapper() {
    if (os != &std::cout) delete os;
  }
};
}

#endif