00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LEMUR_FILE_HPP
00020 #define LEMUR_FILE_HPP
00021
00022 #include <vector>
00023 #include <sstream>
00024 #include <fstream>
00025 #include <assert.h>
00026 #include "lemur-platform.h"
00027 #include "lemur-compat.hpp"
00028
00032
00033 class File {
00034 public:
00035 typedef INT64 offset_type;
00036 typedef FILE_OFFSET library_offset_type;
00037
00038 private:
00039 struct FileSegment {
00040 std::fstream stream;
00041 offset_type start;
00042 offset_type end;
00043
00044 bool contains( offset_type position ) {
00045 return start <= position && end > position;
00046 }
00047
00048 bool before( offset_type position ) {
00049 return end <= position;
00050 }
00051 };
00052
00053 std::string _fileName;
00054 std::vector<FileSegment*> _segments;
00055
00056 FileSegment* _readSegment;
00057 FileSegment* _writeSegment;
00058 offset_type _readPosition;
00059 offset_type _writePosition;
00060 offset_type _readCount;
00061 bool _readPointerValid;
00062 bool _writePointerValid;
00063 int _mode;
00064 int _state;
00065
00066 static std::string segmentName( const std::string& fileName, int segment );
00067 void _appendSegment();
00068
00069 offset_type _absolutePosition( offset_type relativePosition,
00070 offset_type currentPosition,
00071 std::fstream::seekdir direction ) const;
00072 FileSegment* _segmentForPosition( offset_type absolutePosition, FileSegment* guess );
00073 void _validateReadPointer();
00074 void _validateWritePointer();
00075
00076 public:
00077 File();
00078 ~File();
00079 void open( const std::string& fileName, int mode );
00080 void close();
00081 void read( void* buffer, offset_type count );
00082 void write( const void* buffer, offset_type count );
00083 void seekg( offset_type relativePosition, std::fstream::seekdir direction );
00084 void seekp( offset_type relativePosition, std::fstream::seekdir direction ) ;
00085 offset_type tellg();
00086 offset_type tellp();
00087 offset_type gcount();
00088
00089
00090 int rdstate();
00091 offset_type size() const;
00092 void unlink();
00093
00094 static void unlink( const std::string& fileName );
00095 static void rename( const std::string& oldName, const std::string& newName );
00096 };
00097
00098 #endif // LEMUR_FILE_HPP