From fcec03faef17639b05ae13e60b7343e93a2ab3fd Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 6 Aug 2002 12:57:33 +0000 Subject: [PATCH] change read_subfile() and read_file() interfaces --- panda/src/downloader/downloadDb.h | 20 +++---- panda/src/express/multifile.I | 13 +++++ panda/src/express/multifile.cxx | 48 +++++++-------- panda/src/express/multifile.h | 6 +- panda/src/express/ordered_vector.h | 1 + panda/src/putil/virtualFile.I | 12 ++++ panda/src/putil/virtualFile.cxx | 90 ++++++++++++++--------------- panda/src/putil/virtualFile.h | 10 +++- panda/src/putil/virtualFileSystem.I | 82 +++++++++++++++----------- panda/src/putil/virtualFileSystem.h | 8 ++- 10 files changed, 168 insertions(+), 122 deletions(-) diff --git a/panda/src/downloader/downloadDb.h b/panda/src/downloader/downloadDb.h index 38a343a943..f363def1a8 100644 --- a/panda/src/downloader/downloadDb.h +++ b/panda/src/downloader/downloadDb.h @@ -17,22 +17,20 @@ //////////////////////////////////////////////////////////////////// #ifndef DOWNLOADDB_H #define DOWNLOADDB_H -// -//////////////////////////////////////////////////////////////////// -// Includes -//////////////////////////////////////////////////////////////////// -#include -#include -#include -#include + +#include "pandabase.h" +#include "notify.h" +#include "filename.h" +#include "multifile.h" +#include "datagram.h" +#include "datagramIterator.h" #include "pvector.h" -#include -#include +#include "pointerTo.h" #include "pmap.h" #include "hashVal.h" -#include +#include "buffer.h" /* ////////////////////////////////////////////////// diff --git a/panda/src/express/multifile.I b/panda/src/express/multifile.I index df0b926ad4..fcc06edfba 100644 --- a/panda/src/express/multifile.I +++ b/panda/src/express/multifile.I @@ -74,6 +74,19 @@ get_scale_factor() const { return _new_scale_factor; } +//////////////////////////////////////////////////////////////////// +// Function: Multifile::read_subfile +// Access: Published +// Description: Returns a string that contains the entire contents of +// the indicated subfile. +//////////////////////////////////////////////////////////////////// +INLINE string Multifile:: +read_subfile(int index) { + string result; + read_subfile(index, result); + return result; +} + //////////////////////////////////////////////////////////////////// // Function: Multifile::word_to_streampos // Access: Private diff --git a/panda/src/express/multifile.cxx b/panda/src/express/multifile.cxx index d985b1a064..e743094802 100644 --- a/panda/src/express/multifile.cxx +++ b/panda/src/express/multifile.cxx @@ -21,6 +21,7 @@ #include "config_express.h" #include "streamWriter.h" #include "streamReader.h" +#include "datagram.h" #include "zStream.h" #include @@ -714,29 +715,6 @@ get_subfile_compressed_length(int index) const { return _subfiles[index]->_data_length; } -//////////////////////////////////////////////////////////////////// -// Function: Multifile::read_subfile -// Access: Published -// Description: Fills the indicated Datagram with the data from the -// nth subfile. -//////////////////////////////////////////////////////////////////// -void Multifile:: -read_subfile(int index, Datagram &data) { - nassertv(is_read_valid()); - nassertv(index >= 0 && index < (int)_subfiles.size()); - data.clear(); - - istream *in = open_read_subfile(index); - int byte = in->get(); - while (!in->eof() && !in->fail()) { - data.add_int8(byte); - byte = in->get(); - } - bool failed = in->fail(); - delete in; - nassertv(!failed); -} - //////////////////////////////////////////////////////////////////// // Function: Multifile::extract_subfile // Access: Published @@ -786,6 +764,30 @@ ls(ostream &out) const { } } +//////////////////////////////////////////////////////////////////// +// Function: Multifile::read_subfile +// Access: Public +// Description: Fills a string with the entire contents of +// the indicated subfile. +//////////////////////////////////////////////////////////////////// +bool Multifile:: +read_subfile(int index, string &result) { + nassertr(is_read_valid(), false); + nassertr(index >= 0 && index < (int)_subfiles.size(), false); + result = string(); + + istream *in = open_read_subfile(index); + int byte = in->get(); + while (!in->eof() && !in->fail()) { + result += (char)byte; + byte = in->get(); + } + bool failed = in->fail(); + delete in; + nassertr(!failed, false); + return true; +} + //////////////////////////////////////////////////////////////////// // Function: Multifile::open_read // Access: Public diff --git a/panda/src/express/multifile.h b/panda/src/express/multifile.h index 1b23efdadf..ed904cc9b9 100644 --- a/panda/src/express/multifile.h +++ b/panda/src/express/multifile.h @@ -21,8 +21,6 @@ #include "pandabase.h" -#include "datagram.h" -#include "datagramIterator.h" #include "subStream.h" #include "filename.h" #include "ordered_vector.h" @@ -73,13 +71,15 @@ PUBLISHED: bool is_subfile_compressed(int index) const; size_t get_subfile_compressed_length(int index) const; - void read_subfile(int index, Datagram &datagram); + INLINE string read_subfile(int index); bool extract_subfile(int index, const Filename &filename); void output(ostream &out) const; void ls(ostream &out = cout) const; public: + bool read_subfile(int index, string &result); + // Special interfaces to work with iostreams, not necessarily files. bool open_read(istream *multifile_stream); bool open_write(ostream *multifile_stream); diff --git a/panda/src/express/ordered_vector.h b/panda/src/express/ordered_vector.h index 80ed9e96f3..37eb890efa 100644 --- a/panda/src/express/ordered_vector.h +++ b/panda/src/express/ordered_vector.h @@ -23,6 +23,7 @@ #include "pvector.h" #include "pset.h" +#include "notify.h" #include // There are some inheritance issues with template classes and typedef diff --git a/panda/src/putil/virtualFile.I b/panda/src/putil/virtualFile.I index a4fdf39dc5..a803a63e5d 100644 --- a/panda/src/putil/virtualFile.I +++ b/panda/src/putil/virtualFile.I @@ -26,6 +26,18 @@ INLINE VirtualFile:: VirtualFile() { } +//////////////////////////////////////////////////////////////////// +// Function: VirtualFile::read_file +// Access: Public +// Description: Returns the entire contents of the file as a string. +//////////////////////////////////////////////////////////////////// +INLINE string VirtualFile:: +read_file() const { + string result; + read_file(result); + return result; +} + INLINE ostream & operator << (ostream &out, const VirtualFile &file) { diff --git a/panda/src/putil/virtualFile.cxx b/panda/src/putil/virtualFile.cxx index b43501125a..b23152f93c 100644 --- a/panda/src/putil/virtualFile.cxx +++ b/panda/src/putil/virtualFile.cxx @@ -45,51 +45,6 @@ is_regular_file() const { return false; } -//////////////////////////////////////////////////////////////////// -// Function: VirtualFile::read_file -// Access: Published -// Description: Fills up the indicated Datagram with the contents of -// the file, if it is a regular file. Returns true on -// success, false otherwise. -//////////////////////////////////////////////////////////////////// -bool VirtualFile:: -read_file(Datagram &data) const { - data.clear(); - - istream *in = open_read_file(); - if (in == (istream *)NULL) { - util_cat.info() - << "Unable to read " << get_filename() << "\n"; - return false; - } - int byte = in->get(); - while (!in->eof() && !in->fail()) { - data.add_int8(byte); - byte = in->get(); - } - bool failed = in->fail() && !in->eof(); - delete in; - - if (failed) { - util_cat.info() - << "Error while reading " << get_filename() << "\n"; - } - return !failed; -} - -//////////////////////////////////////////////////////////////////// -// Function: VirtualFile::open_read_file -// Access: Published, Virtual -// Description: Opens the file for reading. Returns a newly -// allocated istream on success (which you should -// eventually delete when you are done reading). -// Returns NULL on failure. -//////////////////////////////////////////////////////////////////// -istream *VirtualFile:: -open_read_file() const { - return NULL; -} - //////////////////////////////////////////////////////////////////// // Function: VirtualFile::scan_directory // Access: Published @@ -194,6 +149,51 @@ ls_all(ostream &out) const { } } +//////////////////////////////////////////////////////////////////// +// Function: VirtualFile::read_file +// Access: Public +// Description: Fills up the indicated string with the contents of +// the file, if it is a regular file. Returns true on +// success, false otherwise. +//////////////////////////////////////////////////////////////////// +bool VirtualFile:: +read_file(string &result) const { + result = string(); + + istream *in = open_read_file(); + if (in == (istream *)NULL) { + util_cat.info() + << "Unable to read " << get_filename() << "\n"; + return false; + } + int byte = in->get(); + while (!in->eof() && !in->fail()) { + result += (char)byte; + byte = in->get(); + } + bool failed = in->fail() && !in->eof(); + delete in; + + if (failed) { + util_cat.info() + << "Error while reading " << get_filename() << "\n"; + } + return !failed; +} + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFile::open_read_file +// Access: Public, Virtual +// Description: Opens the file for reading. Returns a newly +// allocated istream on success (which you should +// eventually delete when you are done reading). +// Returns NULL on failure. +//////////////////////////////////////////////////////////////////// +istream *VirtualFile:: +open_read_file() const { + return NULL; +} + //////////////////////////////////////////////////////////////////// // Function: VirtualFile::scan_local_directory // Access: Protected, Virtual diff --git a/panda/src/putil/virtualFile.h b/panda/src/putil/virtualFile.h index 2296069ffb..66c0baef41 100644 --- a/panda/src/putil/virtualFile.h +++ b/panda/src/putil/virtualFile.h @@ -46,15 +46,19 @@ PUBLISHED: virtual bool is_directory() const; virtual bool is_regular_file() const; - bool read_file(Datagram &data) const; - virtual istream *open_read_file() const; - PT(VirtualFileList) scan_directory() const; void output(ostream &out) const; void ls(ostream &out = cout) const; void ls_all(ostream &out = cout) const; + INLINE string read_file() const; + +public: + bool read_file(string &result) const; + virtual istream *open_read_file() const; + + protected: virtual bool scan_local_directory(VirtualFileList *file_list, const ov_set &mount_points) const; diff --git a/panda/src/putil/virtualFileSystem.I b/panda/src/putil/virtualFileSystem.I index 2f4a36fa3b..0c54243f47 100644 --- a/panda/src/putil/virtualFileSystem.I +++ b/panda/src/putil/virtualFileSystem.I @@ -52,40 +52,6 @@ is_regular_file(const Filename &filename) const { return (file != (VirtualFile *)NULL && file->is_regular_file()); } -//////////////////////////////////////////////////////////////////// -// Function: VirtualFileSystem::read_file -// Access: Published -// Description: Convenience function; fills the datagram up with the -// data from the indicated file, if it exists and can be -// read. Returns true on success, false otherwise. -//////////////////////////////////////////////////////////////////// -INLINE bool VirtualFileSystem:: -read_file(const Filename &filename, Datagram &data) const { - PT(VirtualFile) file = get_file(filename); - return (file != (VirtualFile *)NULL && file->read_file(data)); -} - -//////////////////////////////////////////////////////////////////// -// Function: VirtualFileSystem::open_read_file -// Access: Published -// Description: Convenience function; returns a newly allocated -// istream if the file exists and can be read, or NULL -// otherwise. Does not return an invalid istream. -//////////////////////////////////////////////////////////////////// -INLINE istream *VirtualFileSystem:: -open_read_file(const Filename &filename) const { - PT(VirtualFile) file = get_file(filename); - if (file == (VirtualFile *)NULL) { - return NULL; - } - istream *str = file->open_read_file(); - if (str != (istream *)NULL && str->fail()) { - delete str; - str = (istream *)NULL; - } - return str; -} - //////////////////////////////////////////////////////////////////// // Function: VirtualFileSystem::ls // Access: Published @@ -124,3 +90,51 @@ ls_all(const string &filename) const { file->ls_all(); } } + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFileSystem::read_file +// Access: Published +// Description: Convenience function; returns the entire contents of +// the indicated file as a string. +//////////////////////////////////////////////////////////////////// +INLINE string VirtualFileSystem:: +read_file(const Filename &filename) const { + string result; + bool okflag = read_file(filename, result); + nassertr(okflag, string()); + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFileSystem::read_file +// Access: Public +// Description: Convenience function; fills the string up with the +// data from the indicated file, if it exists and can be +// read. Returns true on success, false otherwise. +//////////////////////////////////////////////////////////////////// +INLINE bool VirtualFileSystem:: +read_file(const Filename &filename, string &result) const { + PT(VirtualFile) file = get_file(filename); + return (file != (VirtualFile *)NULL && file->read_file(result)); +} + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFileSystem::open_read_file +// Access: Public +// Description: Convenience function; returns a newly allocated +// istream if the file exists and can be read, or NULL +// otherwise. Does not return an invalid istream. +//////////////////////////////////////////////////////////////////// +INLINE istream *VirtualFileSystem:: +open_read_file(const Filename &filename) const { + PT(VirtualFile) file = get_file(filename); + if (file == (VirtualFile *)NULL) { + return NULL; + } + istream *str = file->open_read_file(); + if (str != (istream *)NULL && str->fail()) { + delete str; + str = (istream *)NULL; + } + return str; +} diff --git a/panda/src/putil/virtualFileSystem.h b/panda/src/putil/virtualFileSystem.h index a26cb93bb4..5cecf3f475 100644 --- a/panda/src/putil/virtualFileSystem.h +++ b/panda/src/putil/virtualFileSystem.h @@ -73,9 +73,6 @@ PUBLISHED: INLINE bool is_directory(const Filename &filename) const; INLINE bool is_regular_file(const Filename &filename) const; - INLINE bool read_file(const Filename &filename, Datagram &data) const; - INLINE istream *open_read_file(const Filename &filename) const; - INLINE void ls(const string &filename) const; INLINE void ls_all(const string &filename) const; @@ -83,7 +80,12 @@ PUBLISHED: static VirtualFileSystem *get_global_ptr(); + INLINE string read_file(const Filename &filename) const; + public: + INLINE bool read_file(const Filename &filename, string &result) const; + INLINE istream *open_read_file(const Filename &filename) const; + void scan_mount_points(vector_string &names, const Filename &path) const; private: