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 namespace lemur
00029 {
00031 namespace file
00032 {
00033
00037
00038 class File {
00039 public:
00040 typedef INT64 offset_type;
00041 typedef FILE_OFFSET library_offset_type;
00042
00043 private:
00044 struct FileSegment {
00045 std::fstream stream;
00046 offset_type start;
00047 offset_type end;
00048
00049 bool contains( offset_type position ) {
00050 return start <= position && end > position;
00051 }
00052
00053 bool before( offset_type position ) {
00054 return end <= position;
00055 }
00056 };
00057
00058 std::string _fileName;
00059 std::vector<FileSegment*> _segments;
00060
00061 FileSegment* _readSegment;
00062 FileSegment* _writeSegment;
00063 offset_type _readPosition;
00064 offset_type _writePosition;
00065 offset_type _readCount;
00066 bool _readPointerValid;
00067 bool _writePointerValid;
00068 int _mode;
00069 int _state;
00070
00071 static std::string segmentName( const std::string& fileName, int segment );
00072 void _appendSegment();
00073
00074 offset_type _absolutePosition( offset_type relativePosition,
00075 offset_type currentPosition,
00076 std::fstream::seekdir direction ) const;
00077 FileSegment* _segmentForPosition( offset_type absolutePosition, FileSegment* guess );
00078 void _validateReadPointer();
00079 void _validateWritePointer();
00080
00081 public:
00082 File();
00083 ~File();
00084 void open( const std::string& fileName, int mode );
00085 void close();
00086 void read( void* buffer, offset_type count );
00087 void write( const void* buffer, offset_type count );
00088 void seekg( offset_type relativePosition, std::fstream::seekdir direction );
00089 void seekp( offset_type relativePosition, std::fstream::seekdir direction ) ;
00090 offset_type tellg();
00091 offset_type tellp();
00092 offset_type gcount();
00093
00094
00095 int rdstate();
00096 offset_type size() const;
00097 void unlink();
00098
00099 static void unlink( const std::string& fileName );
00100 static void rename( const std::string& oldName, const std::string& newName );
00101 };
00102 }
00103 }
00104
00105 #endif // LEMUR_FILE_HPP