|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _FDSTREAM_ |
|
#define _FDSTREAM_ |
|
|
|
#include <iostream> |
|
#include <string> |
|
|
|
#if defined(__GLIBCXX__) || defined(__GLIBCPP__) |
|
#include <ext/stdio_filebuf.h> |
|
|
|
#include "util/file.hh" |
|
|
|
#define BUFFER_SIZE (32768) |
|
|
|
namespace MosesTuning |
|
{ |
|
|
|
class _fdstream |
|
{ |
|
protected: |
|
_fdstream() : |
|
_file_descriptor(), _filebuf(NULL) { |
|
} |
|
|
|
_fdstream(int file_descriptor, std::ios_base::openmode openmode) : |
|
_file_descriptor(-1), _openmode(openmode) { |
|
_filebuf = NULL; |
|
open(file_descriptor, openmode); |
|
_file_descriptor.reset(file_descriptor); |
|
} |
|
|
|
std::ios_base::openmode openmode() const { |
|
return _openmode; |
|
} |
|
|
|
void open(int file_descriptor, std::ios_base::openmode openmode) { |
|
|
|
|
|
if (!_filebuf) { |
|
|
|
|
|
|
|
|
|
|
|
_filebuf = new __gnu_cxx::stdio_filebuf<char> (file_descriptor, |
|
openmode); |
|
} |
|
} |
|
|
|
virtual ~_fdstream() { |
|
delete _filebuf; |
|
_filebuf = NULL; |
|
} |
|
|
|
private: |
|
util::scoped_fd _file_descriptor; |
|
__gnu_cxx::stdio_filebuf<char>* _filebuf; |
|
std::ios_base::openmode _openmode; |
|
|
|
protected: |
|
|
|
__gnu_cxx::stdio_filebuf<char> *get_filebuf() { |
|
return _filebuf; |
|
} |
|
}; |
|
|
|
class ifdstream : public _fdstream |
|
{ |
|
public: |
|
ifdstream() : |
|
_fdstream(), _stream(NULL) { |
|
} |
|
|
|
ifdstream(int file_descriptor) : |
|
_fdstream(file_descriptor, std::ios_base::in) { |
|
_stream = new std::istream(get_filebuf()); |
|
} |
|
|
|
void open(int file_descriptor) { |
|
if (!_stream) { |
|
_fdstream::open(file_descriptor, std::ios_base::in); |
|
_stream = new std::istream(get_filebuf()); |
|
} |
|
} |
|
|
|
ifdstream& operator>> (std::string& str) { |
|
(*_stream) >> str; |
|
|
|
return *this; |
|
} |
|
|
|
std::size_t getline(std::string& str) { |
|
char tmp[BUFFER_SIZE]; |
|
std::size_t ret = getline(tmp, BUFFER_SIZE); |
|
str = tmp; |
|
return ret; |
|
} |
|
|
|
std::size_t getline(char* s, std::streamsize n) { |
|
return (getline(s, n, '\n')); |
|
} |
|
|
|
std::size_t getline(char* s, std::streamsize n, char delim) { |
|
int i = 0; |
|
do { |
|
s[i] = _stream->get(); |
|
i++; |
|
} while(i < n-1 && s[i-1] != delim && s[i-1] != '\0'); |
|
|
|
s[i-1] = '\0'; |
|
|
|
return i-1; |
|
} |
|
|
|
~ifdstream() { |
|
|
|
delete _stream; |
|
} |
|
|
|
private: |
|
std::istream* _stream; |
|
}; |
|
|
|
class ofdstream : public _fdstream |
|
{ |
|
public: |
|
ofdstream() : |
|
_fdstream(), _stream(NULL) { |
|
} |
|
|
|
ofdstream(int file_descriptor) : |
|
_fdstream(file_descriptor, std::ios_base::out) { |
|
_stream = new std::ostream(get_filebuf()); |
|
} |
|
|
|
void open(int file_descriptor) { |
|
if (!_stream) { |
|
_fdstream::open(file_descriptor, std::ios_base::out); |
|
_stream = new std::ostream(get_filebuf()); |
|
} |
|
} |
|
|
|
|
|
ofdstream& operator<< (const std::string& str) { |
|
if (_stream->good()) |
|
(*_stream) << str; |
|
|
|
_stream->flush(); |
|
return *this; |
|
} |
|
|
|
~ofdstream() { |
|
|
|
delete _stream; |
|
} |
|
|
|
private: |
|
std::ostream* _stream; |
|
}; |
|
|
|
#else |
|
#error "Not supported" |
|
#endif |
|
|
|
} |
|
|
|
#endif |
|
|