ofile_pos.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_DEVICES_OFILE_POS_HPP_
13 #define ECL_DEVICES_OFILE_POS_HPP_
14 
15 /*****************************************************************************
16 ** Cross Platform Functionality
17 *****************************************************************************/
18 
19 #include <ecl/config/ecl.hpp>
20 #if defined(ECL_IS_POSIX)
21 
22 /*****************************************************************************
23 ** Includes
24 *****************************************************************************/
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <cstdio> // FILE calls
31 #include <string>
36 #include "modes.hpp"
37 #include "traits.hpp"
38 
39 /*****************************************************************************
40 ** Namespaces
41 *****************************************************************************/
42 
43 namespace ecl {
44 
45 /*****************************************************************************
46 ** Interface [Output File]
47 *****************************************************************************/
97 class OFile {
98 public:
99  /*********************
100  ** C&D
101  **********************/
109  OFile();
122  OFile(const std::string &file_name, const WriteMode &mode = New);
123 
127  virtual ~OFile();
128 
129  /*********************
130  ** Open/Close
131  **********************/
137  virtual bool open() { return ( file != NULL ) ? true : false; }
138 
166  virtual bool open(const std::string &file_name, const WriteMode &mode = New);
167 
185  virtual bool close();
186 
187  /*********************
188  ** Utility Methods
189  **********************/
194  virtual const std::string& filename() const { return name; }
195 
196 
197  /*********************
198  ** Output Methods
199  **********************/
221  virtual long write(const char &c);
244  long write(const char* s, unsigned long n);
245 
273  template <typename ByteArray>
274  long write(const ByteArray &byte_array);
286  virtual bool flush();
287 
291  const Error& error() const { return error_handler; }
292 
293 private:
294  /*********************
295  ** Variables
296  **********************/
297  int file_descriptor;
298  FILE *file;
299  std::string name;
300  Error error_handler;
301 };
302 
303 /*****************************************************************************
304 ** Template Implementation
305 *****************************************************************************/
306 
307 template <typename ByteArray>
308 long OFile::write(const ByteArray&byte_array) {
309 
311  if ( !open() ) {
312  ecl_debug_throw(ecl::StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
313  error_handler = OpenError;
314  return -1;
315  }
316  size_t written = fwrite(&byte_array[0],byte_array.size(),1,file);
317  if ( written == 0) {
318  ecl_debug_throw(ecl::StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
319  error_handler = WriteError;
320  return -1;
321  }
322  error_handler = NoError;
323  return byte_array.size()*written; // fwrite returns the number of 'items' written, not bytes!
324 }
325 
326 
327 /*****************************************************************************
328 ** Traits [OFile]
329 *****************************************************************************/
330 
336 template <>
337 class is_sink<OFile> : public True {};
338 
339 } // namespace ecl
340 
341 
342 #endif /* ECL_IS_POSIX */
343 #endif /* ECL_DEVICES_OFILE_POS_HPP_ */
ecl::OpenError
OpenError
ecl::WriteError
WriteError
ecl::WriteMode
WriteMode
Write mode for devices.
Definition: modes.hpp:41
ecl_compile_time_concept_check
#define ecl_compile_time_concept_check(Model)
ecl::ByteContainerConcept
ecl::New
@ New
Opens a new object (deletes existing objects).
Definition: modes.hpp:50
modes.hpp
Device modes.
ecl::StandardException
ecl::NoError
NoError
True
Bool< true > True
standard_exception.hpp
exception_handler_pos.hpp
Defines exception handlers for posix devices.
containers.hpp
macros.hpp
ByteArray
Array< char, 3 > ByteArray
ecl
Embedded control libraries.
ecl_debug_throw
#define ecl_debug_throw(exception)


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45