13 #include <ecl/config/ecl.hpp>
14 #if defined(ECL_IS_POSIX)
20 #include "../../include/ecl/devices/ofile_pos.hpp"
21 #include "../../include/ecl/devices/detail/error_handler.hpp"
40 OFile::OFile(
const std::string &file_name,
const WriteMode &write_mode) :
45 open(file_name,write_mode);
54 if ( fclose(file) != 0 ) {
65 bool OFile::open(
const std::string &file_name,
const WriteMode &write_mode) {
69 file_descriptor = ::open(name.c_str(), O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
70 if ( file_descriptor == -1 ) {
75 file = fdopen(file_descriptor,
"w");
79 file_descriptor = ::open(name.c_str(),O_WRONLY|O_APPEND|O_CREAT, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
80 if ( file_descriptor == -1 ) {
85 file = fdopen(file_descriptor,
"a");
102 if ( fclose(file) != 0 ) {
116 long OFile::write(
const char &c)
119 ecl_debug_throw(StandardException(LOC, OpenError, std::string(
"File ") + name + std::string(
" is not open for writing.")));
123 size_t written = fwrite(&c,1,1,file);
124 if ( written <= 0 ) {
125 ecl_debug_throw(StandardException(LOC, WriteError, std::string(
"Could not write to ") + name + std::string(
".")));
134 long OFile::write(
const char* s,
unsigned long n)
137 ecl_debug_throw(StandardException(LOC, OpenError, std::string(
"File ") + name + std::string(
" is not open for writing.")));
141 size_t written = fwrite(s,n,1,file);
142 if ( written <= 0 ) {
143 ecl_debug_throw(StandardException(LOC, WriteError, std::string(
"Could not write to ") + name + std::string(
".")));
152 bool OFile::flush() {
156 int result = fflush(file);
158 ecl_debug_throw ( StandardException(LOC, UnknownError, std::string(
"Could not fflush ") + name + std::string(
".")));