1 module libraw.datastream;
2 
3 import libraw.const_;
4 import libraw.types;
5 
6 import core.stdc.errno;
7 import core.stdc.stdio;
8 import core.stdc.string;
9 //#include <sys/types.h>
10 import core.stdc.stdlib;
11 
12 version (Windows) {
13 	import core.sys.windows.winsock2;
14 }
15 
16 
17 //#include <fstream>
18 //#include <memory>
19 
20 version (USE_DNGSDK) {
21 	/+#if defined WIN32 || defined(__MINGW32__)
22 	#define qWinOS 1
23 	#define qMacOS 0
24 	#elif defined(__APPLE__)
25 	#define qWinOS 0
26 	#define qMacOS 1
27 	#else
28 	/* define OS types for DNG here */
29 	#endif
30 	#define qDNGXMPDocOps 0
31 	#define qDNGUseLibJPEG 1
32 	#define qDNGXMPFiles 0
33 	#define qDNGExperimental 1
34 	#define qDNGThreadSafe 1
35 	#include "dng_stream.h"
36 	#endif /* DNGSDK */ +/
37 }
38 
39 /*#define IOERROR()                                                                                                      \
40 	do                                                                                                                   \
41 	{                                                                                                                    \
42 		throw LIBRAW_EXCEPTION_IO_EOF;                                                                                     \
43 	} while (0)*/
44 
45 /+extern (C++) {
46 	class LibRaw_bit_buffer;
47 
48 	class LibRaw_abstract_datastream
49 	{
50 		// NOTE: the virtual destructor produces a multiple definition error if a
51 		//       body is defined and an unresolved external if no body is defined.
52 		//       We define a dummy symbol to take the space of the unresolved symbol
53 		//       and define the destructor without a body to solve this.
54 		void* dummy(uint) { return null; }
55 
56 		//~this();
57 
58 		abstract int valid();
59 		abstract int read(void *, size_t, size_t);
60 		abstract int seek(long, int);
61 		abstract long tell();
62 		abstract long size();
63 		abstract int get_char();
64 		abstract char *gets(char *, int);
65 		abstract int scanf_one(const(char)*, void*);
66 		abstract int eof();
67 		abstract void *make_jas_stream();
68 		int jpeg_src(void*);
69 		int lock();
70 		void unlock();
71 		const(char)* fname();
72 		version (Windows) {
73 			const(wchar_t)* wfname() { return null; }
74 			int subfile_open(const(wchar_t)*) { return -1; }
75 		}
76 		int subfile_open(const(char)*);
77 		void subfile_close();
78 
79 		int tempbuffer_open(void*, size_t);
80 		void tempbuffer_close();
81 
82 	protected:
83 		LibRaw_abstract_datastream *substream;
84 	}
85 }+/
86 
87 /+#ifdef WIN32
88 template class std::auto_ptr<std::streambuf>;
89 #endif
90 
91 class LibRaw_file_datastream : public LibRaw_abstract_datastream
92 {
93 protected:
94 	std::auto_ptr<std::streambuf> f;       /* will close() automatically through dtor */
95 	std::auto_ptr<std::streambuf> saved_f; /* when *f is a subfile, *saved_f is the master file */
96 	std::string filename;
97 	INT64 _fsize;
98 #ifdef WIN32
99 	std::wstring wfilename;
100 #endif
101 	FILE *jas_file;
102 
103 public:
104 	virtual ~LibRaw_file_datastream();
105 	LibRaw_file_datastream(const char *fname);
106 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
107 	LibRaw_file_datastream(const wchar_t *fname);
108 #endif
109 	virtual void *make_jas_stream();
110 	virtual int jpeg_src(void *jpegdata);
111 	virtual int valid();
112 	virtual int read(void *ptr, size_t size, size_t nmemb);
113 	virtual int eof();
114 	virtual int seek(INT64 o, int whence);
115 	virtual INT64 tell();
116 	virtual INT64 size() { return _fsize; }
117 	virtual int get_char()
118 	{
119 		if (substream)
120 			return substream->get_char();
121 		return f->sbumpc();
122 	}
123 	virtual char *gets(char *str, int sz);
124 	virtual int scanf_one(const char *fmt, void *val);
125 	virtual const char *fname();
126 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
127 	virtual const wchar_t *wfname();
128 	virtual int subfile_open(const wchar_t *fn);
129 #endif
130 	virtual int subfile_open(const char *fn);
131 	virtual void subfile_close();
132 };
133 
134 class LibRaw_buffer_datastream : public LibRaw_abstract_datastream
135 {
136 public:
137 	LibRaw_buffer_datastream(void *buffer, size_t bsize);
138 	virtual ~LibRaw_buffer_datastream();
139 	virtual int valid();
140 	virtual void *make_jas_stream();
141 	virtual int jpeg_src(void *jpegdata);
142 	virtual int read(void *ptr, size_t sz, size_t nmemb);
143 	virtual int eof();
144 	virtual int seek(INT64 o, int whence);
145 	virtual INT64 tell();
146 	virtual INT64 size() { return streamsize; }
147 	virtual char *gets(char *s, int sz);
148 	virtual int scanf_one(const char *fmt, void *val);
149 	virtual int get_char()
150 	{
151 		if (substream)
152 			return substream->get_char();
153 		if (streampos >= streamsize)
154 			return -1;
155 		return buf[streampos++];
156 	}
157 
158 private:
159 	unsigned char *buf;
160 	size_t streampos, streamsize;
161 };
162 
163 class LibRaw_bigfile_datastream : public LibRaw_abstract_datastream
164 {
165 public:
166 	LibRaw_bigfile_datastream(const char *fname);
167 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
168 	LibRaw_bigfile_datastream(const wchar_t *fname);
169 #endif
170 	virtual ~LibRaw_bigfile_datastream();
171 	virtual int valid();
172 	virtual int jpeg_src(void *jpegdata);
173 	virtual void *make_jas_stream();
174 
175 	virtual int read(void *ptr, size_t size, size_t nmemb);
176 	virtual int eof();
177 	virtual int seek(INT64 o, int whence);
178 	virtual INT64 tell();
179 	virtual INT64 size() { return _fsize; }
180 	virtual char *gets(char *str, int sz);
181 	virtual int scanf_one(const char *fmt, void *val);
182 	virtual const char *fname();
183 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
184 	virtual const wchar_t *wfname();
185 	virtual int subfile_open(const wchar_t *fn);
186 #endif
187 	virtual int subfile_open(const char *fn);
188 	virtual void subfile_close();
189 	virtual int get_char()
190 	{
191 #if !defined(_WIN32) && !defined(__MINGW32__)
192 		return substream ? substream->get_char() : getc_unlocked(f);
193 #else
194 		return substream ? substream->get_char() : fgetc(f);
195 #endif
196 	}
197 
198 protected:
199 	FILE *f, *sav;
200 	std::string filename;
201 	INT64 _fsize;
202 #ifdef WIN32
203 	std::wstring wfilename;
204 #endif
205 };
206 
207 #ifdef WIN32
208 class LibRaw_windows_datastream : public LibRaw_buffer_datastream
209 {
210 public:
211 	/* ctor: high level constructor opens a file by name */
212 	LibRaw_windows_datastream(const TCHAR *sFile);
213 	/* ctor: construct with a file handle - caller is responsible for closing the file handle */
214 	LibRaw_windows_datastream(HANDLE hFile);
215 	/* dtor: unmap and close the mapping handle */
216 	virtual ~LibRaw_windows_datastream();
217 	virtual INT64 size() { return cbView_; }
218 
219 protected:
220 	void Open(HANDLE hFile);
221 	inline void reconstruct_base()
222 	{
223 		/* this subterfuge is to overcome the private-ness of LibRaw_buffer_datastream */
224 		(LibRaw_buffer_datastream &)*this = LibRaw_buffer_datastream(pView_, (size_t)cbView_);
225 	}
226 
227 	HANDLE hMap_;    /* handle of the file mapping */
228 	void *pView_;    /* pointer to the mapped memory */
229 	__int64 cbView_; /* size of the mapping in bytes */
230 };
231 
232 #endif
233 
234 #ifdef USE_DNGSDK
235 
236 class libraw_dng_stream : public dng_stream
237 {
238 public:
239 	libraw_dng_stream(LibRaw_abstract_datastream *p)
240 			: dng_stream((dng_abort_sniffer *)NULL, kBigBufferSize, 0), parent_stream(p)
241 	{
242 		if (parent_stream)
243 		{
244 			off = parent_stream->tell();
245 			parent_stream->seek(0UL, SEEK_SET); /* seek to start */
246 		}
247 	}
248 	~libraw_dng_stream()
249 	{
250 		if (parent_stream)
251 			parent_stream->seek(off, SEEK_SET);
252 	}
253 	virtual uint64 DoGetLength()
254 	{
255 		if (parent_stream)
256 			return parent_stream->size();
257 		return 0;
258 	}
259 	virtual void DoRead(void *data, uint32 count, uint64 offset)
260 	{
261 		if (parent_stream)
262 		{
263 			parent_stream->seek(offset, SEEK_SET);
264 			parent_stream->read(data, 1, count);
265 		}
266 	}
267 
268 private:
269 	libraw_dng_stream(const libraw_dng_stream &stream);
270 	libraw_dng_stream &operator=(const libraw_dng_stream &stream);
271 	LibRaw_abstract_datastream *parent_stream;
272 	INT64 off;
273 };
274 
275 #endif
276 
277 #endif /* cplusplus */ +/
278