00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _FDSTREAM_
00011 #define _FDSTREAM_
00012
00013 #include <iostream>
00014 #include <string>
00015
00016 #if defined(__GLIBCXX__) || defined(__GLIBCPP__)
00017 #include <ext/stdio_filebuf.h>
00018
00019 #include "util/file.hh"
00020
00021 #define BUFFER_SIZE (32768)
00022
00023 namespace MosesTuning
00024 {
00025
00026 class _fdstream
00027 {
00028 protected:
00029 _fdstream() :
00030 _file_descriptor(), _filebuf(NULL) {
00031 }
00032
00033 _fdstream(int file_descriptor, std::ios_base::openmode openmode) :
00034 _file_descriptor(-1), _openmode(openmode) {
00035 _filebuf = NULL;
00036 open(file_descriptor, openmode);
00037 _file_descriptor.reset(file_descriptor);
00038 }
00039
00040 std::ios_base::openmode openmode() const {
00041 return _openmode;
00042 }
00043
00044 void open(int file_descriptor, std::ios_base::openmode openmode) {
00045
00046
00047 if (!_filebuf) {
00048
00049
00050
00051
00052
00053 _filebuf = new __gnu_cxx::stdio_filebuf<char> (file_descriptor,
00054 openmode);
00055 }
00056 }
00057
00058 virtual ~_fdstream() {
00059 delete _filebuf;
00060 _filebuf = NULL;
00061 }
00062
00063 private:
00064 util::scoped_fd _file_descriptor;
00065 __gnu_cxx::stdio_filebuf<char>* _filebuf;
00066 std::ios_base::openmode _openmode;
00067
00068 protected:
00070 __gnu_cxx::stdio_filebuf<char> *get_filebuf() {
00071 return _filebuf;
00072 }
00073 };
00074
00075 class ifdstream : public _fdstream
00076 {
00077 public:
00078 ifdstream() :
00079 _fdstream(), _stream(NULL) {
00080 }
00081
00082 ifdstream(int file_descriptor) :
00083 _fdstream(file_descriptor, std::ios_base::in) {
00084 _stream = new std::istream(get_filebuf());
00085 }
00086
00087 void open(int file_descriptor) {
00088 if (!_stream) {
00089 _fdstream::open(file_descriptor, std::ios_base::in);
00090 _stream = new std::istream(get_filebuf());
00091 }
00092 }
00093
00094 ifdstream& operator>> (std::string& str) {
00095 (*_stream) >> str;
00096
00097 return *this;
00098 }
00099
00100 std::size_t getline(std::string& str) {
00101 char tmp[BUFFER_SIZE];
00102 std::size_t ret = getline(tmp, BUFFER_SIZE);
00103 str = tmp;
00104 return ret;
00105 }
00106
00107 std::size_t getline(char* s, std::streamsize n) {
00108 return (getline(s, n, '\n'));
00109 }
00110
00111 std::size_t getline(char* s, std::streamsize n, char delim) {
00112 int i = 0;
00113 do {
00114 s[i] = _stream->get();
00115 i++;
00116 } while(i < n-1 && s[i-1] != delim && s[i-1] != '\0');
00117
00118 s[i-1] = '\0';
00119
00120 return i-1;
00121 }
00122
00123 ~ifdstream() {
00124
00125 delete _stream;
00126 }
00127
00128 private:
00129 std::istream* _stream;
00130 };
00131
00132 class ofdstream : public _fdstream
00133 {
00134 public:
00135 ofdstream() :
00136 _fdstream(), _stream(NULL) {
00137 }
00138
00139 ofdstream(int file_descriptor) :
00140 _fdstream(file_descriptor, std::ios_base::out) {
00141 _stream = new std::ostream(get_filebuf());
00142 }
00143
00144 void open(int file_descriptor) {
00145 if (!_stream) {
00146 _fdstream::open(file_descriptor, std::ios_base::out);
00147 _stream = new std::ostream(get_filebuf());
00148 }
00149 }
00150
00151
00152 ofdstream& operator<< (const std::string& str) {
00153 if (_stream->good())
00154 (*_stream) << str;
00155
00156 _stream->flush();
00157 return *this;
00158 }
00159
00160 ~ofdstream() {
00161
00162 delete _stream;
00163 }
00164
00165 private:
00166 std::ostream* _stream;
00167 };
00168
00169 #else
00170 #error "Not supported"
00171 #endif
00172
00173 }
00174
00175 #endif // _FDSTREAM_