Panda3D
parse_vrml.cxx
1 // Filename: parse_vrml.cxx
2 // Created by: drose (01Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 /**************************************************
16  * VRML 2.0, Draft 2 Parser
17  * Copyright (C) 1996 Silicon Graphics, Inc.
18  *
19  * Author(s) : Gavin Bell
20  * Daniel Woods (first port)
21  **************************************************
22  */
23 
24 #include "pandatoolbase.h"
25 
26 #include "parse_vrml.h"
27 #include "vrmlParserDefs.h"
28 #include "vrmlNodeType.h"
29 #include "vrmlNode.h"
30 #include "standard_nodes.h"
31 #include "zStream.h"
32 #include "virtualFileSystem.h"
33 
34 extern int vrmlyyparse();
35 extern void vrmlyyResetLineNumber();
36 extern int vrmlyydebug;
37 extern int vrmlyy_flex_debug;
38 
39 extern VrmlScene *parsed_scene;
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: get_standard_nodes
43 // Description: Loads the set of standard VRML node definitions into
44 // the parser, if it has not already been loaded.
45 ////////////////////////////////////////////////////////////////////
46 static bool
47 get_standard_nodes() {
48  static bool got_standard_nodes = false;
49  static bool read_ok = true;
50  if (got_standard_nodes) {
51  return read_ok;
52  }
53 
54  // The standardNodes.wrl file has been compiled into this binary.
55  // Extract it out.
56 
57  string data((const char *)standard_nodes_data, standard_nodes_data_len);
58 
59 #ifdef HAVE_ZLIB
60  // The data is stored compressed; decompress it on-the-fly.
61  istringstream inz(data);
62  IDecompressStream in(&inz, false);
63 
64 #else
65  // The data is stored uncompressed, so just load it.
66  istringstream in(data);
67 #endif // HAVE_ZLIB
68 
69  vrml_init_parser(in, "standardNodes.wrl");
70  if (vrmlyyparse() != 0) {
71  read_ok = false;
72  }
73  vrml_cleanup_parser();
74 
75  got_standard_nodes = true;
76  return read_ok;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: parse_vrml
81 // Description: Reads the named VRML file and returns a corresponding
82 // VrmlScene, or NULL if there is a parse error.
83 ////////////////////////////////////////////////////////////////////
84 VrmlScene *
85 parse_vrml(Filename filename) {
86  filename.set_text();
88  istream *in = vfs->open_read_file(filename, true);
89  if (in == (istream *)NULL) {
90  nout << "Cannot open " << filename << " for reading.\n";
91  return NULL;
92  }
93  VrmlScene *result = parse_vrml(*in, filename);
94  vfs->close_read_file(in);
95  return result;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: parse_vrml
100 // Description: Reads the indicated input stream and returns a corresponding
101 // VrmlScene, or NULL if there is a parse error.
102 ////////////////////////////////////////////////////////////////////
103 VrmlScene *
104 parse_vrml(istream &in, const string &filename) {
105  if (!get_standard_nodes()) {
106  cerr << "Internal error--unable to parse VRML.\n";
107  return NULL;
108  }
109 
110  VrmlScene *scene = NULL;
111  VrmlNodeType::pushNameSpace();
112 
113  vrml_init_parser(in, filename);
114  if (vrmlyyparse() == 0) {
115  scene = parsed_scene;
116  }
117  vrml_cleanup_parser();
118 
119  VrmlNodeType::popNameSpace();
120 
121  return scene;
122 }
123 
124 #if 0
125 int
126 main(int argc, char *argv[]) {
127  if (argc < 2) {
128  cerr << "parse_vrml filename.wrl\n";
129  exit(1);
130  }
131 
132  VrmlScene *scene = parse_vrml(argv[1]);
133  if (scene == (VrmlScene *)NULL) {
134  exit(1);
135  }
136 
137  cout << *scene << "\n";
138  return (0);
139 }
140 #endif
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&#39;s file system.
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_text()
Indicates that the filename represents a text file.
Definition: filename.I:507
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.