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) ecl_throw_decl(StandardException);
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) ecl_throw_decl(StandardException);
167 
185  virtual bool close() ecl_throw_decl(StandardException);
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) ecl_debug_throw_decl(StandardException);
244  long write(const char* s, unsigned long n) ecl_debug_throw_decl(StandardException);
245 
273  template <typename ByteArray>
274  long write(const ByteArray &byte_array) ecl_debug_throw_decl(StandardException);
286  virtual bool flush() ecl_debug_throw_decl(StandardException);
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) ecl_debug_throw_decl(StandardException) {
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_ */
Embedded control libraries.
WriteMode
Write mode for devices.
Definition: modes.hpp:35
WriteError
Bool< true > True
Device modes.
Array< char, 3 > ByteArray
#define ecl_compile_time_concept_check(Model)
#define ecl_debug_throw_decl(exception)
#define ecl_debug_throw(exception)
Defines exception handlers for posix devices.
Opens a new object (deletes existing objects).
Definition: modes.hpp:36


ecl_devices
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:45