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