lxfile.hpp
00001 #if !defined(__SYSTEM_L4_LXFILE_HPP__)
00002 #define __SYSTEM_L4_LXFILE_HPP__
00003
00004
00005
00006
00007 #include <l4/lxfuxlibc/lxfuxlc.h>
00008 #include <l4/env/errno.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
00022 struct lxfile : public file, private noncopyable
00023 {
00024 static const uint32_t MAX_IO_BLOCK_SIZE = 0;
00025
00026 protected:
00027 std::string filename;
00028 bool readonly;
00029 LX_FILE *lx_file;
00030 offset_t lx_size;
00031
00032 public:
00033
00034
00035
00036 inline lxfile(void)
00037 : lx_file(nullptr), lx_size(0)
00038 {}
00039
00040 inline lxfile(const std::string &filename, const bool readonly=false)
00041 : filename(filename), readonly(readonly), lx_file(nullptr), lx_size(0)
00042 {
00043 open(filename, readonly);
00044 }
00045
00046 virtual inline ~lxfile(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() ? filename.c_str() : nullptr;
00063 }
00064
00065 virtual inline bool is_open(void) const
00066 {
00067 return lx_file != 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() ? lx_size : -1;
00078 }
00079
00080
00081
00082
00083 virtual offset_t read(uint8_t *buffer, offset_t offset, offset_t number);
00084 virtual offset_t write(uint8_t *buffer, offset_t offset, offset_t number);
00085
00086 virtual inline int flush(void)
00087 {
00088 if (!is_open()) return -L4_EOPEN;
00089 return lx_fflush(lx_file) ? -L4_EIO : 0;
00090 }
00091
00092 private:
00093 inline bool exists(void)
00094 {
00095 LX_FILE *f;
00096 if ((f=lx_fopen(filename.c_str(), "r")) == nullptr) return false;
00097 lx_fclose(f);
00098 return true;
00099 }
00100 };
00101
00102 #endif
00103
00104
00105