duck_ifstream.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #if !defined(_duck_ifstream_h)
  2. #define _duck_ifstream_h
  3. #include "duck_io.h"
  4. #include <stdio.h>
  5. #include <sstream>
  6. #include <ios>
  7. #include <assert.h>
  8. #include "iduck_ifstream.hpp"
  9. class duck_ifstream_ifstream : duck_ifstream
  10. {
  11. public:
  12. bool operator!() const
  13. {
  14. return (m_fd == 0);
  15. }
  16. bool is_open()
  17. {
  18. return (m_fd != 0);
  19. }
  20. int length()
  21. {
  22. if (m_length < 0)
  23. {
  24. FILE* fp = (FILE* ) m_fd;
  25. long off = ftell(fp);
  26. fseek(fp, 0,SEEK_END);
  27. long off2 = ftell(fp);
  28. fseek(fp, off, SEEK_SET);
  29. m_length = (int ) off2;
  30. return (int) off2;
  31. }
  32. else
  33. {
  34. return m_length;
  35. }
  36. }
  37. void time_out(unsigned long milli)
  38. {
  39. ;
  40. }
  41. bool eof()
  42. {
  43. return feof(m_fd);
  44. }
  45. operator bool () const
  46. {
  47. return (m_fd != 0);
  48. }
  49. void open(const char* filename, std::ios_base::openmode mode)
  50. {
  51. m_length = -1;
  52. m_fd = fopen(filename, "rb");
  53. }
  54. void open(void *src, void* ignore)
  55. {
  56. assert(0);
  57. }
  58. void close()
  59. {
  60. if (m_fd)
  61. {
  62. fclose(m_fd);
  63. }
  64. }
  65. void read(void *buffer, size_t len)
  66. {
  67. fread((unsigned char* ) buffer, sizeof(char), len, m_fd);
  68. }
  69. void get(char& c)
  70. {
  71. fread((unsigned char* ) &c, sizeof(char), 1, m_fd);
  72. }
  73. void seekg(long position)
  74. {
  75. fseek(m_fd, position, SEEK_SET);
  76. }
  77. void seekg(long offset, int origin)
  78. {
  79. switch (origin)
  80. {
  81. case std::ios_base::cur :
  82. fseek(m_fd, offset , SEEK_CUR);
  83. break;
  84. case std::ios_base::end :
  85. fseek(m_fd, offset, SEEK_END);
  86. break;
  87. case std::ios_base::beg :
  88. fseek(m_fd, offset, SEEK_SET);
  89. break;
  90. default :
  91. assert(0);
  92. break;
  93. }
  94. }
  95. void ignore(long offset)
  96. {
  97. fseek(m_fd, offset, SEEK_CUR);
  98. }
  99. long tellg()
  100. {
  101. const std::streamoff off = ftell(m_fd);
  102. return std::streampos(off);
  103. }
  104. private:
  105. FILE* m_fd;
  106. int m_length;
  107. };
  108. extern "C" {
  109. void MessageBox(char* title, char* msg);
  110. }
  111. #include "duck_io.h"
  112. class duck_ifstream_http : duck_ifstream
  113. {
  114. public:
  115. bool operator!() const
  116. {
  117. return (m_fd <= 0);
  118. }
  119. bool is_open()
  120. {
  121. return (m_fd > 0);
  122. }
  123. ~duck_ifstream_http()
  124. {
  125. duck_exit_http(m_fd);
  126. }
  127. operator bool () const
  128. {
  129. return (m_fd >= 0);
  130. }
  131. int length()
  132. {
  133. return duck_length((int ) m_fd);
  134. }
  135. void time_out(unsigned long milli)
  136. {
  137. duck_http_timeout(m_fd, milli);
  138. }
  139. bool eof()
  140. {
  141. return duck_eof(m_fd);
  142. }
  143. void open(const char* url, std::ios_base::openmode mode)
  144. {
  145. m_fd = (int) duck_init_http(url);
  146. if (duck_open((char *) m_fd, 0) < 0)
  147. {
  148. if (m_fd)
  149. {
  150. duck_close((int ) m_fd);
  151. m_fd = -1;
  152. }
  153. }
  154. }
  155. void open(void *src, void* ignore)
  156. {
  157. assert(0);
  158. }
  159. void close()
  160. {
  161. if (m_fd >= 0)
  162. {
  163. duck_close(m_fd);
  164. }
  165. }
  166. void read(void *buffer, size_t len)
  167. {
  168. size_t x;
  169. x = duck_read(m_fd, (unsigned char* ) buffer, (long ) len);
  170. if (x != len)
  171. {
  172. MessageBox("Error", "NSV Read Failed");
  173. }
  174. }
  175. void get(char& c)
  176. {
  177. duck_read(m_fd, (unsigned char *) &c, 1);
  178. }
  179. void seekg(long position)
  180. {
  181. long o = position - duck_tell(m_fd);
  182. if (o >= 0)
  183. {
  184. duck_seek(m_fd, o, SEEK_CUR);
  185. }
  186. else
  187. {
  188. duck_close(m_fd);
  189. duck_open((char *) m_fd, 0);
  190. duck_seek(m_fd, position, SEEK_CUR);
  191. }
  192. }
  193. void seekg(long offset, int origin)
  194. {
  195. switch (origin)
  196. {
  197. case std::ios_base::cur :
  198. duck_seek(m_fd, offset, SEEK_CUR);
  199. break;
  200. default :
  201. /* don't do it ! */
  202. /* assert(0); */
  203. break;
  204. }
  205. }
  206. void ignore(long offset)
  207. {
  208. duck_seek(m_fd, offset, SEEK_CUR);
  209. }
  210. long tellg()
  211. {
  212. long off = duck_tell(m_fd);
  213. return off;
  214. }
  215. private:
  216. int m_fd;
  217. #if 0
  218. /* disable copying ! */
  219. duck_ifstream_http(const duck_ifstream_http& );
  220. /* disable copying ! */
  221. operator= (const duck_ifstream_http& );
  222. #endif
  223. };
  224. #endif