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 		void buffering_off() {}
70 		int lock();
71 		void unlock();
72 		const(char)* fname();
73 		version (Windows) {
74 			const(wchar_t)* wfname() { return null; }
75 			int subfile_open(const(wchar_t)*) { return -1; }
76 		}
77 		int subfile_open(const(char)*);
78 		void subfile_close();
79 
80 		int tempbuffer_open(void*, size_t);
81 		void tempbuffer_close();
82 
83 	protected:
84 		LibRaw_abstract_datastream *substream;
85 	}
86 }+/
87 
88 /+#ifdef WIN32
89 template class std::auto_ptr<std::streambuf>;
90 #endif
91 
92 class LibRaw_file_datastream : public LibRaw_abstract_datastream
93 {
94 protected:
95 	std::auto_ptr<std::streambuf> f;       /* will close() automatically through dtor */
96 	std::auto_ptr<std::streambuf> saved_f; /* when *f is a subfile, *saved_f is the master file */
97 	std::string filename;
98 	INT64 _fsize;
99 #ifdef WIN32
100 	std::wstring wfilename;
101 #endif
102 	FILE *jas_file;
103 
104 public:
105 	virtual ~LibRaw_file_datastream();
106 	LibRaw_file_datastream(const char *fname);
107 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
108 	LibRaw_file_datastream(const wchar_t *fname);
109 #endif
110 	virtual void *make_jas_stream();
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 void *make_jas_stream();
173 
174 	virtual int read(void *ptr, size_t size, size_t nmemb);
175 	virtual int eof();
176 	virtual int seek(INT64 o, int whence);
177 	virtual INT64 tell();
178 	virtual INT64 size() { return _fsize; }
179 	virtual char *gets(char *str, int sz);
180 	virtual int scanf_one(const char *fmt, void *val);
181 	virtual const char *fname();
182 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
183 	virtual const wchar_t *wfname();
184 	virtual int subfile_open(const wchar_t *fn);
185 #endif
186 	virtual int subfile_open(const char *fn);
187 	virtual void subfile_close();
188 	virtual int get_char()
189 	{
190 #if !defined(_WIN32) && !defined(__MINGW32__)
191 		return substream ? substream->get_char() : getc_unlocked(f);
192 #else
193 		return substream ? substream->get_char() : fgetc(f);
194 #endif
195 	}
196 
197 protected:
198 	FILE *f, *sav;
199 	std::string filename;
200 	INT64 _fsize;
201 #ifdef WIN32
202 	std::wstring wfilename;
203 #endif
204 };
205 
206 #ifdef WIN32
207 class LibRaw_windows_datastream : public LibRaw_buffer_datastream
208 {
209 public:
210 	/* ctor: high level constructor opens a file by name */
211 	LibRaw_windows_datastream(const TCHAR *sFile);
212 	/* ctor: construct with a file handle - caller is responsible for closing the file handle */
213 	LibRaw_windows_datastream(HANDLE hFile);
214 	/* dtor: unmap and close the mapping handle */
215 	virtual ~LibRaw_windows_datastream();
216 	virtual INT64 size() { return cbView_; }
217 
218 protected:
219 	void Open(HANDLE hFile);
220 	inline void reconstruct_base()
221 	{
222 		/* this subterfuge is to overcome the private-ness of LibRaw_buffer_datastream */
223 		(LibRaw_buffer_datastream &)*this = LibRaw_buffer_datastream(pView_, (size_t)cbView_);
224 	}
225 
226 	HANDLE hMap_;    /* handle of the file mapping */
227 	void *pView_;    /* pointer to the mapped memory */
228 	__int64 cbView_; /* size of the mapping in bytes */
229 };
230 
231 #endif
232 
233 #ifdef USE_DNGSDK
234 
235 class libraw_dng_stream : public dng_stream
236 {
237 public:
238 	libraw_dng_stream(LibRaw_abstract_datastream *p)
239 			: dng_stream((dng_abort_sniffer *)NULL, kBigBufferSize, 0), parent_stream(p)
240 	{
241 		if (parent_stream)
242 		{
243 			parent_stream->buffering_off();
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