Fix some new compile problems.

* file_piece.cc used isnan() instead of std::isnan().
 * Fdstream.h used close() but Windows doesn't have unistd.h.

Fixed Fdstream.h by using util::scoped_fd.  Thanks Ken.
This commit is contained in:
Jeroen Vermeulen 2015-05-20 11:40:11 +07:00
parent a70d37e46f
commit ef5a17b2f9
2 changed files with 21 additions and 11 deletions

View File

@ -16,6 +16,8 @@
#if defined(__GLIBCXX__) || defined(__GLIBCPP__)
#include <ext/stdio_filebuf.h>
#include "util/file.hh"
#define BUFFER_SIZE (32768)
namespace MosesTuning
@ -25,13 +27,14 @@ class _fdstream
{
protected:
_fdstream() :
_file_descriptor(-1), _filebuf(NULL) {
_file_descriptor(), _filebuf(NULL) {
}
_fdstream(int file_descriptor, std::ios_base::openmode openmode) :
_file_descriptor(file_descriptor), _openmode(openmode) {
_file_descriptor(-1), _openmode(openmode) {
_filebuf = NULL;
open(file_descriptor, openmode);
_file_descriptor.reset(file_descriptor);
}
std::ios_base::openmode openmode() const {
@ -39,7 +42,9 @@ protected:
}
void open(int file_descriptor, std::ios_base::openmode openmode) {
if (!_filebuf)
// TODO: How does file_descriptor relate to the one we already have?
// Should we reset our own _file_descriptor to match it?
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
@ -48,16 +53,21 @@ protected:
_filebuf = new __gnu_cxx::stdio_filebuf<char> (file_descriptor,
openmode);
}
}
virtual ~_fdstream() {
close(_file_descriptor);
delete _filebuf;
_filebuf = NULL;
}
int _file_descriptor;
private:
util::scoped_fd _file_descriptor;
__gnu_cxx::stdio_filebuf<char>* _filebuf;
std::ios_base::openmode _openmode;
protected:
/// For child classes only: retrieve filebuf.
__gnu_cxx::stdio_filebuf<char> *get_filebuf() { return _filebuf; }
};
class ifdstream : public _fdstream
@ -69,13 +79,13 @@ public:
ifdstream(int file_descriptor) :
_fdstream(file_descriptor, std::ios_base::in) {
_stream = new std::istream(_filebuf);
_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(_filebuf);
_stream = new std::istream(get_filebuf());
}
}
@ -126,13 +136,13 @@ public:
ofdstream(int file_descriptor) :
_fdstream(file_descriptor, std::ios_base::out) {
_stream = new std::ostream(_filebuf);
_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(_filebuf);
_stream = new std::ostream(get_filebuf());
}
}

View File

@ -164,13 +164,13 @@ StringPiece FirstToken(StringPiece str) {
const char *ParseNumber(StringPiece str, float &out) {
int count;
out = kConverter.StringToFloat(str.data(), str.size(), &count);
UTIL_THROW_IF_ARG(isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "float");
UTIL_THROW_IF_ARG(std::isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "float");
return str.data() + count;
}
const char *ParseNumber(StringPiece str, double &out) {
int count;
out = kConverter.StringToDouble(str.data(), str.size(), &count);
UTIL_THROW_IF_ARG(isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "double");
UTIL_THROW_IF_ARG(std::isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "double");
return str.data() + count;
}
const char *ParseNumber(StringPiece str, long int &out) {