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