39 #include <boost/format.hpp> 40 #include <boost/make_shared.hpp> 45 # define fseeko fseeko64 46 # define ftello ftello64 50 # define fseeko _fseeki64 51 # define ftello _ftelli64 52 # define fileno _fileno 53 # define ftruncate _chsize 90 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 91 fopen_s( &
file_, filename.c_str(),
"r" );
93 file_ = fopen(filename.c_str(),
"r");
97 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 98 fopen_s( &file_, filename.c_str(),
"w+b" );
100 file_ = fopen(filename.c_str(),
"w+b");
103 if (fclose(file_) != 0)
104 throw BagIOException((format(
"Error closing file: %1%") % filename.c_str()).str());
107 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 108 fopen_s( &file_, filename.c_str(),
"r+b" );
110 file_ = fopen(filename.c_str(),
"r+b");
115 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 116 fopen_s( &
file_, filename.c_str(), mode.c_str() );
118 file_ = fopen(filename.c_str(), mode.c_str());
122 throw BagIOException((format(
"Error opening file: %1%") % filename.c_str()).str());
124 read_stream_ = boost::make_shared<UncompressedStream>(
this);
125 write_stream_ = boost::make_shared<UncompressedStream>(
this);
131 return feof(
file_) == 0 && ferror(
file_) == 0;
145 int success = fclose(
file_);
161 throw BagIOException(
"Can't set compression mode before opening a file");
166 stream->startWrite();
173 throw BagIOException(
"Can't set compression mode before opening a file");
189 int success = fseeko(
file_, offset, origin);
204 int fd = fileno(
file_);
205 return ftruncate(fd, length) == 0;
211 if(fgets(buffer, 1024,
file_))
222 stream_factory_->getStream(compression)->decompress(dest, dest_len, source, source_len);
void openRead(std::string const &filename)
open file for reading
void setReadMode(CompressionType type)
void read(void *ptr, size_t size)
read size bytes from the file into ptr
void open(std::string const &filename, std::string const &mode)
bool isOpen() const
return true if file is open for reading or writing
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
void write(std::string const &s)
bool truncate(uint64_t length)
void seek(uint64_t offset, int origin=std::ios_base::beg)
seek to given offset from origin
void close()
close the file
bool good() const
return true if hasn't reached end-of-file and no error
void swap(Bag &a, Bag &b)
std::string getFileName() const
return path of currently open file
void swap(ChunkedFile &other)
uint64_t offset_
current position in the file
uint64_t getOffset() const
return current offset from the beginning of the file
void setWriteMode(CompressionType type)
uint32_t getCompressedBytesIn() const
return the number of bytes written to current compressed stream
int nUnused_
number of bytes of extra data read by compressed stream
Exception thrown when on IO problems.
boost::shared_ptr< StreamFactory > stream_factory_
boost::shared_ptr< Stream > write_stream_
static void setFile(Stream &a, ChunkedFile *file)
uint64_t compressed_in_
number of bytes written to current compressed stream
std::string filename_
path to file
void openWrite(std::string const &filename)
open file for writing
void decompress(CompressionType compression, uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
void openReadWrite(std::string const &filename)
open file for reading & writing
boost::shared_ptr< Stream > read_stream_
char * unused_
extra data read by compressed stream