HPGSTranslator now uses the class StreamBuffer

adjusted StreamBuffer::Seek() to match BPositionIO API


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24078 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2008-02-23 15:05:45 +00:00
parent e6190664a1
commit ac43767349
5 changed files with 49 additions and 22 deletions
@@ -39,5 +39,5 @@ IStreamWrapper::tellg()
void void
IStreamWrapper::seekg(Int64 pos) IStreamWrapper::seekg(Int64 pos)
{ {
fStream.Seek(pos); fStream.Seek(pos, SEEK_SET);
} }
@@ -129,8 +129,7 @@ HPGSTranslator::DerivedIdentify(BPositionIO *stream,
free(reader); free(reader);
free(size_dev); free(size_dev);
free(istream->stream); hpgs_free_wrapper_istream(istream);
free(istream);
return err; return err;
} }
+21 -9
View File
@@ -9,9 +9,11 @@
#include <hpgs.h> #include <hpgs.h>
#include <DataIO.h> #include <DataIO.h>
#include "StreamBuffer.h"
typedef struct my_hpgs_istream_st typedef struct my_hpgs_istream_st
{ {
BPositionIO *stream; StreamBuffer *buffer;
status_t iseof; status_t iseof;
} my_hpgs_istream; } my_hpgs_istream;
@@ -22,7 +24,7 @@ positionio_getc(my_hpgs_istream *stream)
unsigned char value; unsigned char value;
status_t error; status_t error;
stream->iseof = 0; stream->iseof = 0;
error = stream->stream->Read((void *)&value, sizeof(unsigned char)); error = stream->buffer->Read((void *)&value, sizeof(unsigned char));
if (error > B_OK) if (error > B_OK)
return value; return value;
stream->iseof = EOF; stream->iseof = EOF;
@@ -33,7 +35,7 @@ positionio_getc(my_hpgs_istream *stream)
static int static int
positionio_ungetc(int c, my_hpgs_istream *stream) positionio_ungetc(int c, my_hpgs_istream *stream)
{ {
return stream->stream->Seek(-1, SEEK_CUR); return stream->buffer->Seek(-1, SEEK_CUR);
} }
@@ -61,7 +63,7 @@ positionio_error(my_hpgs_istream *stream)
static int static int
positionio_tell(my_hpgs_istream *stream, size_t *pos) positionio_tell(my_hpgs_istream *stream, size_t *pos)
{ {
*pos = stream->stream->Position(); *pos = stream->buffer->Position();
return 0; return 0;
} }
@@ -69,7 +71,7 @@ positionio_tell(my_hpgs_istream *stream, size_t *pos)
static int static int
positionio_seek(my_hpgs_istream *stream, size_t pos) positionio_seek(my_hpgs_istream *stream, size_t pos)
{ {
stream->stream->Seek(pos, SEEK_SET); stream->buffer->Seek(pos, SEEK_SET);
return 0; return 0;
} }
@@ -77,7 +79,7 @@ positionio_seek(my_hpgs_istream *stream, size_t pos)
static int static int
positionio_seekend(my_hpgs_istream *stream, size_t pos) positionio_seekend(my_hpgs_istream *stream, size_t pos)
{ {
stream->stream->Seek(pos, SEEK_END); stream->buffer->Seek(pos, SEEK_END);
return 0; return 0;
} }
@@ -88,7 +90,7 @@ positionio_read(void *ptr, size_t size, size_t nmemb, my_hpgs_istream *stream)
unsigned char *iptr = (unsigned char *)ptr; unsigned char *iptr = (unsigned char *)ptr;
size_t i = 0; size_t i = 0;
for (; i < nmemb; i++) { for (; i < nmemb; i++) {
if (size != stream->stream->Read(iptr, size)) if (size != stream->buffer->Read(iptr, size))
break; break;
iptr += size; iptr += size;
} }
@@ -109,7 +111,8 @@ static hpgs_istream_vtable positionio_vtable =
(hpgs_istream_seekend_func_t) positionio_seekend (hpgs_istream_seekend_func_t) positionio_seekend
}; };
hpgs_istream *hpgs_new_wrapper_istream(BPositionIO *stream) hpgs_istream *
hpgs_new_wrapper_istream(BPositionIO *stream)
{ {
hpgs_istream *ret = (hpgs_istream *)malloc(sizeof(hpgs_istream)); hpgs_istream *ret = (hpgs_istream *)malloc(sizeof(hpgs_istream));
@@ -119,9 +122,18 @@ hpgs_istream *hpgs_new_wrapper_istream(BPositionIO *stream)
ret->stream = (my_hpgs_istream *)malloc(sizeof(my_hpgs_istream)); ret->stream = (my_hpgs_istream *)malloc(sizeof(my_hpgs_istream));
ret->vtable = &positionio_vtable; ret->vtable = &positionio_vtable;
((my_hpgs_istream *)ret->stream)->iseof = 0; ((my_hpgs_istream *)ret->stream)->iseof = 0;
((my_hpgs_istream *)ret->stream)->stream = stream; ((my_hpgs_istream *)ret->stream)->buffer = new StreamBuffer(stream, 2048);
return ret; return ret;
} }
void
hpgs_free_wrapper_istream(hpgs_istream *istream)
{
delete ((my_hpgs_istream *)istream->stream)->buffer;
free(istream->stream);
free(istream);
}
#endif // READ_HELPER_H #endif // READ_HELPER_H
@@ -190,20 +190,36 @@ StreamBuffer::Write(void *pinto, size_t nbytes)
// //
// Postconditions: // Postconditions:
// //
// Returns: true if the seek was successful, // Returns: the new position
// false if the seek operation failed
// --------------------------------------------------------------- // ---------------------------------------------------------------
bool off_t
StreamBuffer::Seek(off_t position) StreamBuffer::Seek(off_t position, uint32 seekMode)
{ {
fLen = 0; // just seek in the current buffer if the new position is in it
fPos = 0; if (seekMode == SEEK_CUR) {
if (fToRead
&& (fPos + position < fLen)
&& (fPos + position >= 0)) {
fPos += position;
return Position();
} else if (!fToRead
&& (fLen + position < fBufferSize)
&& (fLen + position >= 0)) {
fLen += position;
return Position();
}
}
if (fStream->Seek(position, SEEK_SET) == position) { // flush if something to write
return true; if (!fToRead
&& fLen > 0) {
fStream->Write(fBuffer, fLen);
} }
return false; fLen = 0;
fPos = 0;
return fStream->Seek(position, seekMode);
} }
@@ -28,7 +28,7 @@ public:
void Write(void *buffer, size_t size); void Write(void *buffer, size_t size);
// copy nbytes from the stream into pinto // copy nbytes from the stream into pinto
bool Seek(off_t position); off_t Seek(off_t position, uint32 seekMode);
// seek the stream to the given position // seek the stream to the given position
off_t Position(); off_t Position();