ofile_pos.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_POSIX)
15 
16 /*****************************************************************************
17 ** Includes
18 *****************************************************************************/
19 
20 #include "../../include/ecl/devices/ofile_pos.hpp"
21 #include "../../include/ecl/devices/detail/error_handler.hpp"
22 #include <iostream>
24 
25 /*****************************************************************************
26 ** Namespaces
27 *****************************************************************************/
28 
29 namespace ecl {
30 
31 /*****************************************************************************
32 ** Implementation [OFile]
33 *****************************************************************************/
34 
35 OFile::OFile() :
36  file(NULL),
37  error_handler(NoError)
38 {}
39 
40 OFile::OFile(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) :
41  file(NULL),
42  error_handler(NoError)
43 {
44  ecl_try {
45  open(file_name,write_mode);
46  } ecl_catch( StandardException &e ) {
47  ecl_throw(StandardException(LOC,e));
48  }
49 }
50 
51 OFile::~OFile() {
52  if ( open() ) {
53  // This flushes and closes the file descriptor.
54  if ( fclose(file) != 0 ) {
55  // implement some mechanism if ever needed, but no
56  // exceptions allowed in destructors, just have to assume the best.
57  }
58  file = NULL;
59  }
60 }
61 /*****************************************************************************
62 ** Implementation [OFile][open/close]
63 *****************************************************************************/
64 
65 bool OFile::open(const std::string &file_name, const WriteMode &write_mode) ecl_throw_decl(StandardException) {
66  name = file_name;
67  switch(write_mode) {
68  case(New) : {
69  file_descriptor = ::open(name.c_str(), O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
70  if ( file_descriptor == -1 ) {
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  file_descriptor = ::open(name.c_str(),O_WRONLY|O_APPEND|O_CREAT, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
80  if ( file_descriptor == -1 ) {
81  ecl_throw(devices::open_exception(LOC,file_name));
82  error_handler = devices::open_error();
83  return false;
84  }
85  file = fdopen(file_descriptor,"a");
86  break;
87  }
88  default : break;
89  }
90  if ( file == NULL ) {
91  ecl_throw(devices::open_exception(LOC,file_name));
92  error_handler = devices::open_error();
93  return false;
94  }
95  error_handler = NoError;
96  return true;
97 }
98 
99 bool OFile::close() ecl_throw_decl(StandardException) {
100  if ( open() ) {
101  // This flushes and closes the file descriptor.
102  if ( fclose(file) != 0 ) {
104  error_handler = devices::close_error();
105  return false;
106  }
107  file = NULL;
108  }
109  error_handler = NoError;
110  return true;
111 }
112 /*****************************************************************************
113 ** Implementation [OFile][write]
114 *****************************************************************************/
115 
116 long OFile::write(const char &c) ecl_debug_throw_decl(StandardException)
117 {
118  if ( !open() ) {
119  ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
120  error_handler = OpenError;
121  return -1;
122  }
123  size_t written = fwrite(&c,1,1,file);
124  if ( written <= 0 ) {
125  ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
126  error_handler = WriteError;
127  return -1;
128  }
129  error_handler = NoError;
130  // fwrite returns the number of 'items' written, not bytes!
131  return written;
132 }
133 
134 long OFile::write(const char* s, unsigned long n) ecl_debug_throw_decl(StandardException)
135 {
136  if ( !open() ) {
137  ecl_debug_throw(StandardException(LOC, OpenError, std::string("File ") + name + std::string(" is not open for writing.")));
138  error_handler = OpenError;
139  return -1;
140  }
141  size_t written = fwrite(s,n,1,file);
142  if ( written <= 0 ) {
143  ecl_debug_throw(StandardException(LOC, WriteError, std::string("Could not write to ") + name + std::string(".")));
144  error_handler = WriteError;
145  return -1;
146  }
147  error_handler = NoError;
148  // fwrite returns the number of 'items' written, not bytes!
149  return n*written;
150 }
151 
152 bool OFile::flush() ecl_debug_throw_decl(StandardException) {
153  // This flushes userland buffers to the kernel buffers...not sure why,
154  // but you can still read the file in real time, so its good enough and
155  // thus better than a more expensive fsync here.
156  int result = fflush(file);
157  if ( result != 0 ) {
158  ecl_debug_throw ( StandardException(LOC, UnknownError, std::string("Could not fflush ") + name + std::string(".")));
159  error_handler = UnknownError;
160  return false;
161  }
162  error_handler = NoError;
163  return true;
164 }
165 
166 }; // namespace ecl
167 
168 #endif /* ECL_IS_POSIX */
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