22 using std::istringstream;
24 using std::ostringstream;
32 compress_string(
const string &source,
int compression_level) {
36 OCompressStream compress;
37 compress.open(&dest,
false, compression_level);
38 compress.write(source.data(), source.length());
40 if (compress.fail()) {
56 decompress_string(
const string &source) {
57 istringstream source_stream(source);
58 ostringstream dest_stream;
60 if (!decompress_stream(source_stream, dest_stream)) {
64 return dest_stream.str();
73 EXPCL_PANDA_EXPRESS
bool 74 compress_file(
const Filename &source,
const Filename &dest,
int compression_level) {
81 istream *source_stream = vfs->
open_read_file(source_filename,
false);
82 if (source_stream ==
nullptr) {
83 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
87 Filename dest_filename = Filename::binary_filename(dest);
88 ostream *dest_stream = vfs->
open_write_file(dest_filename,
false,
true);
89 if (dest_stream ==
nullptr) {
90 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
95 bool result = compress_stream(*source_stream, *dest_stream, compression_level);
110 EXPCL_PANDA_EXPRESS
bool 112 Filename source_filename = Filename::binary_filename(source);
114 istream *source_stream = vfs->
open_read_file(source_filename,
false);
115 if (source_stream ==
nullptr) {
116 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
125 ostream *dest_stream = vfs->
open_write_file(dest_filename,
false,
true);
126 if (dest_stream ==
nullptr) {
127 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
132 bool result = decompress_stream(*source_stream, *dest_stream);
145 compress_stream(istream &source, ostream &dest,
int compression_level) {
146 OCompressStream compress;
147 compress.open(&dest,
false, compression_level);
149 static const size_t buffer_size = 4096;
150 char buffer[buffer_size];
152 source.read(buffer, buffer_size);
153 size_t count = source.gcount();
155 compress.write(buffer, count);
156 source.read(buffer, buffer_size);
157 count = source.gcount();
161 return (!source.fail() || source.eof()) && (!compress.fail());
174 decompress_stream(istream &source, ostream &dest) {
175 IDecompressStream decompress(&source,
false);
177 static const size_t buffer_size = 4096;
178 char buffer[buffer_size];
180 decompress.read(buffer, buffer_size);
181 size_t count = decompress.gcount();
183 dest.write(buffer, count);
184 decompress.read(buffer, buffer_size);
185 count = decompress.gcount();
188 return (!decompress.fail() || decompress.eof()) && (!dest.fail());
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
std::istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read...
void set_binary()
Indicates that the filename represents a binary file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void close_read_file(std::istream *stream)
Closes a file opened by a previous call to open_read_file().
The name of a file, such as a texture file or an Egg file.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
static void close_write_file(std::ostream *stream)
Closes a file opened by a previous call to open_write_file().
std::ostream * open_write_file(const Filename &filename, bool auto_wrap, bool truncate)
Convenience function; returns a newly allocated ostream if the file exists and can be written...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_binary_or_text() const
Returns true either is_binary() or is_text() is true; that is, that the filename has been specified a...