l4file.hpp
00001 #if !defined(__SYSTEM_L4_L4FILE_HPP__)
00002 #define __SYSTEM_L4_L4FILE_HPP__
00003
00004
00005
00006
00007 #include <l4/env/errno.h>
00008 #include <l4/env/env.h>
00009
00010
00011
00012
00013 #include "core/util/noncopyable.hpp"
00014 #include "core/util/likely.hpp"
00015 #include "core/util/c++0x.hpp"
00016 #include "core/system/file.hpp"
00017 #include "dataspace.hpp"
00018
00023 struct l4file : public file, private noncopyable
00024 {
00025 protected:
00026 struct dataspace dataspace;
00027 bool readonly;
00028
00029 uint8_t *_data;
00030 offset_t _size;
00031
00032 public:
00033
00034
00035
00036 inline l4file(void)
00037 : _data(nullptr), _size(0)
00038 {}
00039
00040 inline l4file(const std::string &filename, const bool readonly=false)
00041 : readonly(readonly), _data(nullptr), _size(0)
00042 {
00043 open(filename, readonly);
00044 }
00045
00046 virtual inline ~l4file(void)
00047 {
00048 close();
00049 }
00050
00051
00052
00053
00054 virtual int open(const std::string &filename, bool readonly=false);
00055 virtual int close(void);
00056
00057
00058
00059
00060 virtual inline const char *name(void) const
00061 {
00062 return is_open() ? dataspace.name() : nullptr;
00063 }
00064
00065 virtual inline bool is_open(void) const
00066 {
00067 return _data != nullptr;
00068 }
00069
00070 virtual inline bool is_readonly(void) const
00071 {
00072 return readonly;
00073 }
00074
00075 virtual inline offset_t size(void) const
00076 {
00077 return is_open() ? _size : -1;
00078 }
00079
00080
00081
00082
00083 virtual inline offset_t read(uint8_t *buffer, offset_t offset, offset_t number)
00084 {
00085 if (!is_open()) return -L4_EOPEN;
00086 if (offset + number > size()) return -L4_EINVAL;
00087 memcpy(buffer, data(offset), number);
00088 return number;
00089 }
00090
00091 virtual inline offset_t write(uint8_t *buffer, offset_t offset, offset_t number)
00092 {
00093 if (!is_open()) return -L4_EOPEN;
00094 if (readonly) return -L4_EROFS;
00095 if (offset + number > size()) return -L4_EINVAL;
00096 memcpy(data(offset), buffer, number);
00097 return number;
00098 }
00099
00100 virtual inline int flush(void)
00101 {
00102 if (!is_open()) return -L4_EOPEN;
00103 return 0;
00104 }
00105
00106
00107
00108
00109 inline uint8_t *data(offset_t offset=0) const
00110 {
00111 return _data + offset;
00112 }
00113
00114 protected:
00115
00116
00117
00118 static const l4env_infopage_t *get_l4env_infopage(void);
00119 };
00120
00121 #endif
00122
00123
00124