123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #if !defined(_duck_ifstream_h)
- #define _duck_ifstream_h
- #include "duck_io.h"
- #include <stdio.h>
- #include <sstream>
- #include <ios>
- #include <assert.h>
- #include "iduck_ifstream.hpp"
- class duck_ifstream_ifstream : duck_ifstream
- {
- public:
-
- bool operator!() const
- {
- return (m_fd == 0);
- }
-
-
- bool is_open()
- {
- return (m_fd != 0);
- }
-
-
- int length()
- {
- if (m_length < 0)
- {
- FILE* fp = (FILE* ) m_fd;
- long off = ftell(fp);
-
- fseek(fp, 0,SEEK_END);
-
- long off2 = ftell(fp);
-
- fseek(fp, off, SEEK_SET);
-
- m_length = (int ) off2;
-
- return (int) off2;
- }
- else
- {
- return m_length;
- }
- }
-
-
- void time_out(unsigned long milli)
- {
- ;
- }
-
-
- bool eof()
- {
- return feof(m_fd);
- }
-
-
- operator bool () const
- {
- return (m_fd != 0);
- }
-
-
- void open(const char* filename, std::ios_base::openmode mode)
- {
- m_length = -1;
- m_fd = fopen(filename, "rb");
- }
-
-
- void open(void *src, void* ignore)
- {
- assert(0);
- }
-
- void close()
- {
- if (m_fd)
- {
- fclose(m_fd);
- }
- }
-
- void read(void *buffer, size_t len)
- {
- fread((unsigned char* ) buffer, sizeof(char), len, m_fd);
- }
-
- void get(char& c)
- {
- fread((unsigned char* ) &c, sizeof(char), 1, m_fd);
- }
-
-
- void seekg(long position)
- {
- fseek(m_fd, position, SEEK_SET);
- }
-
-
-
- void seekg(long offset, int origin)
- {
-
- switch (origin)
- {
- case std::ios_base::cur :
- fseek(m_fd, offset , SEEK_CUR);
- break;
- case std::ios_base::end :
- fseek(m_fd, offset, SEEK_END);
- break;
- case std::ios_base::beg :
- fseek(m_fd, offset, SEEK_SET);
- break;
-
- default :
- assert(0);
- break;
- }
- }
-
-
- void ignore(long offset)
- {
- fseek(m_fd, offset, SEEK_CUR);
- }
-
-
- long tellg()
- {
- const std::streamoff off = ftell(m_fd);
- return std::streampos(off);
- }
- private:
- FILE* m_fd;
-
- int m_length;
-
- };
- extern "C" {
- void MessageBox(char* title, char* msg);
- }
- #include "duck_io.h"
- class duck_ifstream_http : duck_ifstream
- {
- public:
-
- bool operator!() const
- {
- return (m_fd <= 0);
- }
-
-
- bool is_open()
- {
- return (m_fd > 0);
- }
-
-
- ~duck_ifstream_http()
- {
- duck_exit_http(m_fd);
- }
-
-
- operator bool () const
- {
- return (m_fd >= 0);
- }
-
-
- int length()
- {
- return duck_length((int ) m_fd);
- }
-
-
-
- void time_out(unsigned long milli)
- {
- duck_http_timeout(m_fd, milli);
- }
-
-
-
- bool eof()
- {
- return duck_eof(m_fd);
- }
-
-
-
-
-
- void open(const char* url, std::ios_base::openmode mode)
- {
-
- m_fd = (int) duck_init_http(url);
- if (duck_open((char *) m_fd, 0) < 0)
- {
- if (m_fd)
- {
- duck_close((int ) m_fd);
- m_fd = -1;
- }
-
- }
-
- }
-
- void open(void *src, void* ignore)
- {
- assert(0);
- }
-
- void close()
- {
- if (m_fd >= 0)
- {
- duck_close(m_fd);
- }
- }
-
- void read(void *buffer, size_t len)
- {
- size_t x;
-
- x = duck_read(m_fd, (unsigned char* ) buffer, (long ) len);
-
- if (x != len)
- {
- MessageBox("Error", "NSV Read Failed");
- }
- }
-
- void get(char& c)
- {
- duck_read(m_fd, (unsigned char *) &c, 1);
- }
-
-
- void seekg(long position)
- {
- long o = position - duck_tell(m_fd);
-
- if (o >= 0)
- {
- duck_seek(m_fd, o, SEEK_CUR);
- }
- else
- {
- duck_close(m_fd);
- duck_open((char *) m_fd, 0);
-
- duck_seek(m_fd, position, SEEK_CUR);
- }
- }
-
-
-
- void seekg(long offset, int origin)
- {
-
- switch (origin)
- {
- case std::ios_base::cur :
- duck_seek(m_fd, offset, SEEK_CUR);
- break;
- default :
- /* don't do it ! */
- /* assert(0); */
- break;
- }
- }
-
-
- void ignore(long offset)
- {
- duck_seek(m_fd, offset, SEEK_CUR);
- }
-
-
- long tellg()
- {
- long off = duck_tell(m_fd);
- return off;
- }
- private:
- int m_fd;
-
-
- #if 0
- /* disable copying ! */
- duck_ifstream_http(const duck_ifstream_http& );
-
-
- /* disable copying ! */
- operator= (const duck_ifstream_http& );
- #endif
-
- };
- #endif
|