123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #ifndef __PJPP_FILE_HPP__
- #define __PJPP_FILE_HPP__
- #include <pj/file_io.h>
- #include <pj/file_access.h>
- #include <pj++/types.hpp>
- #include <pj++/pool.hpp>
- class Pj_File_API
- {
- public:
-
-
-
- static bool file_exists(const char *filename)
- {
- return pj_file_exists(filename) != 0;
- }
-
-
-
- static pj_off_t file_size(const char *filename)
- {
- return pj_file_size(filename);
- }
-
-
-
- static pj_status_t file_delete(const char *filename)
- {
- return pj_file_delete(filename);
- }
-
-
-
- static pj_status_t file_move(const char *oldname, const char *newname)
- {
- return pj_file_move(oldname, newname);
- }
-
-
-
- static pj_status_t file_stat(const char *filename, pj_file_stat *buf)
- {
- return pj_file_getstat(filename, buf);
- }
- };
- class Pj_File : public Pj_Object
- {
- public:
-
-
-
- enum Offset_Type
- {
- PJ_SEEK_SET = PJ_SEEK_SET,
- PJ_SEEK_CUR = PJ_SEEK_CUR,
- PJ_SEEK_END = PJ_SEEK_END,
- };
-
-
-
- Pj_File()
- : hnd_(0)
- {
- }
-
-
-
- Pj_File(Pj_Pool *pool, const char *filename,
- unsigned access = PJ_O_RDONLY)
- : hnd_(NULL)
- {
- open(pool, filename, access);
- }
-
-
-
- ~Pj_File()
- {
- close();
- }
-
-
-
- pj_status_t open(Pj_Pool *pool, const char *filename,
- unsigned access = PJ_O_RDONLY )
- {
- close();
- return pj_file_open(pool->pool_(), filename, access, &hnd_);
- }
-
-
-
- void close()
- {
- if (hnd_ != 0) {
- pj_file_close(hnd_);
- hnd_ = 0;
- }
- }
-
-
-
- pj_ssize_t write(const void *buff, pj_size_t size)
- {
- pj_ssize_t bytes = size;
- if (pj_file_write(hnd_, buff, &bytes) != PJ_SUCCESS)
- return -1;
- return bytes;
- }
-
-
-
- pj_ssize_t read(void *buf, pj_size_t size)
- {
- pj_ssize_t bytes = size;
- if (pj_file_read(hnd_, buf, &bytes) != PJ_SUCCESS)
- return -1;
- return bytes;
- }
-
-
-
- pj_status_t setpos(pj_off_t offset, Offset_Type whence)
- {
- return pj_file_setpos(hnd_, offset,
- (enum pj_file_seek_type)whence);
- }
-
-
-
- pj_off_t getpos()
- {
- pj_off_t pos;
- if (pj_file_getpos(hnd_, &pos) != PJ_SUCCESS)
- return -1;
- return pos;
- }
- private:
- pj_oshandle_t hnd_;
- };
- #endif
|