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");
105 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 106 fopen_s( &file_, filename.c_str(),
"r+b" );
108 file_ = fopen(filename.c_str(),
"r+b");
113 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 114 fopen_s( &
file_, filename.c_str(), mode.c_str() );
116 file_ = fopen(filename.c_str(), mode.c_str());
120 throw BagIOException((format(
"Error opening file: %1%") % filename.c_str()).str());
122 read_stream_ = boost::make_shared<UncompressedStream>(
this);
123 write_stream_ = boost::make_shared<UncompressedStream>(
this);
129 return feof(
file_) == 0 && ferror(
file_) == 0;
143 int success = fclose(
file_);
159 throw BagIOException(
"Can't set compression mode before opening a file");
164 stream->startWrite();
171 throw BagIOException(
"Can't set compression mode before opening a file");
187 int success = fseeko(
file_, offset, origin);
202 int fd = fileno(
file_);
203 return ftruncate(fd, length) == 0;
209 if(fgets(buffer, 1024,
file_))
220 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
bool good() const
return true if hasn't reached end-of-file and no error
void open(std::string const &filename, std::string const &mode)
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
void write(std::string const &s)
uint64_t getOffset() const
return current offset from the beginning of the file
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
void swap(Bag &a, Bag &b)
void swap(ChunkedFile &other)
uint32_t getCompressedBytesIn() const
return the number of bytes written to current compressed stream
uint64_t offset_
current position in the file
void setWriteMode(CompressionType type)
int nUnused_
number of bytes of extra data read by compressed stream
Exception thrown when on IO problems.
bool isOpen() const
return true if file is open for reading or writing
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)
std::string getFileName() const
return path of currently open file
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