Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/chancfg/chanparse.cxx

Go to the documentation of this file.
00001 // Filename: chanparse.cxx
00002 // Created by:  cary (02Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "chanparse.h"
00020 #include <notify.h>
00021 
00022 const int ChanFileEOF = -1;
00023 
00024 static bool file_done;
00025 
00026 INLINE std::string ChanReadLine(istream& is) {
00027   if (is.eof() || is.fail())
00028 #ifdef BROKEN_EXCEPTIONS
00029     {
00030       file_done = true;
00031       return "";
00032     }
00033 #else
00034     throw ChanFileEOF;  // all done
00035 #endif /* BROKEN_EXCEPTIONS */
00036   std::string S;
00037   std::getline(is, S);
00038   size_t i = S.find_first_not_of(" /t/f/r/n");
00039   if ((i == std::string::npos) || (S[i] == ';'))
00040     return ChanReadLine(is);  // all white space or comment
00041   if (i != 0)
00042     S.erase(0, i);  // remove leading white space
00043   i = S.find_first_of(";");
00044   if (i != std::string::npos)
00045     S.erase(i, std::string::npos);  // remove trailing comment
00046   return S;
00047 }
00048 
00049 int ChanMatchingParen(std::string S) {
00050   int i = 1, j = 1;
00051   std::string::iterator k = S.begin();
00052 
00053   ++k;
00054   while ((k != S.end()) && (i != 0)) {
00055     switch (*k) {
00056     case '(':
00057       ++i;
00058       break;
00059     case ')':
00060       --i;
00061       break;
00062     }
00063     ++j;
00064     ++k;
00065   }
00066   if ((k == S.end()) && (i != 0))
00067     j = -1;
00068   return j;
00069 }
00070 
00071 void ChanParse(istream& is, ChanParseFunctor* p) {
00072   file_done = false;
00073 
00074   std::string S;
00075 #ifdef BROKEN_EXCEPTIONS
00076   S = ChanReadLine(is);
00077   if (file_done) {
00078     nout << "No data in file to parse" << endl;
00079     return;
00080   }
00081 #else
00082   try {
00083     S = ChanReadLine(is);
00084   }
00085   catch (...) {
00086     nout << "No data in file to parse" << endl;
00087     return;  // comment that there was no data in file to parse
00088   }
00089 #endif /* BROKEN_EXCEPTIONS */
00090 
00091   // now the main loop
00092 #ifdef BROKEN_EXCEPTIONS
00093   {
00094     int i;
00095     while (true) {
00096       if ((!S.empty()) && (S[0] != '(')) {
00097         // error, should have leading paren
00098         nout << "error, no leading paren in '" << S << "'" << endl;
00099         S.erase(0, S.find_first_of("("));
00100       }
00101       while ((S.empty())||(i = ChanMatchingParen(S)) == -1) {
00102         S += " ";
00103         S += ChanReadLine(is);
00104         if (file_done) {
00105           if (!S.empty() && (!(S == " ")))
00106             // error, trailing text usually indicates an error in the file
00107             nout << "error, trailing text in file S = '" << S << "'" << endl;
00108           return;
00109         }
00110         ChanEatFrontWhite(S);
00111       }
00112       (*p)(S.substr(1, i-2));
00113       S.erase(0, i);
00114       ChanEatFrontWhite(S);
00115     }
00116   }
00117 #else
00118   try {
00119     int i;
00120     while (true) {
00121       if ((!S.empty()) && (S[0] != '(')) {
00122         // error, should have leading paren
00123         nout << "error, no leading paren in '" << S << "'" << endl;
00124         S.erase(0, S.find_first_of("("));
00125       }
00126       while ((S.empty())||(i = ChanMatchingParen(S)) == -1) {
00127         S += " ";
00128         S += ChanReadLine(is);
00129         ChanEatFrontWhite(S);
00130       }
00131       (*p)(S.substr(1, i-2));
00132       S.erase(0, i);
00133       ChanEatFrontWhite(S);
00134     }
00135   }
00136   catch (...) {
00137     if (!S.empty() && (!(S == " ")))
00138       // error, trailing text usually indicates an error in the file
00139       nout << "error, trailing text in file S = '" << S << "'" << endl;
00140     return;
00141   }
00142 #endif /* BROKEN_EXCEPTIONS */
00143 }
00144 
00145 ChanParseFunctor::~ChanParseFunctor() {
00146   return;
00147 }
00148 
00149 void ChanParseFunctor::operator()(std::string) {
00150   // error, should never be in here
00151   return;
00152 }

Generated on Fri May 2 00:35:18 2003 for Panda by doxygen1.3