00001 /*========================================================================== 00002 * Copyright (c) 2003 University of Massachusetts. All Rights Reserved. 00003 * 00004 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval 00005 * is subject to the terms of the software license set forth in the LICENSE 00006 * file included with this software, and also available at 00007 * http://www.lemurproject.org/license.html 00008 * 00009 *========================================================================== 00010 */ 00011 00012 00013 // 00014 // ReadBuffer 00015 // 00016 // tds - 13 November 2003 00017 // 00018 00019 #ifndef LEMUR_READBUFFER_HPP 00020 #define LEMUR_READBUFFER_HPP 00021 #include "File.hpp" 00022 namespace lemur 00023 { 00024 namespace file 00025 { 00026 class ReadBuffer { 00027 private: 00028 File::offset_type _filePosition; 00029 char* _buffer; 00030 File& _file; 00031 size_t _bufferSize; 00032 size_t _bufferPosition; 00033 size_t _bufferDataLength; 00034 bool _gValid; 00035 bool _exclusiveAccess; 00036 00037 public: 00038 // wrap <file> in a ReadBuffer with initial 00039 // buffer size <bufferSize>. The buffer 00040 // may grow if necessary to support large 00041 // peek() and read() requests. 00042 // <exclusiveAccess> = true if the ReadBuffer is 00043 // the only user of the underlying file, false otherwise. 00044 ReadBuffer( File& file, size_t bufferSize, bool exclusiveAccess = true ); 00045 ~ReadBuffer(); 00046 00047 // standard read() semantics; will perform 00048 // an unbuffered read if the read size is 00049 // large enough 00050 void read( char* data, size_t length ); 00051 00052 // return a pointer to a buffer containing 00053 // the next <length> bytes, but do not advance 00054 // the read pointer 00055 const char* peek( size_t length ); 00056 00057 // return a pointer to a buffer containing 00058 // the next <length> bytes, and advance 00059 // the read pointer 00060 const char* read( size_t length ); 00061 00062 // move the internal read pointer 00063 // to <position>, which may be relative to the 00064 // beginning, end or current position of the file 00065 // as specified by <direction>. 00066 void seekg( File::offset_type position, std::fstream::seekdir direction ); 00067 00068 // return the read pointer location with 00069 // reference to the beginning of the file 00070 File::offset_type tellg(); 00071 00072 // return the current read state bits, 00073 // eofbit, badbit, etc. (same 00074 // semantics as std::ifstream::rdstate()) 00075 int rdstate(); 00076 00077 // marks the underlying stream pointer 00078 // as invalid--call this if you seek 00079 // the underlying file to a new location 00080 void invalidateg(); 00081 }; 00082 } 00083 } 00084 00085 #endif // LEMUR_READBUFFER_HPP