00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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;
00035 #endif
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);
00041 if (i != 0)
00042 S.erase(0, i);
00043 i = S.find_first_of(";");
00044 if (i != std::string::npos)
00045 S.erase(i, std::string::npos);
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;
00088 }
00089 #endif
00090
00091
00092 #ifdef BROKEN_EXCEPTIONS
00093 {
00094 int i;
00095 while (true) {
00096 if ((!S.empty()) && (S[0] != '(')) {
00097
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
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
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
00139 nout << "error, trailing text in file S = '" << S << "'" << endl;
00140 return;
00141 }
00142 #endif
00143 }
00144
00145 ChanParseFunctor::~ChanParseFunctor() {
00146 return;
00147 }
00148
00149 void ChanParseFunctor::operator()(std::string) {
00150
00151 return;
00152 }