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                 // Set the pointer to the beginning of the buffer
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                 // Calculate how much we need to read.
00031                 // This can be sizeToRead*byteSize or less depending on how near the EOF marker we are
00032                 std::size_t bytesRequested = sizeToRead * byteSize;
00033                 std::size_t bytesLeft = self->_source.buffer + self->_source.length - self->_curPtr;
00034 
00035                 // Clamp the number of bytes to read to the number of bytes left
00036                 std::size_t actualSizeToRead = (bytesRequested < bytesLeft) ? bytesRequested : bytesLeft;
00037 
00038                 if (actualSizeToRead > 0) {
00039                     // Copy the data
00040                     memcpy(ptr, self->_curPtr, actualSizeToRead);
00041                     // Increase the local pointer
00042                     self->_curPtr += actualSizeToRead;
00043                 }
00044 
00045                 // Return how much we read (in the same way fread would)
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                 // Goto where we wish to seek to
00054                 switch (whence) {
00055                 case SEEK_SET:
00056                     // Seek to the start of the data file
00057                     self->_curPtr = self->_source.buffer + offset;
00058                     break;
00059                 case SEEK_CUR:
00060                     // Seek relatively to the current position
00061                     self->_curPtr += offset;
00062                     break;
00063                 case SEEK_END:
00064                     // Seek the end of the file
00065                     self->_curPtr = self->_source.buffer + self->_source.length;
00066                     break;
00067                 default:
00068                     // Unknown SEEK case
00069                     break;
00070                 };
00071 
00072                 // Clamp the curPtr to allowed values
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 } // namespace sound
00093 
00094 #endif /*OGGFILESTREAM_H_*/

Generated by  doxygen 1.6.2