/src/FreeImage/Source/LibRawLite/libraw/libraw_datastream.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 205 lines · 147 code · 31 blank · 27 comment · 4 complexity · 96832c727268ec920807280a831f8919 MD5 · raw file

  1. /* -*- C -*-
  2. * File: libraw_datastream.h
  3. * Copyright 2008-2010 LibRaw LLC (info@libraw.org)
  4. * Created: Sun Jan 18 13:07:35 2009
  5. *
  6. * LibRaw Data stream interface
  7. LibRaw is free software; you can redistribute it and/or modify
  8. it under the terms of the one of three licenses as you choose:
  9. 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  10. (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  11. 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  12. (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
  13. 3. LibRaw Software License 27032010
  14. (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
  15. */
  16. #ifndef __LIBRAW_DATASTREAM_H
  17. #define __LIBRAW_DATASTREAM_H
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #ifndef __cplusplus
  23. #else /* __cplusplus */
  24. #include "libraw_const.h"
  25. #include "libraw_types.h"
  26. #include <fstream>
  27. #include <memory>
  28. #if defined (WIN32)
  29. #include <winsock2.h>
  30. /* MSVS 2008 and above... */
  31. #if _MSC_VER >= 1500
  32. #define WIN32SECURECALLS
  33. #endif
  34. #endif
  35. #define IOERROR() do { throw LIBRAW_EXCEPTION_IO_EOF; } while(0)
  36. class LibRaw_buffer_datastream;
  37. class LibRaw_byte_buffer;
  38. class LibRaw_bit_buffer;
  39. class DllDef LibRaw_abstract_datastream
  40. {
  41. public:
  42. LibRaw_abstract_datastream(){ substream=0;};
  43. virtual ~LibRaw_abstract_datastream(void){if(substream) delete substream;}
  44. virtual int valid() = 0;
  45. virtual int read(void *,size_t, size_t ) = 0;
  46. virtual int seek(INT64 , int ) = 0;
  47. virtual INT64 tell() = 0;
  48. virtual int get_char() = 0;
  49. virtual char* gets(char *, int) = 0;
  50. virtual int scanf_one(const char *, void *) = 0;
  51. virtual int eof() = 0;
  52. virtual void * make_jas_stream() = 0;
  53. /* Make buffer from current offset */
  54. virtual LibRaw_byte_buffer *make_byte_buffer(unsigned int sz);
  55. /* subfile parsing not implemented in base class */
  56. virtual const char* fname(){ return NULL;};
  57. virtual int subfile_open(const char*) { return -1;}
  58. virtual void subfile_close() { }
  59. virtual int tempbuffer_open(void*, size_t);
  60. virtual void tempbuffer_close();
  61. protected:
  62. LibRaw_abstract_datastream *substream;
  63. };
  64. #ifdef WIN32
  65. template class DllDef std::auto_ptr<std::streambuf>;
  66. #endif
  67. class DllDef LibRaw_file_datastream: public LibRaw_abstract_datastream
  68. {
  69. protected:
  70. std::auto_ptr<std::streambuf> f; /* will close() automatically through dtor */
  71. std::auto_ptr<std::streambuf> saved_f; /* when *f is a subfile, *saved_f is the master file */
  72. const char *filename;
  73. public:
  74. virtual ~LibRaw_file_datastream(){}
  75. LibRaw_file_datastream(const char *fname);
  76. virtual void *make_jas_stream();
  77. virtual int valid();
  78. virtual int read(void * ptr,size_t size, size_t nmemb);
  79. virtual int eof();
  80. virtual int seek(INT64 o, int whence);
  81. virtual INT64 tell();
  82. virtual int get_char()
  83. {
  84. if(substream) return substream->get_char();
  85. return f->sbumpc();
  86. }
  87. virtual char* gets(char *str, int sz);
  88. virtual int scanf_one(const char *fmt, void*val);
  89. virtual const char* fname();
  90. virtual int subfile_open(const char *fn);
  91. virtual void subfile_close();
  92. };
  93. class DllDef LibRaw_buffer_datastream : public LibRaw_abstract_datastream
  94. {
  95. public:
  96. LibRaw_buffer_datastream(void *buffer, size_t bsize);
  97. virtual ~LibRaw_buffer_datastream();
  98. virtual int valid();
  99. virtual void *make_jas_stream();
  100. virtual LibRaw_byte_buffer *make_byte_buffer(unsigned int sz);
  101. virtual int read(void * ptr,size_t sz, size_t nmemb);
  102. virtual int eof();
  103. virtual int seek(INT64 o, int whence);
  104. virtual INT64 tell();
  105. virtual char* gets(char *s, int sz);
  106. virtual int scanf_one(const char *fmt, void* val);
  107. virtual int get_char()
  108. {
  109. if(substream) return substream->get_char();
  110. if(streampos>=streamsize)
  111. return -1;
  112. return buf[streampos++];
  113. }
  114. private:
  115. unsigned char *buf;
  116. size_t streampos,streamsize;
  117. };
  118. class DllDef LibRaw_bigfile_datastream : public LibRaw_abstract_datastream
  119. {
  120. public:
  121. LibRaw_bigfile_datastream(const char *fname);
  122. virtual ~LibRaw_bigfile_datastream();
  123. virtual int valid();
  124. virtual void *make_jas_stream();
  125. virtual int read(void * ptr,size_t size, size_t nmemb);
  126. virtual int eof();
  127. virtual int seek(INT64 o, int whence);
  128. virtual INT64 tell();
  129. virtual char* gets(char *str, int sz);
  130. virtual int scanf_one(const char *fmt, void*val);
  131. virtual const char *fname();
  132. virtual int subfile_open(const char *fn);
  133. virtual void subfile_close();
  134. virtual int get_char()
  135. {
  136. #ifndef WIN32
  137. return substream?substream->get_char():getc_unlocked(f);
  138. #else
  139. return substream?substream->get_char():fgetc(f);
  140. #endif
  141. }
  142. private:
  143. FILE *f,*sav;
  144. const char *filename;
  145. };
  146. #ifdef WIN32
  147. class DllDef LibRaw_windows_datastream : public LibRaw_buffer_datastream
  148. {
  149. public:
  150. /* ctor: high level constructor opens a file by name */
  151. LibRaw_windows_datastream(const TCHAR* sFile);
  152. /* ctor: construct with a file handle - caller is responsible for closing the file handle */
  153. LibRaw_windows_datastream(HANDLE hFile);
  154. /* dtor: unmap and close the mapping handle */
  155. virtual ~LibRaw_windows_datastream();
  156. protected:
  157. void Open(HANDLE hFile);
  158. inline void reconstruct_base()
  159. {
  160. /* this subterfuge is to overcome the private-ness of LibRaw_buffer_datastream */
  161. (LibRaw_buffer_datastream&)*this = LibRaw_buffer_datastream(pView_, (size_t)cbView_);
  162. }
  163. HANDLE hMap_; /* handle of the file mapping */
  164. void* pView_; /* pointer to the mapped memory */
  165. __int64 cbView_; /* size of the mapping in bytes */
  166. };
  167. #endif
  168. #endif /* cplusplus */
  169. #endif