mosesdecoder/mert/Fdstream.h

159 lines
3.1 KiB
C
Raw Normal View History

/*
* This class creates c++ like stream from file descriptor
*/
#ifndef _FDSTREAM_
#define _FDSTREAM_
#include <iostream>
#include <string>
#if defined(__GLIBCXX__) || defined(__GLIBCPP__)
#include <ext/stdio_filebuf.h>
2012-05-10 02:08:54 +04:00
#define BUFFER_SIZE (32768)
2013-05-29 21:16:15 +04:00
namespace MosesTuning
{
class _fdstream
{
protected:
_fdstream() :
_file_descriptor(-1), _filebuf(NULL) {
}
_fdstream(int file_descriptor, std::ios_base::openmode openmode) :
2013-05-29 21:16:15 +04:00
_file_descriptor(file_descriptor), _openmode(openmode) {
_filebuf = NULL;
open(file_descriptor, openmode);
}
2013-05-29 21:16:15 +04:00
std::ios_base::openmode openmode() const {
return _openmode;
}
2013-05-29 21:16:15 +04:00
void open(int file_descriptor, std::ios_base::openmode openmode) {
if (!_filebuf)
// We create a C++ stream from a file descriptor
// stdio_filebuf is not synced with stdio.
// From GCC 3.4.0 on exists in addition stdio_sync_filebuf
// You can also create the filebuf from a FILE* with
// FILE* f = fdopen(file_descriptor, mode);
_filebuf = new __gnu_cxx::stdio_filebuf<char> (file_descriptor,
2013-05-29 21:16:15 +04:00
openmode);
}
2013-05-29 21:16:15 +04:00
virtual ~_fdstream() {
close(_file_descriptor);
delete _filebuf;
_filebuf = NULL;
}
int _file_descriptor;
__gnu_cxx::stdio_filebuf<char>* _filebuf;
std::ios_base::openmode _openmode;
};
class ifdstream : public _fdstream
{
public:
ifdstream() :
_fdstream(), _stream(NULL) {
}
ifdstream(int file_descriptor) :
2013-05-29 21:16:15 +04:00
_fdstream(file_descriptor, std::ios_base::in) {
_stream = new std::istream(_filebuf);
}
2013-05-29 21:16:15 +04:00
void open(int file_descriptor) {
if (!_stream) {
_fdstream::open(file_descriptor, std::ios_base::in);
_stream = new std::istream(_filebuf);
}
}
2013-05-29 21:16:15 +04:00
ifdstream& operator>> (std::string& str) {
(*_stream) >> str;
return *this;
}
2013-05-29 21:16:15 +04:00
std::size_t getline(std::string& str) {
char tmp[BUFFER_SIZE];
std::size_t ret = getline(tmp, BUFFER_SIZE);
str = tmp;
return ret;
}
2013-05-29 21:16:15 +04:00
std::size_t getline(char* s, std::streamsize n) {
return (getline(s, n, '\n'));
}
2013-05-29 21:16:15 +04:00
std::size_t getline(char* s, std::streamsize n, char delim) {
int i = 0;
2013-05-29 21:16:15 +04:00
do {
s[i] = _stream->get();
i++;
2013-05-29 21:16:15 +04:00
} while(i < n-1 && s[i-1] != delim && s[i-1] != '\0');
s[i-1] = '\0'; // overwrite the delimiter given with string end
return i-1;
}
2013-05-29 21:16:15 +04:00
~ifdstream() {
//this->~_fdstream();
delete _stream;
}
private:
std::istream* _stream;
};
class ofdstream : public _fdstream
{
public:
ofdstream() :
_fdstream(), _stream(NULL) {
}
ofdstream(int file_descriptor) :
2013-05-29 21:16:15 +04:00
_fdstream(file_descriptor, std::ios_base::out) {
_stream = new std::ostream(_filebuf);
}
2013-05-29 21:16:15 +04:00
void open(int file_descriptor) {
if (!_stream) {
_fdstream::open(file_descriptor, std::ios_base::out);
_stream = new std::ostream(_filebuf);
}
}
2013-05-29 21:16:15 +04:00
ofdstream& operator<< (const std::string& str) {
if (_stream->good())
(*_stream) << str;
_stream->flush();
return *this;
}
2013-05-29 21:16:15 +04:00
~ofdstream() {
//this->~_fdstream();
delete _stream;
}
private:
std::ostream* _stream;
};
#else
#error "Not supported"
#endif
}
#endif // _FDSTREAM_