Panda3D
|
00001 // Filename: parse_vrml.cxx 00002 // Created by: drose (01Oct04) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 /************************************************** 00016 * VRML 2.0, Draft 2 Parser 00017 * Copyright (C) 1996 Silicon Graphics, Inc. 00018 * 00019 * Author(s) : Gavin Bell 00020 * Daniel Woods (first port) 00021 ************************************************** 00022 */ 00023 00024 #include "pandatoolbase.h" 00025 00026 #include "parse_vrml.h" 00027 #include "vrmlParserDefs.h" 00028 #include "vrmlNodeType.h" 00029 #include "vrmlNode.h" 00030 #include "standard_nodes.h" 00031 #include "zStream.h" 00032 #include "virtualFileSystem.h" 00033 00034 extern int vrmlyyparse(); 00035 extern void vrmlyyResetLineNumber(); 00036 extern int vrmlyydebug; 00037 extern int vrmlyy_flex_debug; 00038 00039 extern VrmlScene *parsed_scene; 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: get_standard_nodes 00043 // Description: Loads the set of standard VRML node definitions into 00044 // the parser, if it has not already been loaded. 00045 //////////////////////////////////////////////////////////////////// 00046 static bool 00047 get_standard_nodes() { 00048 static bool got_standard_nodes = false; 00049 static bool read_ok = true; 00050 if (got_standard_nodes) { 00051 return read_ok; 00052 } 00053 00054 // The standardNodes.wrl file has been compiled into this binary. 00055 // Extract it out. 00056 00057 string data((const char *)standard_nodes_data, standard_nodes_data_len); 00058 00059 #ifdef HAVE_ZLIB 00060 // The data is stored compressed; decompress it on-the-fly. 00061 istringstream inz(data); 00062 IDecompressStream in(&inz, false); 00063 00064 #else 00065 // The data is stored uncompressed, so just load it. 00066 istringstream in(data); 00067 #endif // HAVE_ZLIB 00068 00069 vrml_init_parser(in, "standardNodes.wrl"); 00070 if (vrmlyyparse() != 0) { 00071 read_ok = false; 00072 } 00073 vrml_cleanup_parser(); 00074 00075 got_standard_nodes = true; 00076 return read_ok; 00077 } 00078 00079 //////////////////////////////////////////////////////////////////// 00080 // Function: parse_vrml 00081 // Description: Reads the named VRML file and returns a corresponding 00082 // VrmlScene, or NULL if there is a parse error. 00083 //////////////////////////////////////////////////////////////////// 00084 VrmlScene * 00085 parse_vrml(Filename filename) { 00086 filename.set_text(); 00087 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00088 istream *in = vfs->open_read_file(filename, true); 00089 if (in == (istream *)NULL) { 00090 nout << "Cannot open " << filename << " for reading.\n"; 00091 return NULL; 00092 } 00093 VrmlScene *result = parse_vrml(*in, filename); 00094 vfs->close_read_file(in); 00095 return result; 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: parse_vrml 00100 // Description: Reads the indicated input stream and returns a corresponding 00101 // VrmlScene, or NULL if there is a parse error. 00102 //////////////////////////////////////////////////////////////////// 00103 VrmlScene * 00104 parse_vrml(istream &in, const string &filename) { 00105 if (!get_standard_nodes()) { 00106 cerr << "Internal error--unable to parse VRML.\n"; 00107 return NULL; 00108 } 00109 00110 VrmlScene *scene = NULL; 00111 VrmlNodeType::pushNameSpace(); 00112 00113 vrml_init_parser(in, filename); 00114 if (vrmlyyparse() == 0) { 00115 scene = parsed_scene; 00116 } 00117 vrml_cleanup_parser(); 00118 00119 VrmlNodeType::popNameSpace(); 00120 00121 return scene; 00122 } 00123 00124 #if 0 00125 int 00126 main(int argc, char *argv[]) { 00127 if (argc < 2) { 00128 cerr << "parse_vrml filename.wrl\n"; 00129 exit(1); 00130 } 00131 00132 VrmlScene *scene = parse_vrml(argv[1]); 00133 if (scene == (VrmlScene *)NULL) { 00134 exit(1); 00135 } 00136 00137 cout << *scene << "\n"; 00138 return (0); 00139 } 00140 #endif