Skip to content

Instantly share code, notes, and snippets.

@jmue
Created August 12, 2011 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmue/1141634 to your computer and use it in GitHub Desktop.
Save jmue/1141634 to your computer and use it in GitHub Desktop.
LibRaw stream implementation
#include "testdatastream.h"
TestDataStream::TestDataStream(const char *fname)
: filename(fname)
{
}
TestDataStream::~TestDataStream(void)
{
}
int TestDataStream::valid()
{
return f.get() ? 1 : 0;
}
int TestDataStream::read(void* ptr, size_t size, size_t nmemb)
{
if(substream)
return substream->read(ptr,size,nmemb);
//LR_STREAM_CHK();
return int(f->sgetn(static_cast<char*>(ptr), std::streamsize(nmemb * size)) / size);
}
int TestDataStream::seek(INT64 offset, int whence)
{
if(substream)
return substream->seek(offset,whence);
//LR_STREAM_CHK();
std::ios_base::seekdir dir;
switch (whence)
{
case SEEK_SET: dir = std::ios_base::beg; break;
case SEEK_CUR: dir = std::ios_base::cur; break;
case SEEK_END: dir = std::ios_base::end; break;
default: dir = std::ios_base::beg;
}
return (int)f->pubseekoff((long)offset, dir);
}
INT64 TestDataStream::tell()
{
if(substream)
return substream->tell();
//LR_STREAM_CHK();
return f->pubseekoff(0, std::ios_base::cur);
}
int TestDataStream::get_char()
{
if(substream)
return substream->get_char();
//LR_STREAM_CHK();
return f->sbumpc();
}
char* TestDataStream::gets(char* str, int size)
{
if(substream)
return substream->gets(str,size);
//LR_STREAM_CHK();
std::istream is(f.get());
is.getline(str, size);
if (is.fail())
return 0;
return str;
}
int TestDataStream::scanf_one(const char* fmt, void* val)
{
if(substream)
return substream->scanf_one(fmt,val);
//LR_STREAM_CHK();
std::istream is(f.get());
/* HUGE ASSUMPTION: *fmt is either "%d" or "%f" */
if (strcmp(fmt, "%d") == 0)
{
int d;
is >> d;
if (is.fail())
return EOF;
*(static_cast<int*>(val)) = d;
}
else
{
float f;
is >> f;
if (is.fail())
return EOF;
*(static_cast<float*>(val)) = f;
}
return 1;
}
int TestDataStream::eof()
{
if(substream)
return substream->eof();
//LR_STREAM_CHK();
return f->sgetc() == EOF;
}
void* TestDataStream::make_jas_stream()
{
return NULL;
}
#pragma once
#include <stdlib.h>
#include "libraw/libraw_datastream.h"
class TestDataStream : public LibRaw_abstract_datastream
{
public:
TestDataStream(const char *fname);
~TestDataStream(void);
virtual int valid();
virtual int read(void* ptr, size_t size, size_t nmemb);
virtual int seek(INT64 offset, int whence);
virtual INT64 tell();
virtual int get_char();
virtual char* gets(char* str, int size);
virtual int scanf_one(const char* fmt, void* val);
virtual int eof();
virtual void* make_jas_stream();
protected:
std::auto_ptr<std::streambuf> f; /* will close() automatically through dtor */
std::auto_ptr<std::streambuf> saved_f; /* when *f is a subfile, *saved_f is the master file */
const char *filename;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment