ofile_w32.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_DEVICES_OFILE_W32_HPP_
13 #define ECL_DEVICES_OFILE_W32_HPP_
14 
15 /*****************************************************************************
16 ** Cross Platform Functionality
17 *****************************************************************************/
18 
19 #include <ecl/config/ecl.hpp>
20 #if defined(ECL_IS_WIN32)
21 
22 /*****************************************************************************
23 ** Includes
24 *****************************************************************************/
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <cstdio> // FILE calls
30 #include <string>
31 #include <ecl/concepts/containers.hpp>
32 #include <ecl/exceptions/macros.hpp>
33 #include <ecl/exceptions/standard_exception.hpp>
34 #include "detail/exception_handler_pos.hpp"
35 #include "modes.hpp"
36 #include "traits.hpp"
37 #include "macros.hpp"
38 
39 /*****************************************************************************
40 ** Namespaces
41 *****************************************************************************/
42 
43 namespace ecl {
44 
45 /*****************************************************************************
46 ** Interface [Output File]
47 *****************************************************************************/
97 class ecl_devices_PUBLIC 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_WIN32 */
343 #endif /* ECL_DEVICES_OFILE_W32_HPP_ */
WriteMode
#define ecl_throw_decl(exception)
Standard ecl throw exception declaration.
#define ecl_devices_PUBLIC
#define LOC
Stringify the line of code you are at.
Defines validating functionality for the byte container concept.
Error
Standard exception type, provides code location and error string.
WriteError
Bool< true > True
Array< char, 3 > ByteArray
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
#define ecl_debug_throw_decl(exception)
Debug mode throw exception declaration.
#define ecl_debug_throw(exception)
Debug mode exception throw.


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13