PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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