basic/geometry/VRML_20_Reader.cpp

Go to the documentation of this file.
00001 #include <additional\streams\mystreams.h>
00002 #include <assert.h>
00003 #include "debug/debug.h"
00004 #include <stdio.h>
00005 #include "geometry/IGS/IGS_Object.h"
00006 #include <geometry/vrml_reader/keyword.h>
00007 #include <geometry/vrml_reader/utility.h>
00008 #include <geometry/vrml_reader/VrmlStack.h>
00009 #ifdef WIN32
00010     #include <direct.h>
00011 #endif
00012 #include <fstream>
00013 #include "geometry\VRML_20_Reader.h"
00014 #include <geometry/vrml_reader/vrmlinline.h>
00015 
00016 //char VRML_20_Reader::rootpath[ 256 ] = "";
00017 VRML_20_Reader::VRML_20_Reader()
00018 {
00019         rootpath[0] = '\0';
00020 }
00021 
00022 VRML_20_Reader::~VRML_20_Reader()
00023 {
00024   //## begin VRML_20_Reader::~VRML_20_Reader%.body preserve=yes
00025   //## end VRML_20_Reader::~VRML_20_Reader%.body
00026 }
00027 
00028 void VRML_20_Reader::SetRootPath( const char* path )
00029 {
00030     strcpy( rootpath, path );
00031 }
00032 
00033 void GetPath( const char* file, char* path )
00034 {
00035     const char* p1 = file ;
00036     while( strchr( p1, '/' ) != NULL )
00037     {
00038         p1 = strchr( p1, '/' );
00039         p1++;
00040     }
00041 
00042     const char* p2 = file ;
00043     while( strchr( p2, '\\' ) != NULL )
00044     {
00045         p2 = strchr( p2, '\\' );
00046         p2++;
00047     }
00048 
00049     const char* position;
00050     if( p1 > p2 )
00051     {
00052         position = p1;
00053     }
00054     else
00055     {
00056         position = p2;
00057     }
00058 
00059     int number = position - file;
00060     strncpy( path, file, number );
00061 }
00062 
00063 //## Other Operations (implementation)
00064 bool VRML_20_Reader::Read ()
00065 {
00066     char path[ 256 ] = "";
00067     GetPath( filename.c_str(), path );
00068     char wholePath[ 256 ] = "";
00069     strcat( wholePath, rootpath );
00070     strcat( wholePath, path );
00071     VrmlInline::SetRootPath( wholePath );
00072 
00073         //open the file
00074     char filen[ 256 ] = "";
00075     strcat( filen, rootpath );
00076     strcat( filen, filename.c_str() );
00077         FILE* file = fopen( filen, "rb" );
00078         IJG_Assert( file != NULL );
00079         if (file == NULL)
00080                 return false;
00081 
00082         long start = ftell( file );
00083         int success = fseek( file, 0, SEEK_END );
00084         long end = ftell( file );
00085         rewind( file );
00086         assert( success == 0 );
00087         int size = end - start;
00088         char* buffer = new char[ size + 1 ];
00089         memset( buffer, 0x00, size + 1 );
00090         char* originalBuffer = buffer;
00091         fread( buffer, 1, size, file );
00092         success = fclose( file );
00093         assert( success == 0 );
00094 
00095         char* endOfBuffer = buffer + size;
00096 
00097         //now the VRML file is loaded into a buffer
00098         VrmlStack stack;
00099 
00100 	::RemoveAllCommentsFromBuffer( buffer, size );
00101         while( buffer < endOfBuffer )
00102         {
00103                 Keyword keyword = VrmlNode::GetKeyword( buffer );
00104 
00105                 //we must process define keywords differently
00106                 EatWhite( buffer );
00107                 if( keyword != VRML_Define )
00108                 {
00109                         //parse the node
00110                         EatWhite( buffer );
00111                         buffer++;
00112                 }
00113 
00114                 VrmlNode* node = VrmlNode::ParseNode( keyword, buffer );
00115                 if( node != NULL )
00116                 {
00117                         node->AddRef();
00118                         m_Nodes.push_back( node );
00119 
00120                         IGS_Object* object = node->RenderToIGS( stack );
00121                         if( object != NULL )
00122                         {
00123                                 //IAN IMPROVE: this should be calling a member function in the parent
00124                                 m_Objects.push_back( object );
00125                         }
00126                 }
00127                 else
00128                 {
00129                         assert( node != NULL );
00130                 }
00131                 EatWhite( buffer );
00132         }
00133 
00134         int i;
00135         size = m_Nodes.size();
00136         for( i = 0; i < size; i++ )
00137         {
00138                 VrmlNode* node = m_Nodes[ i ];
00139         }
00140         delete[] originalBuffer;
00141         originalBuffer = NULL;
00142 
00143         //return to the original directory
00144 //      int returnValue = ::chdir( directory );
00145 //      assert( returnValue == 0 );
00146 
00147         size = m_Nodes.size();
00148         for( i = 0; i < size; i++ )
00149         {
00150                 VrmlNode* node = m_Nodes[ i ];
00151                 node->Release();
00152         }
00153         m_Nodes.clear();
00154         VrmlNode::CleanupGlobals();
00155         return true;                                    //IAN IMPROVE: is true the right thing to return all the time?
00156 }
00157 
00158 // Additional Declarations
00159   //## begin VRML_20_Reader%37CC77810341.declarations preserve=yes
00160   //## end VRML_20_Reader%37CC77810341.declarations
00161 
00162 //## begin module%37CC77810341.epilog preserve=yes
00163 //## end module%37CC77810341.epilog

Generated on Sat Apr 1 21:30:33 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO