file.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* $Id$ */
  2. /*
  3. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  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. #include <pj/file_io.h>
  23. #include <pj/file_access.h>
  24. #include <pj++/types.hpp>
  25. #include <pj++/pool.hpp>
  26. //
  27. // File API.
  28. //
  29. class Pj_File_API
  30. {
  31. public:
  32. //
  33. // Check file existance.
  34. //
  35. static bool file_exists(const char *filename)
  36. {
  37. return pj_file_exists(filename) != 0;
  38. }
  39. //
  40. // Get file size.
  41. //
  42. static pj_off_t file_size(const char *filename)
  43. {
  44. return pj_file_size(filename);
  45. }
  46. //
  47. // Delete file.
  48. //
  49. static pj_status_t file_delete(const char *filename)
  50. {
  51. return pj_file_delete(filename);
  52. }
  53. //
  54. // Move/rename file.
  55. //
  56. static pj_status_t file_move(const char *oldname, const char *newname)
  57. {
  58. return pj_file_move(oldname, newname);
  59. }
  60. //
  61. // Get stat.
  62. //
  63. static pj_status_t file_stat(const char *filename, pj_file_stat *buf)
  64. {
  65. return pj_file_getstat(filename, buf);
  66. }
  67. };
  68. //
  69. // File.
  70. //
  71. class Pj_File : public Pj_Object
  72. {
  73. public:
  74. //
  75. // Offset type to be used in setpos.
  76. //
  77. enum Offset_Type
  78. {
  79. PJ_SEEK_SET = PJ_SEEK_SET,
  80. PJ_SEEK_CUR = PJ_SEEK_CUR,
  81. PJ_SEEK_END = PJ_SEEK_END,
  82. };
  83. //
  84. // Default constructor.
  85. //
  86. Pj_File()
  87. : hnd_(0)
  88. {
  89. }
  90. //
  91. // Construct and open a file.
  92. //
  93. Pj_File(Pj_Pool *pool, const char *filename,
  94. unsigned access = PJ_O_RDONLY)
  95. : hnd_(NULL)
  96. {
  97. open(pool, filename, access);
  98. }
  99. //
  100. // Destructor closes the file.
  101. //
  102. ~Pj_File()
  103. {
  104. close();
  105. }
  106. //
  107. // Open a file.
  108. //
  109. pj_status_t open(Pj_Pool *pool, const char *filename,
  110. unsigned access = PJ_O_RDONLY )
  111. {
  112. close();
  113. return pj_file_open(pool->pool_(), filename, access, &hnd_);
  114. }
  115. //
  116. // Close a file.
  117. //
  118. void close()
  119. {
  120. if (hnd_ != 0) {
  121. pj_file_close(hnd_);
  122. hnd_ = 0;
  123. }
  124. }
  125. //
  126. // Write data.
  127. //
  128. pj_ssize_t write(const void *buff, pj_size_t size)
  129. {
  130. pj_ssize_t bytes = size;
  131. if (pj_file_write(hnd_, buff, &bytes) != PJ_SUCCESS)
  132. return -1;
  133. return bytes;
  134. }
  135. //
  136. // Read data.
  137. //
  138. pj_ssize_t read(void *buf, pj_size_t size)
  139. {
  140. pj_ssize_t bytes = size;
  141. if (pj_file_read(hnd_, buf, &bytes) != PJ_SUCCESS)
  142. return -1;
  143. return bytes;
  144. }
  145. //
  146. // Set file position.
  147. //
  148. pj_status_t setpos(pj_off_t offset, Offset_Type whence)
  149. {
  150. return pj_file_setpos(hnd_, offset,
  151. (enum pj_file_seek_type)whence);
  152. }
  153. //
  154. // Get file position.
  155. //
  156. pj_off_t getpos()
  157. {
  158. pj_off_t pos;
  159. if (pj_file_getpos(hnd_, &pos) != PJ_SUCCESS)
  160. return -1;
  161. return pos;
  162. }
  163. private:
  164. pj_oshandle_t hnd_;
  165. };
  166. #endif /* __PJPP_FILE_HPP__ */