Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

File.hpp

Go to the documentation of this file.
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 // File.hpp
00015 //
00016 // 1 December 2003 - tds
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; // seekg, read
00064       offset_type _writePosition; // seekp, write
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       // only does eof right now
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

Generated on Tue Jun 15 11:02:54 2010 for Lemur by doxygen 1.3.4