OggFileStream.h
Go to the documentation of this file.00001 #ifndef OGGFILESTREAM_H_
00002 #define OGGFILESTREAM_H_
00003
00011 namespace sound
00012 {
00013 class OggFileStream
00014 {
00015 ScopedArchiveBuffer& _source;
00016
00017 unsigned char* _curPtr;
00018 public:
00019 OggFileStream (ScopedArchiveBuffer& source) :
00020 _source(source)
00021 {
00022
00023 _curPtr = _source.buffer;
00024 }
00025
00026 static std::size_t oggReadFunc (void* ptr, std::size_t byteSize, std::size_t sizeToRead, void* datasource)
00027 {
00028 OggFileStream* self = reinterpret_cast<OggFileStream*> (datasource);
00029
00030
00031
00032 std::size_t bytesRequested = sizeToRead * byteSize;
00033 std::size_t bytesLeft = self->_source.buffer + self->_source.length - self->_curPtr;
00034
00035
00036 std::size_t actualSizeToRead = (bytesRequested < bytesLeft) ? bytesRequested : bytesLeft;
00037
00038 if (actualSizeToRead > 0) {
00039
00040 memcpy(ptr, self->_curPtr, actualSizeToRead);
00041
00042 self->_curPtr += actualSizeToRead;
00043 }
00044
00045
00046 return actualSizeToRead;
00047 }
00048
00049 static int oggSeekFunc (void* datasource, ogg_int64_t offset, int whence)
00050 {
00051 OggFileStream* self = reinterpret_cast<OggFileStream*> (datasource);
00052
00053
00054 switch (whence) {
00055 case SEEK_SET:
00056
00057 self->_curPtr = self->_source.buffer + offset;
00058 break;
00059 case SEEK_CUR:
00060
00061 self->_curPtr += offset;
00062 break;
00063 case SEEK_END:
00064
00065 self->_curPtr = self->_source.buffer + self->_source.length;
00066 break;
00067 default:
00068
00069 break;
00070 };
00071
00072
00073 if (self->_curPtr > self->_source.buffer + self->_source.length) {
00074 self->_curPtr = self->_source.buffer + self->_source.length;
00075 }
00076
00077 return 0;
00078 }
00079
00080 static int oggCloseFunc (void* datasource)
00081 {
00082 return 1;
00083 }
00084
00085 static long oggTellFunc (void* datasource)
00086 {
00087 OggFileStream* self = reinterpret_cast<OggFileStream*> (datasource);
00088 return static_cast<long> (self->_curPtr - self->_source.buffer);
00089 }
00090 };
00091
00092 }
00093
00094 #endif