00001 #include "odlparser.h" 00002 #include <algorithm> 00003 00004 ODLParser::ODLParser( const std::string &odldata ) : 00005 mODLData( odldata ) { 00006 ipos = jpos = 0; 00007 if ( odldata != "" ) 00008 parse( mTop ); 00009 } 00010 00011 bool ODLParser::parse( ODLTree &tree ) { 00012 std::string::size_type tpos1; 00013 std::string line; 00014 int objcnt = 0; 00015 int grpcnt = 0; 00016 00017 00018 const unsigned int DATALEN = mODLData.length(); 00019 bool retval; 00020 00021 jpos = ipos; 00022 retval = true; 00023 00024 while ( ipos < DATALEN-1 ) { 00025 // GET NEXT LINE 00026 // ODL STANDARD FORM EFFECTOR CONSTANTS ARE 00027 // CARRAIGE RETURN, NEW LINE, FORM FEED, 00028 // VERTICAL TAB 00029 tpos1 = mODLData.find( "\r", ipos ); 00030 00031 tpos1 = _uint_min( tpos1, mODLData.find( "\n", ipos ) ); 00032 tpos1 = _uint_min( tpos1, mODLData.find( "\f", ipos ) ); 00033 tpos1 = _uint_min( tpos1, mODLData.find( "\v", ipos ) ); 00034 jpos = _uint_min( tpos1, (DATALEN-1) ); 00035 line = trim( mODLData.substr( ipos, jpos-ipos ) ); 00036 ipos = jpos+1; 00037 00038 tpos1 = line.find( '=' ); 00039 if ( (int)tpos1 == -1 ) { 00040 std::string ustr = ucase( line ); 00041 if ( ustr == "END" || 00042 ustr == "END_GROUP" || 00043 ustr == "END_OBJECT" ) 00044 return true; 00045 retval = false; 00046 } else { 00047 std::string part1, part2; 00048 part1 = trim( line.substr( 0, tpos1 ) ); 00049 part2 = trim( line.substr( tpos1 + 1 ) ); 00050 00051 // ODL IS CASE INSENSITIVE SO WE'LL CONVERT ALL NON-LITERALS 00052 // TO UPPER CASE 00053 part1 = ucase( part1 ); 00054 00055 if ( part2.find( '\"' ) == 0 ) { 00056 while ( (int)part2.find( '\"', 1 ) == -1 ) { 00057 // MULTI LINE STRING -- GET THE REST 00058 tpos1 = mODLData.find( "\r", ipos ); 00059 tpos1 = _uint_min( tpos1, mODLData.find( "\n", ipos ) ); 00060 tpos1 = _uint_min( tpos1, mODLData.find( "\f", ipos ) ); 00061 tpos1 = _uint_min( tpos1, mODLData.find( "\v", ipos ) ); 00062 jpos = _uint_min( tpos1, (DATALEN-1) ); 00063 part2 += std::string(" ") + trim( mODLData.substr( ipos, jpos-ipos ) ); 00064 ipos = jpos+1; 00065 } 00066 } 00067 00068 if ( part1 == "GROUP" || 00069 part1 == "BEGIN_GROUP" ) { // PDS 00070 ODLTree child( part2, ODLGROUP ); 00071 tree.addChild( child ); 00072 grpcnt++; 00073 parse( *tree.getLastChild() ); 00074 } else if ( part1 == "OBJECT" || 00075 part1 == "BEGIN_OBJECT" ) { // PDS 00076 ODLTree child( part2, ODLOBJECT ); 00077 tree.addChild( child ); 00078 objcnt++; 00079 parse( *tree.getLastChild() ); 00080 } else if ( part1 == "END" || 00081 part1 == "END_OBJECT" || 00082 part1 == "END_GROUP" ) { 00083 return true; 00084 } else { 00085 ODLElement elem( part1, part2 ); 00086 tree.addElem( elem ); 00087 } 00088 } 00089 } 00090 return true; 00091 } 00092 00093 std::string ODLParser::trim( const std::string &str ) { 00094 std::string tstr; 00095 char *cstr; 00096 std::string::size_type ipos; 00097 std::string::size_type jpos; 00098 00099 cstr = (char*)str.c_str(); 00100 ipos = 0; 00101 while ( ipos < str.length()-1 && 00102 ( cstr[ipos] == '\r' || 00103 cstr[ipos] == '\n' || 00104 cstr[ipos] == '\f' || 00105 cstr[ipos] == '\v' || 00106 cstr[ipos] == ' ' || 00107 cstr[ipos] == '\t' ) ) { 00108 ipos++; 00109 } 00110 00111 jpos = str.length()-1; 00112 00113 while ( jpos > ipos && 00114 ( cstr[jpos] == '\r' || 00115 cstr[jpos] == '\n' || 00116 cstr[jpos] == '\f' || 00117 cstr[jpos] == '\v' || 00118 cstr[jpos] == ' ' || 00119 cstr[jpos] == '\t' ) ) { 00120 jpos--; 00121 } 00122 00123 return str.substr( ipos, jpos-ipos+1 ); 00124 } 00125 00126 std::string ODLParser::ucase( const std::string &str ) { 00127 std::string retstr = str; 00128 char *cstr = (char*)retstr.c_str(); 00129 00130 for ( int i=0; i < (int)str.length(); i++ ) 00131 cstr[i] = toupper( cstr[i] ); 00132 00133 return retstr; 00134 } 00135 00136 const ODLTree* ODLParser::getObject( const ODLTree& tree, 00137 const std::string &objname ) const { 00138 if ( tree.getName() == objname ) 00139 return(const ODLTree*)&tree; 00140 00141 for ( int iChld=0; iChld < tree.getNumChild(); iChld++ ) { 00142 const ODLTree *test = getObject( *tree.getChild( iChld ), objname ); 00143 if ( test ) 00144 return test; 00145 } 00146 00147 return NULL; 00148 } 00149 00150 const ODLTree* ODLParser::getGroup( const ODLTree&, 00151 const std::string & ) const { 00152 return NULL; 00153 } 00154 00155
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |