ofile_w32.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Cross Platform Functionality
00011 *****************************************************************************/
00012 
00013 #include <ecl/config/ecl.hpp>
00014 #if defined(ECL_IS_WIN32)
00015 
00016 /*****************************************************************************
00017 ** Includes
00018 *****************************************************************************/
00019 
00020 #include "../../include/ecl/devices/ofile_w32.hpp"
00021 #include "../../include/ecl/devices/detail/error_handler.hpp"
00022 #include <iostream>
00023 #include <ecl/exceptions/macros.hpp>
00024 #include <io.h>
00025 
00026 /*****************************************************************************
00027 ** Namespaces
00028 *****************************************************************************/
00029 
00030 namespace ecl {
00031 
00032 /*****************************************************************************
00033 ** Implementation [OFile]
00034 *****************************************************************************/
00035 
00036 OFile::OFile() :
00037         file(NULL),
00038         error_handler(NoError)
00039 {}
00040 
00041 OFile::OFile(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) :
00042         file(NULL),
00043         error_handler(NoError)
00044 {
00045         ecl_try {
00046                 open(file_name,write_mode);
00047         } ecl_catch( StandardException &e ) {
00048                 ecl_throw(StandardException(LOC,e));
00049         }
00050 }
00051 
00052 OFile::~OFile() {
00053         if ( open() ) {
00054                 // This flushes and closes the file descriptor.
00055                 if ( fclose(file) != 0 ) {
00056                         // implement some mechanism if ever needed, but no
00057                         // exceptions allowed in destructors, just have to assume the best.
00058                 }
00059                 file = NULL;
00060         }
00061 }
00062 /*****************************************************************************
00063 ** Implementation [OFile][open/close]
00064 *****************************************************************************/
00065 
00066 bool OFile::open(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) {
00067         name = file_name;
00068     switch(write_mode) {
00069         case(New) : {
00070                 if (_sopen_s(&file_descriptor, name.c_str(), _O_WRONLY | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE)) {
00071                 ecl_throw(devices::open_exception(LOC,file_name));
00072                 error_handler = devices::open_error();
00073                 return false;
00074             }
00075             file = _fdopen(file_descriptor, "w");
00076             break;
00077         }
00078         case(Append) : {
00079                 if (_sopen_s(&file_descriptor, name.c_str(), _O_WRONLY | _O_APPEND | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE)) {
00080                 ecl_throw(devices::open_exception(LOC,file_name));
00081                 error_handler = devices::open_error();
00082                 return false;
00083             }
00084             file = _fdopen(file_descriptor, "a");
00085             break;
00086         }
00087         default : break;
00088     }
00089     if ( file == NULL ) {
00090         ecl_throw(devices::open_exception(LOC,file_name));
00091         error_handler = devices::open_error();
00092         return false;
00093     }
00094         error_handler = NoError;
00095     return true;
00096 }
00097 
00098 bool OFile::close() ecl_throw_decl(StandardException) {
00099         if ( open() ) {
00100                 // This flushes and closes the file descriptor.
00101                 if ( fclose(file) != 0 ) {
00102                         ecl_throw(devices::close_exception(LOC,name));
00103                         error_handler = devices::close_error();
00104                         return false;
00105                 }
00106                 file = NULL;
00107         }
00108         error_handler = NoError;
00109         return true;
00110 }
00111 /*****************************************************************************
00112 ** Implementation [OFile][write]
00113 *****************************************************************************/
00114 
00115 long OFile::write(const char &c) ecl_debug_throw_decl(StandardException)
00116 {
00117         if ( !open() ) {
00118                 ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
00119                 error_handler = OpenError;
00120         return -1;
00121         }
00122     size_t written = fwrite(&c,1,1,file);
00123     if ( written <= 0 ) {
00124         ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
00125         error_handler = WriteError;
00126         return -1;
00127     }
00128         error_handler = NoError;
00129     // fwrite returns the number of 'items' written, not bytes!
00130     return written;
00131 }
00132 
00133 long OFile::write(const char* s, unsigned long n) ecl_debug_throw_decl(StandardException)
00134 {
00135         if ( !open() ) {
00136                 ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
00137                 error_handler = OpenError;
00138         return -1;
00139         }
00140         size_t written = fwrite(s,n,1,file);
00141     if ( written <= 0 ) {
00142         ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
00143         error_handler = WriteError;
00144         return -1;
00145     }
00146         error_handler = NoError;
00147     // fwrite returns the number of 'items' written, not bytes!
00148     return n*written;
00149 }
00150 
00151 bool OFile::flush() ecl_debug_throw_decl(StandardException) {
00152         // This flushes userland buffers to the kernel buffers...not sure why,
00153         // but you can still read the file in real time, so its good enough and
00154         // thus better than a more expensive fsync here.
00155         int result = fflush(file);
00156         if ( result != 0 ) {
00157                 ecl_debug_throw ( StandardException(LOC, UnknownError, std::string("Could not fflush ") + name + std::string(".")));
00158                 error_handler = UnknownError;
00159                 return false;
00160         }
00161         error_handler = NoError;
00162         return true;
00163 }
00164 
00165 }; // namespace ecl
00166 
00167 #endif /* ECL_IS_WIN32 */


ecl_devices
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:18:03