-
Notifications
You must be signed in to change notification settings - Fork 193
/
stream_wrapper.h
57 lines (50 loc) · 1.21 KB
/
stream_wrapper.h
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
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: stream_wrapper.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#ifndef CRFPP_STREAM_WRAPPER_H_
#define CRFPP_STREAM_WRAPPER_H_
#include <iostream>
#include <fstream>
#include <cstring>
#include "common.h"
namespace CRFPP {
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(WPATH(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(WPATH(filename));
}
~ostream_wrapper() {
if (os != &std::cout) delete os;
}
};
}
#endif