00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00043
00044
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
00055
00056
00057 string data((const char *)standard_nodes_data, standard_nodes_data_len);
00058
00059 #ifdef HAVE_ZLIB
00060
00061 istringstream inz(data);
00062 IDecompressStream in(&inz, false);
00063
00064 #else
00065
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
00081
00082
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
00100
00101
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