[4] | 1 | /**
|
---|
| 2 | * @file file.h
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief file
|
---|
| 5 | */
|
---|
| 6 | #ifndef __file_h__
|
---|
| 7 | #define __file_h__
|
---|
| 8 |
|
---|
| 9 | #include "cf/exception.h"
|
---|
| 10 | #include "cf/bin.h"
|
---|
| 11 |
|
---|
| 12 | #include <string>
|
---|
| 13 | #include <list>
|
---|
| 14 |
|
---|
| 15 | namespace cf
|
---|
| 16 | {
|
---|
| 17 | /**
|
---|
| 18 | * file
|
---|
| 19 | */
|
---|
| 20 | class file
|
---|
| 21 | {
|
---|
| 22 | public:
|
---|
| 23 | /** file flag */
|
---|
| 24 | enum FILE_FLAG
|
---|
| 25 | {
|
---|
| 26 | READ = 0x00000001, /**< read-only */
|
---|
| 27 | WRITE = 0x00000002, /**< write-only */
|
---|
| 28 | RW = 0x00000004, /**< read & write */
|
---|
| 29 | CREATE = 0x00000008, /**< create */
|
---|
| 30 | TRUNC = 0x00000010, /**< truncate */
|
---|
| 31 | APPEND = 0x00000020, /**< append */
|
---|
| 32 | EXCL = 0x00000040, /**< exclusive */
|
---|
| 33 | LOCK = 0x00010000 /**< lock */
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | /** whence for seeking */
|
---|
| 37 | enum FILE_WHENCE
|
---|
| 38 | {
|
---|
| 39 | BEGIN, /**< from begin */
|
---|
| 40 | CURRENT, /**< from current position */
|
---|
| 41 | END /**< from end */
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | /** type of dir/file entry */
|
---|
| 45 | enum ENTRY_TYPE
|
---|
| 46 | {
|
---|
| 47 | UNDEFINED, /**< undefined in file::ENTRY_TYPE */
|
---|
| 48 | DIRECTORY, /**< directory */
|
---|
| 49 | REGULAR_FILE /**< regular file */
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | /** dir/file entry */
|
---|
| 53 | typedef struct ENTRY
|
---|
| 54 | {
|
---|
| 55 | std::string mName; /**< name */
|
---|
| 56 | ENTRY_TYPE mType; /**< @see ENTRY_TYPE */
|
---|
| 57 | } ENTRY;
|
---|
| 58 |
|
---|
| 59 | typedef std::list<file::ENTRY> EntryList;
|
---|
| 60 |
|
---|
| 61 | private:
|
---|
| 62 | cf::int32_t mFD;
|
---|
| 63 | std::string mPath;
|
---|
| 64 | cf::bool_t mIsLocked;
|
---|
| 65 |
|
---|
| 66 | public:
|
---|
| 67 | /**
|
---|
| 68 | * constructor
|
---|
| 69 | * @param path file path
|
---|
| 70 | * @throw cf::exception
|
---|
| 71 | */
|
---|
| 72 | file(const cf::char_t * path)
|
---|
| 73 | throw (cf::exception);
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * destructor
|
---|
| 77 | */
|
---|
| 78 | ~file();
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * get path delimiter of system
|
---|
| 82 | * @return delimiter
|
---|
| 83 | * @remarks on windows, "\\"; on *nix, "/"
|
---|
| 84 | */
|
---|
| 85 | static const cf::char_t * getDelimiter();
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * open
|
---|
| 89 | * @param flag flag
|
---|
| 90 | * @throw cf::exception
|
---|
| 91 | */
|
---|
| 92 | cf::void_t open(const cf::int32_t flag)
|
---|
| 93 | throw (cf::exception);
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * close
|
---|
| 97 | * @throw cf::exception
|
---|
| 98 | * @remarks if lock-file was not removed, an exception is thrown
|
---|
| 99 | */
|
---|
| 100 | cf::void_t close()
|
---|
| 101 | throw (cf::exception);
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * is opened ?
|
---|
| 105 | * @return true; false
|
---|
| 106 | */
|
---|
| 107 | cf::bool_t isOpened();
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * set position
|
---|
| 111 | * @return offset
|
---|
| 112 | * @param offset offset
|
---|
| 113 | * @param whence whence
|
---|
| 114 | * @throw cf::exception
|
---|
| 115 | */
|
---|
| 116 | cf::int32_t seek(const cf::int32_t offset,
|
---|
| 117 | const cf::int32_t whence = cf::file::BEGIN)
|
---|
| 118 | throw (cf::exception);
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * get current file position
|
---|
| 122 | * @return get position
|
---|
| 123 | * @throw cf::exception
|
---|
| 124 | */
|
---|
| 125 | cf::int32_t tell()
|
---|
| 126 | throw (cf::exception);
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * get size
|
---|
| 130 | * @return file size
|
---|
| 131 | * @throw cf::exception
|
---|
| 132 | */
|
---|
| 133 | cf::int32_t size()
|
---|
| 134 | throw (cf::exception);
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * exists ?
|
---|
| 138 | * @return true; false
|
---|
| 139 | */
|
---|
| 140 | cf::bool_t exists() const;
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
| 143 | * read
|
---|
| 144 | * @param out a buffer, allocated ahead
|
---|
| 145 | * @throw cf::exception
|
---|
| 146 | */
|
---|
| 147 | cf::void_t read(bin & out)
|
---|
| 148 | throw (cf::exception);
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * read
|
---|
| 152 | * @return RawStub
|
---|
| 153 | * @param len read bytes
|
---|
| 154 | * @throw cf::exception
|
---|
| 155 | */
|
---|
| 156 | bin read(const cf::int32_t len = 0)
|
---|
| 157 | throw (cf::exception);
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
| 160 | * write
|
---|
| 161 | * @param in RawStub instance
|
---|
| 162 | * @throw cf::exception
|
---|
| 163 | */
|
---|
| 164 | cf::void_t write(const bin & in)
|
---|
| 165 | throw (cf::exception);
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * remove file
|
---|
| 169 | * @throw cf::exception
|
---|
| 170 | */
|
---|
| 171 | cf::void_t remove()
|
---|
| 172 | throw (cf::exception);
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * rename file
|
---|
| 176 | * @param path new path
|
---|
| 177 | * @throw cf::exception
|
---|
| 178 | */
|
---|
| 179 | cf::void_t rename(const cf::char_t * path)
|
---|
| 180 | throw (cf::exception);
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * get path
|
---|
| 184 | * @return path
|
---|
| 185 | */
|
---|
| 186 | const cf::char_t * getPath() const;
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * is locked ?
|
---|
| 190 | * @return true; false
|
---|
| 191 | */
|
---|
| 192 | cf::bool_t isLocked() const;
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * make directory
|
---|
| 196 | * @param path directory path
|
---|
| 197 | * @throw cf::exception
|
---|
| 198 | */
|
---|
| 199 | static cf::void_t makedir(const cf::char_t * path)
|
---|
| 200 | throw (cf::exception);
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * get directory entry-list
|
---|
| 204 | * @param path directory entry-list
|
---|
| 205 | * @param exceptDots except . and .. from list ? (as default, true)
|
---|
| 206 | * @throw cf::exception
|
---|
| 207 | * @see EntryList, ENTRY, ENTRY_TYPE
|
---|
| 208 | */
|
---|
| 209 | static EntryList getEntryList(const cf::char_t * path,
|
---|
| 210 | const cf::bool_t exceptDots = true)
|
---|
| 211 | throw (cf::exception);
|
---|
| 212 | };
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | #endif // #ifndef __file_h__
|
---|