Browse Source

change read_subfile() and read_file() interfaces

David Rose 23 years ago
parent
commit
fcec03faef

+ 9 - 11
panda/src/downloader/downloadDb.h

@@ -17,22 +17,20 @@
 ////////////////////////////////////////////////////////////////////
 #ifndef DOWNLOADDB_H
 #define DOWNLOADDB_H
-//
-////////////////////////////////////////////////////////////////////
-// Includes
-////////////////////////////////////////////////////////////////////
-#include <pandabase.h>
-#include <notify.h>
-#include <filename.h>
-#include <multifile.h>
+
+#include "pandabase.h"
+#include "notify.h"
+#include "filename.h"
+#include "multifile.h"
+#include "datagram.h"
+#include "datagramIterator.h"
 
 #include "pvector.h"
-#include <string>
-#include <pointerTo.h>
+#include "pointerTo.h"
 #include "pmap.h"
 
 #include "hashVal.h"
-#include <buffer.h>
+#include "buffer.h"
 
 /*
 //////////////////////////////////////////////////

+ 13 - 0
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

+ 25 - 23
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 <algorithm>
@@ -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

+ 3 - 3
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);

+ 1 - 0
panda/src/express/ordered_vector.h

@@ -23,6 +23,7 @@
 
 #include "pvector.h"
 #include "pset.h"
+#include "notify.h"
 #include <algorithm>
 
 // There are some inheritance issues with template classes and typedef

+ 12 - 0
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) {

+ 45 - 45
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

+ 7 - 3
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<string> &mount_points) const;

+ 48 - 34
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;
+}

+ 5 - 3
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: