Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 1 | /* $Id$ */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 2 | /*
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 4 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify
|
| 6 | * it under the terms of the GNU General Public License as published by
|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
| 8 | * (at your option) any later version.
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 9 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful,
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 | * GNU General Public License for more details.
|
| 14 | *
|
| 15 | * You should have received a copy of the GNU General Public License
|
| 16 | * along with this program; if not, write to the Free Software
|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 18 | */
|
| 19 | #ifndef __PJPP_FILE_HPP__
|
| 20 | #define __PJPP_FILE_HPP__
|
| 21 |
|
| 22 | #include <pj/file_io.h>
|
| 23 | #include <pj/file_access.h>
|
| 24 | #include <pj++/types.hpp>
|
| 25 | #include <pj++/pool.hpp>
|
| 26 |
|
| 27 | //
|
| 28 | // File API.
|
| 29 | //
|
| 30 | class Pj_File_API
|
| 31 | {
|
| 32 | public:
|
| 33 | //
|
| 34 | // Check file existance.
|
| 35 | //
|
| 36 | static bool file_exists(const char *filename)
|
| 37 | {
|
| 38 | return pj_file_exists(filename) != 0;
|
| 39 | }
|
| 40 |
|
| 41 | //
|
| 42 | // Get file size.
|
| 43 | //
|
| 44 | static pj_off_t file_size(const char *filename)
|
| 45 | {
|
| 46 | return pj_file_size(filename);
|
| 47 | }
|
| 48 |
|
| 49 | //
|
| 50 | // Delete file.
|
| 51 | //
|
| 52 | static pj_status_t file_delete(const char *filename)
|
| 53 | {
|
| 54 | return pj_file_delete(filename);
|
| 55 | }
|
| 56 |
|
| 57 | //
|
| 58 | // Move/rename file.
|
| 59 | //
|
| 60 | static pj_status_t file_move(const char *oldname, const char *newname)
|
| 61 | {
|
| 62 | return pj_file_move(oldname, newname);
|
| 63 | }
|
| 64 |
|
| 65 | //
|
| 66 | // Get stat.
|
| 67 | //
|
| 68 | static pj_status_t file_stat(const char *filename, pj_file_stat *buf)
|
| 69 | {
|
| 70 | return pj_file_getstat(filename, buf);
|
| 71 | }
|
| 72 | };
|
| 73 |
|
| 74 |
|
| 75 | //
|
| 76 | // File.
|
| 77 | //
|
| 78 | class Pj_File : public Pj_Object
|
| 79 | {
|
| 80 | public:
|
| 81 | //
|
| 82 | // Offset type to be used in setpos.
|
| 83 | //
|
| 84 | enum Offset_Type
|
| 85 | {
|
| 86 | SEEK_SET = PJ_SEEK_SET,
|
| 87 | SEEK_CUR = PJ_SEEK_CUR,
|
| 88 | SEEK_END = PJ_SEEK_END,
|
| 89 | };
|
| 90 |
|
| 91 | //
|
| 92 | // Default constructor.
|
| 93 | //
|
| 94 | Pj_File()
|
| 95 | : hnd_(0)
|
| 96 | {
|
| 97 | }
|
| 98 |
|
| 99 | //
|
| 100 | // Construct and open a file.
|
| 101 | //
|
| 102 | Pj_File(Pj_Pool *pool, const char *filename,
|
| 103 | unsigned access = PJ_O_RDONLY)
|
| 104 | : hnd_(NULL)
|
| 105 | {
|
| 106 | open(pool, filename, access);
|
| 107 | }
|
| 108 |
|
| 109 | //
|
| 110 | // Destructor closes the file.
|
| 111 | //
|
| 112 | ~Pj_File()
|
| 113 | {
|
| 114 | close();
|
| 115 | }
|
| 116 |
|
| 117 | //
|
| 118 | // Open a file.
|
| 119 | //
|
| 120 | pj_status_t open(Pj_Pool *pool, const char *filename,
|
| 121 | unsigned access = PJ_O_RDONLY )
|
| 122 | {
|
| 123 | close();
|
| 124 | return pj_file_open(pool->pool_(), filename, access, &hnd_);
|
| 125 | }
|
| 126 |
|
| 127 | //
|
| 128 | // Close a file.
|
| 129 | //
|
| 130 | void close()
|
| 131 | {
|
| 132 | if (hnd_ != 0) {
|
| 133 | pj_file_close(hnd_);
|
| 134 | hnd_ = 0;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | //
|
| 139 | // Write data.
|
| 140 | //
|
| 141 | pj_ssize_t write(const void *buff, pj_size_t size)
|
| 142 | {
|
| 143 | pj_ssize_t bytes = size;
|
| 144 | if (pj_file_write(hnd_, buff, &bytes) != PJ_SUCCESS)
|
| 145 | return -1;
|
| 146 | return bytes;
|
| 147 | }
|
| 148 |
|
| 149 | //
|
| 150 | // Read data.
|
| 151 | //
|
| 152 | pj_ssize_t read(void *buf, pj_size_t size)
|
| 153 | {
|
| 154 | pj_ssize_t bytes = size;
|
| 155 | if (pj_file_read(hnd_, buf, &bytes) != PJ_SUCCESS)
|
| 156 | return -1;
|
| 157 | return bytes;
|
| 158 | }
|
| 159 |
|
| 160 | //
|
| 161 | // Set file position.
|
| 162 | //
|
| 163 | pj_status_t setpos(pj_off_t offset, Offset_Type whence)
|
| 164 | {
|
| 165 | return pj_file_setpos(hnd_, offset,
|
| 166 | (enum pj_file_seek_type)whence);
|
| 167 | }
|
| 168 |
|
| 169 | //
|
| 170 | // Get file position.
|
| 171 | //
|
| 172 | pj_off_t getpos()
|
| 173 | {
|
| 174 | pj_off_t pos;
|
| 175 | if (pj_file_getpos(hnd_, &pos) != PJ_SUCCESS)
|
| 176 | return -1;
|
| 177 | return pos;
|
| 178 | }
|
| 179 |
|
| 180 | private:
|
| 181 | pj_oshandle_t hnd_;
|
| 182 | };
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 | #endif /* __PJPP_FILE_HPP__ */
|
| 187 |
|