ofile_w32.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Cross Platform Functionality
11 *****************************************************************************/
12 
13 #include <ecl/config/ecl.hpp>
14 #if defined(ECL_IS_WIN32)
15 
16 /*****************************************************************************
17 ** Includes
18 *****************************************************************************/
19 
20 #include "../../include/ecl/devices/ofile_w32.hpp"
21 #include "../../include/ecl/devices/detail/error_handler.hpp"
22 #include <iostream>
24 #include <io.h>
25 
26 /*****************************************************************************
27 ** Namespaces
28 *****************************************************************************/
29 
30 namespace ecl {
31 
32 /*****************************************************************************
33 ** Implementation [OFile]
34 *****************************************************************************/
35 
36 OFile::OFile() :
37  file(NULL),
38  error_handler(NoError)
39 {}
40 
41 OFile::OFile(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) :
42  file(NULL),
43  error_handler(NoError)
44 {
45  ecl_try {
46  open(file_name,write_mode);
47  } ecl_catch( StandardException &e ) {
48  ecl_throw(StandardException(LOC,e));
49  }
50 }
51 
52 OFile::~OFile() {
53  if ( open() ) {
54  // This flushes and closes the file descriptor.
55  if ( fclose(file) != 0 ) {
56  // implement some mechanism if ever needed, but no
57  // exceptions allowed in destructors, just have to assume the best.
58  }
59  file = NULL;
60  }
61 }
62 /*****************************************************************************
63 ** Implementation [OFile][open/close]
64 *****************************************************************************/
65 
66 bool OFile::open(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) {
67  name = file_name;
68  switch(write_mode) {
69  case(New) : {
70  if (_sopen_s(&file_descriptor, name.c_str(), _O_WRONLY | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE)) {
71  ecl_throw(devices::open_exception(LOC,file_name));
72  error_handler = devices::open_error();
73  return false;
74  }
75  file = _fdopen(file_descriptor, "w");
76  break;
77  }
78  case(Append) : {
79  if (_sopen_s(&file_descriptor, name.c_str(), _O_WRONLY | _O_APPEND | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE)) {
80  ecl_throw(devices::open_exception(LOC,file_name));
81  error_handler = devices::open_error();
82  return false;
83  }
84  file = _fdopen(file_descriptor, "a");
85  break;
86  }
87  default : break;
88  }
89  if ( file == NULL ) {
90  ecl_throw(devices::open_exception(LOC,file_name));
91  error_handler = devices::open_error();
92  return false;
93  }
94  error_handler = NoError;
95  return true;
96 }
97 
98 bool OFile::close() ecl_throw_decl(StandardException) {
99  if ( open() ) {
100  // This flushes and closes the file descriptor.
101  if ( fclose(file) != 0 ) {
103  error_handler = devices::close_error();
104  return false;
105  }
106  file = NULL;
107  }
108  error_handler = NoError;
109  return true;
110 }
111 /*****************************************************************************
112 ** Implementation [OFile][write]
113 *****************************************************************************/
114 
115 long OFile::write(const char &c) ecl_debug_throw_decl(StandardException)
116 {
117  if ( !open() ) {
118  ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
119  error_handler = OpenError;
120  return -1;
121  }
122  size_t written = fwrite(&c,1,1,file);
123  if ( written <= 0 ) {
124  ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
125  error_handler = WriteError;
126  return -1;
127  }
128  error_handler = NoError;
129  // fwrite returns the number of 'items' written, not bytes!
130  return written;
131 }
132 
133 long OFile::write(const char* s, unsigned long n) ecl_debug_throw_decl(StandardException)
134 {
135  if ( !open() ) {
136  ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
137  error_handler = OpenError;
138  return -1;
139  }
140  size_t written = fwrite(s,n,1,file);
141  if ( written <= 0 ) {
142  ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
143  error_handler = WriteError;
144  return -1;
145  }
146  error_handler = NoError;
147  // fwrite returns the number of 'items' written, not bytes!
148  return n*written;
149 }
150 
151 bool OFile::flush() ecl_debug_throw_decl(StandardException) {
152  // This flushes userland buffers to the kernel buffers...not sure why,
153  // but you can still read the file in real time, so its good enough and
154  // thus better than a more expensive fsync here.
155  int result = fflush(file);
156  if ( result != 0 ) {
157  ecl_debug_throw ( StandardException(LOC, UnknownError, std::string("Could not fflush ") + name + std::string(".")));
158  error_handler = UnknownError;
159  return false;
160  }
161  error_handler = NoError;
162  return true;
163 }
164 
165 }; // namespace ecl
166 
167 #endif /* ECL_IS_WIN32 */
Embedded control libraries.
WriteMode
Write mode for devices.
Definition: modes.hpp:35
#define ecl_throw(exception)
#define ecl_throw_decl(exception)
UnknownError
StandardException open_exception(const char *loc, const std::string &file_name)
WriteError
ecl::Error open_error()
ecl::Error close_error()
StandardException close_exception(const char *loc, const std::string &file_name)
#define ecl_debug_throw_decl(exception)
#define ecl_debug_throw(exception)
#define ecl_catch(exception)
Opens a new object (deletes existing objects).
Definition: modes.hpp:36
Appends to an existing object (opens if not existing).
Definition: modes.hpp:37
#define ecl_try


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