file.hpp 3.7 KB

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