binfile.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Lesser General Public
  4. * License as published by the Free Software Foundation; either
  5. * version 2.1 of the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Lesser General Public
  13. * License along with this library; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * binfile.h - Binary file I/O
  17. * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
  18. */
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include "binfile.h"
  22. /***** binfbase *****/
  23. binfbase::binfbase()
  24. : f(NULL) {
  25. }
  26. binfbase::~binfbase() {
  27. if (f != NULL) close();
  28. }
  29. void binfbase::close() {
  30. if (f != NULL) {
  31. if (fclose(f) == EOF) err |= Fatal;
  32. else f = NULL;
  33. } else
  34. err |= NotOpen;
  35. }
  36. void binfbase::seek(long pos, Offset offs) {
  37. int error = -1;
  38. if (f == NULL) {
  39. err |= NotOpen;
  40. return;
  41. }
  42. switch (offs) {
  43. case Set:
  44. error = fseek(f, pos, SEEK_SET);
  45. break;
  46. case Add:
  47. error = fseek(f, pos, SEEK_CUR);
  48. break;
  49. case End:
  50. error = fseek(f, pos, SEEK_END);
  51. break;
  52. }
  53. if (error == -1)
  54. err |= Fatal;
  55. }
  56. long binfbase::pos() {
  57. long pos;
  58. if (f == NULL) {
  59. err |= NotOpen;
  60. return 0;
  61. }
  62. pos = ftell(f);
  63. if (pos == -1) {
  64. err |= Fatal;
  65. return 0;
  66. } else
  67. return pos;
  68. }
  69. /***** binifstream *****/
  70. binifstream::binifstream() {
  71. }
  72. binifstream::binifstream(const char *filename, const Mode mode) {
  73. open(filename, mode);
  74. }
  75. #if BINIO_ENABLE_STRING
  76. binifstream::binifstream(const std::string &filename, const Mode mode) {
  77. open(filename, mode);
  78. }
  79. #endif
  80. binifstream::~binifstream() {
  81. }
  82. void binifstream::open(const char *filename, const Mode mode) {
  83. f = fopen(filename, "rb");
  84. if (f == NULL)
  85. switch (errno) {
  86. case ENOENT:
  87. err |= NotFound;
  88. break;
  89. case EACCES:
  90. err |= Denied;
  91. break;
  92. default:
  93. err |= NotOpen;
  94. break;
  95. }
  96. }
  97. #if BINIO_ENABLE_STRING
  98. void binifstream::open(const std::string &filename, const Mode mode) {
  99. open(filename.c_str(), mode);
  100. }
  101. #endif
  102. binifstream::Byte binifstream::getByte() {
  103. int read;
  104. if (f != NULL) {
  105. read = fgetc(f);
  106. if (read == EOF) err |= Eof;
  107. return (Byte)read;
  108. } else {
  109. err |= NotOpen;
  110. return 0;
  111. }
  112. }
  113. /***** binofstream *****/
  114. binofstream::binofstream() {
  115. }
  116. binofstream::binofstream(const char *filename, const Mode mode) {
  117. open(filename, mode);
  118. }
  119. #if BINIO_ENABLE_STRING
  120. binofstream::binofstream(const std::string &filename, const Mode mode) {
  121. open(filename, mode);
  122. }
  123. #endif
  124. binofstream::~binofstream() {
  125. }
  126. void binofstream::open(const char *filename, const Mode mode) {
  127. char modestr[] = "wb";
  128. // Check if append mode is desired
  129. if (mode & Append) modestr[0] = 'a';
  130. f = fopen(filename, modestr);
  131. if (f == NULL)
  132. switch (errno) {
  133. case EEXIST:
  134. case EACCES:
  135. case EROFS:
  136. err |= Denied;
  137. break;
  138. case ENOENT:
  139. err |= NotFound;
  140. break;
  141. default:
  142. err |= NotOpen;
  143. break;
  144. }
  145. }
  146. #if BINIO_ENABLE_STRING
  147. void binofstream::open(const std::string &filename, const Mode mode) {
  148. open(filename.c_str(), mode);
  149. }
  150. #endif
  151. void binofstream::putByte(Byte b) {
  152. if (f == NULL) {
  153. err |= NotOpen;
  154. return;
  155. }
  156. if (fputc(b, f) == EOF)
  157. err |= Fatal;
  158. }
  159. /***** binfstream *****/
  160. binfstream::binfstream() {
  161. }
  162. binfstream::binfstream(const char *filename, const Mode mode) {
  163. open(filename, mode);
  164. }
  165. #if BINIO_ENABLE_STRING
  166. binfstream::binfstream(const std::string &filename, const Mode mode) {
  167. open(filename, mode);
  168. }
  169. #endif
  170. binfstream::~binfstream() {
  171. }
  172. void binfstream::open(const char *filename, const Mode mode) {
  173. char modestr[] = "w+b"; // Create & at beginning
  174. int ferror = 0;
  175. // Apply desired mode
  176. if (mode & NoCreate) {
  177. if (!(mode & Append))
  178. modestr[0] = 'r'; // NoCreate & at beginning
  179. } else
  180. if (mode & Append) // Create & append
  181. modestr[0] = 'a';
  182. f = fopen(filename, modestr);
  183. // NoCreate & append (emulated -- not possible with standard C fopen())
  184. if (f != NULL && (mode & Append) && (mode & NoCreate))
  185. ferror = fseek(f, 0, SEEK_END);
  186. if (f == NULL || ferror == -1) {
  187. switch (errno) {
  188. case EEXIST:
  189. case EACCES:
  190. case EROFS:
  191. err |= Denied;
  192. break;
  193. case ENOENT:
  194. err |= NotFound;
  195. break;
  196. default:
  197. err |= NotOpen;
  198. break;
  199. }
  200. }
  201. }
  202. #if BINIO_ENABLE_STRING
  203. void binfstream::open(const std::string &filename, const Mode mode) {
  204. open(filename.c_str(), mode);
  205. }
  206. #endif