소스 검색

multifile::set_header_prefix

David Rose 16 년 전
부모
커밋
0c13f6a654
3개의 변경된 파일67개의 추가작업 그리고 0개의 파일을 삭제
  1. 30 0
      panda/src/express/multifile.I
  2. 33 0
      panda/src/express/multifile.cxx
  3. 4 0
      panda/src/express/multifile.h

+ 30 - 0
panda/src/express/multifile.I

@@ -377,6 +377,36 @@ get_magic_number() {
   return string(_header, _header_size);
   return string(_header, _header_size);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: Multifile::set_header_prefix
+//       Access: Published
+//  Description: Sets the string which is written to the Multifile
+//               before the Multifile header.  This string must begin
+//               with a hash mark and end with a newline character,
+//               and include at least 6 characters; and if it includes
+//               embedded newline characters, each one must be
+//               followed by a hash mark.
+//
+//               This is primarily useful as a simple hack to allow
+//               p3d applications to be run directly from the command
+//               line on Unix-like systems.
+////////////////////////////////////////////////////////////////////
+INLINE void Multifile::
+set_header_prefix(const string &header_prefix) {
+  _header_prefix = header_prefix;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: Multifile::get_header_prefix
+//       Access: Published
+//  Description: Returns the string that preceded the Multifile header
+//               on the file, if any.  See set_header_prefix().
+////////////////////////////////////////////////////////////////////
+INLINE const string &Multifile::
+get_header_prefix() const {
+  return _header_prefix;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: Multifile::word_to_streampos
 //     Function: Multifile::word_to_streampos
 //       Access: Private
 //       Access: Private

+ 33 - 0
panda/src/express/multifile.cxx

@@ -1535,6 +1535,38 @@ read_index() {
     close();
     close();
     return false;
     return false;
   }
   }
+
+  // Here's a special case: if the multifile begins with a hash
+  // character, then we skip at least 6 characters (the length of the
+  // original header string we already read), and continue reading and
+  // discarding lines of ASCII text, until we come across a nonempty
+  // line that does not begin with a hash character.  This allows a
+  // P3D application (which is a multifile) to be run directly on the
+  // command line on Unix-based systems.
+  _header_prefix = string();
+
+  if (this_header[0] == '#') {
+    _header_prefix = string(this_header, _header_size);
+    int ch = '#';
+
+    while (ch != EOF && ch == '#') {
+      // Skip to the end of the line.
+      while (ch != EOF && ch != '\n') {
+        _header_prefix += ch;
+        ch = read->get();
+      }
+      // Skip to the first non-whitespace character of the line.
+      while (ch != EOF && (isspace(ch) || ch == '\r')) {
+        _header_prefix += ch;
+        ch = read->get();
+      }
+    }
+
+    // Now fill up the header.
+    this_header[0] = ch;
+    read->read(this_header + 1, _header_size - 1);
+  }
+
   if (memcmp(this_header, _header, _header_size) != 0) {
   if (memcmp(this_header, _header, _header_size) != 0) {
     express_cat.info()
     express_cat.info()
       << _multifile_name << " is not a Multifile.\n";
       << _multifile_name << " is not a Multifile.\n";
@@ -1657,6 +1689,7 @@ write_header() {
 
 
   nassertr(_write != (ostream *)NULL, false);
   nassertr(_write != (ostream *)NULL, false);
   nassertr(_write->tellp() == (streampos)0, false);
   nassertr(_write->tellp() == (streampos)0, false);
+  _write->write(_header_prefix.data(), _header_prefix.size());
   _write->write(_header, _header_size);
   _write->write(_header, _header_size);
   StreamWriter writer(_write, false);
   StreamWriter writer(_write, false);
   writer.add_int16(_current_major_ver);
   writer.add_int16(_current_major_ver);

+ 4 - 0
panda/src/express/multifile.h

@@ -114,6 +114,9 @@ PUBLISHED:
 
 
   static INLINE string get_magic_number();
   static INLINE string get_magic_number();
 
 
+  INLINE void set_header_prefix(const string &header_prefix);
+  INLINE const string &get_header_prefix() const;
+
 public:
 public:
   bool read_subfile(int index, string &result);
   bool read_subfile(int index, string &result);
   bool read_subfile(int index, pvector<unsigned char> &result);
   bool read_subfile(int index, pvector<unsigned char> &result);
@@ -199,6 +202,7 @@ private:
   pfstream _read_write_file;
   pfstream _read_write_file;
   StreamWrapper _read_write_filew;
   StreamWrapper _read_write_filew;
   Filename _multifile_name;
   Filename _multifile_name;
+  string _header_prefix;
 
 
   int _file_major_ver;
   int _file_major_ver;
   int _file_minor_ver;
   int _file_minor_ver;