libscio/fdiobuf.cpp
2020-04-24 17:45:33 +02:00

51 lines
1.1 KiB
C++

//
// fdiobuf.cpp
// libscio
//
// Created by Bob Polis at 2020-02-14
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif
std::streambuf::int_type sc::io::fdiobuf::underflow() {
int num = ::read(_fd, &_ch, 1);
if (num <= 0) {
return EOF;
}
setg(&_ch, &_ch, &_ch + 1);
return _ch;
}
std::streamsize sc::io::fdiobuf::xsgetn(char* s, std::streamsize n) {
return ::read(_fd, s, n);
}
std::streambuf::int_type sc::io::fdiobuf::overflow(std::streambuf::int_type c) {
if (c != EOF) {
int res;
if ((res = ::write(_fd, &c, 1)) != 1) {
return EOF;
}
else {
std::cerr << "write result = " << res << ", errno = " << errno << '\n';
}
}
return c;
}
std::streamsize sc::io::fdiobuf::xsputn(const char* s, std::streamsize num) {
int res = ::write(_fd, s, num);
if (res != num) {
std::cerr << "write result = " << res << ", errno = " << errno << '\n';
return EOF;
}
return res;
}