ofile_pos.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_DEVICES_OFILE_POS_HPP_
00013 #define ECL_DEVICES_OFILE_POS_HPP_
00014 
00015 /*****************************************************************************
00016 ** Cross Platform Functionality
00017 *****************************************************************************/
00018 
00019 #include <ecl/config/ecl.hpp>
00020 #if defined(ECL_IS_POSIX)
00021 
00022 /*****************************************************************************
00023 ** Includes
00024 *****************************************************************************/
00025 
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <fcntl.h>
00029 #include <unistd.h>
00030 #include <cstdio> // FILE calls
00031 #include <string>
00032 #include <ecl/concepts/containers.hpp>
00033 #include <ecl/exceptions/macros.hpp>
00034 #include <ecl/exceptions/standard_exception.hpp>
00035 #include "detail/exception_handler_pos.hpp"
00036 #include "modes.hpp"
00037 #include "traits.hpp"
00038 
00039 /*****************************************************************************
00040 ** Namespaces
00041 *****************************************************************************/
00042 
00043 namespace ecl {
00044 
00045 /*****************************************************************************
00046 ** Interface [Output File]
00047 *****************************************************************************/
00097 class OFile {
00098 public:
00099         /*********************
00100         ** C&D
00101         **********************/
00109         OFile();
00122         OFile(const std::string &file_name, const WriteMode &mode = New) ecl_throw_decl(StandardException);
00123 
00127         virtual ~OFile();
00128 
00129         /*********************
00130         ** Open/Close
00131         **********************/
00137         virtual bool open() { return ( file != NULL ) ? true : false; }
00138 
00166         virtual bool open(const std::string &file_name, const WriteMode &mode = New) ecl_throw_decl(StandardException);
00167 
00185         virtual bool close() ecl_throw_decl(StandardException);
00186 
00187         /*********************
00188         ** Utility Methods
00189         **********************/
00194         virtual const std::string& filename() const { return name; }
00195 
00196 
00197         /*********************
00198         ** Output Methods
00199         **********************/
00221         virtual long write(const char &c) ecl_debug_throw_decl(StandardException);
00244         long write(const char* s, unsigned long n) ecl_debug_throw_decl(StandardException);
00245 
00273         template <typename ByteArray>
00274         long write(const ByteArray &byte_array) ecl_debug_throw_decl(StandardException);
00286         virtual bool flush() ecl_debug_throw_decl(StandardException);
00287 
00291         const Error& error() const { return error_handler; }
00292 
00293 private:
00294         /*********************
00295         ** Variables
00296         **********************/
00297         int file_descriptor;
00298     FILE *file;
00299     std::string name;
00300     Error error_handler;
00301 };
00302 
00303 /*****************************************************************************
00304 ** Template Implementation
00305 *****************************************************************************/
00306 
00307 template <typename ByteArray>
00308 long OFile::write(const ByteArray&byte_array) ecl_debug_throw_decl(StandardException) {
00309 
00310     ecl_compile_time_concept_check(ecl::ByteContainerConcept<ByteArray>);
00311     if ( !open() ) {
00312         ecl_debug_throw(ecl::StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
00313         error_handler = OpenError;
00314         return -1;
00315     }
00316     size_t written = fwrite(&byte_array[0],byte_array.size(),1,file);
00317     if ( written == 0) {
00318         ecl_debug_throw(ecl::StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
00319         error_handler = WriteError;
00320         return -1;
00321     }
00322     error_handler = NoError;
00323     return byte_array.size()*written; // fwrite returns the number of 'items' written, not bytes!
00324 }
00325 
00326 
00327 /*****************************************************************************
00328 ** Traits [OFile]
00329 *****************************************************************************/
00330 
00336 template <>
00337 class is_sink<OFile> : public True {};
00338 
00339 } // namespace ecl
00340 
00341 
00342 #endif /* ECL_IS_POSIX */
00343 #endif /* ECL_DEVICES_OFILE_POS_HPP_ */


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:37