00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef INCLUDE_RSRC_PARSER
00012 #define INCLUDE_RSRC_PARSER
00013
00014 #include "../Surcouche/SurcoucheModule.h"
00015 #include "../Tools/Tools.h"
00016 #include "../Error/Error.h"
00017 #include "ParserRule.h"
00018 #include <vector>
00019 #include <string>
00020
00021 using namespace std;
00022
00023
00024 #define PARSER_BUFFER 10240
00025 #define PARSER_LABEL 32
00026 #define PARSER_TOK '$'
00027
00028
00029
00030
00031 class ParserRule
00032 {
00033 public :
00034 ROUTINE function;
00035 string syntax;
00036
00037
00038
00039
00040
00041 ParserRule(ROUTINE pFunction, const char *pSyntax)
00042 {
00043 function = pFunction;
00044 syntax = pSyntax;
00045 };
00046 };
00047
00048
00049
00050
00051 template <class T> class Parser
00052 {
00053 private :
00054 vector<ParserRule *> rules;
00055 T *obj;
00056 char none[1];
00057
00058 public :
00059
00060
00061
00062 Parser(T *pObj)
00063 {
00064 obj = pObj;
00065 *none = '\0';
00066 };
00067
00068
00069
00070 ~Parser()
00071 {
00072 unsigned int i;
00073
00074 for(i = 0; i < rules.size(); i ++)
00075 delete rules[i];
00076 rules.clear();
00077 };
00078
00079
00080
00081
00082
00083
00084 void AddRule(ROUTINE pFunction, const char *pSyntax, ...)
00085 {
00086 char temp[PARSER_BUFFER];
00087 va_list args;
00088
00089 va_start(args, pSyntax);
00090 vsprintf(temp, pSyntax, args);
00091 va_end(args);
00092
00093 rules.push_back(new ParserRule(pFunction, temp));
00094 };
00095
00096
00097
00098
00099
00100 void DelRule(unsigned int iPos)
00101 {
00102 if(iPos < rules.size())
00103 rules.erase(rules.begin() + iPos);
00104 };
00105
00106
00107
00108
00109
00110
00111 inline const char *GetSyntax(unsigned int iPos)
00112 {
00113 if(iPos < rules.size())
00114 return rules[iPos]->syntax.data();
00115
00116 return none;
00117 };
00118
00119
00120
00121
00122
00123 inline unsigned int GetNbRule()
00124 {
00125 return rules.size();
00126 };
00127
00128
00129
00130
00131
00132 void ParseLine(const char *pBuffer)
00133 {
00134 char out[PARSER_BUFFER];
00135 const char *b = pBuffer;
00136 char *o = out;
00137 TRY
00138
00139 while(*b)
00140 {
00141 if((*b != '\n') && (*b != '\r'))
00142 {
00143 *o++ = *b;
00144 }
00145 else if(out != o)
00146 {
00147 *o = '\0';
00148 Parse(out);
00149 o = out;
00150 }
00151
00152 b++;
00153 }
00154
00155 CATCH
00156 };
00157
00158
00159
00160
00161
00162 void Parse(const char *pBuffer)
00163 {
00164 unsigned int i = 0;
00165 const char *pSyntax = NULL;
00166 char end = '\0';
00167 char buffer[PARSER_BUFFER];
00168 char temp[PARSER_BUFFER];
00169 char label[PARSER_LABEL];
00170 char *pBuf = NULL;
00171 char *pTemp = NULL;
00172 char *pLabel = NULL;
00173 ParserRuleParameter<T> *parameter = NULL;
00174 TRY
00175
00176 strncpy(buffer, pBuffer, PARSER_BUFFER);
00177
00178 for(i = 0; i < rules.size(); i++)
00179 {
00180 pBuf = buffer;
00181 pSyntax = rules[i]->syntax.c_str();
00182 parameter = new ParserRuleParameter<T>(obj);
00183
00184 do
00185 {
00186 if(*pSyntax == PARSER_TOK)
00187 {
00188 pLabel = label;
00189 pTemp = temp;
00190
00191 pSyntax++;
00192 while(IsAlphaNumCar(*pSyntax))
00193 *pLabel++ = *pSyntax++;
00194 *pLabel = '\0';
00195
00196 end = *pSyntax;
00197
00198 while((*pBuf != '\0') && (*pBuf != end))
00199 {
00200 *pTemp = *pBuf;
00201 pTemp++;
00202 pBuf++;
00203 }
00204 *pTemp = '\0';
00205 pBuf++;
00206
00207 parameter->Add(label, temp);
00208 }
00209 else
00210 {
00211 if(*pSyntax == *pBuf)
00212 pBuf++;
00213 else
00214 break;
00215 }
00216
00217 if(*pSyntax != '\0')
00218 pSyntax++;
00219 }
00220 while(*pBuf && *pSyntax);
00221
00222 if((*pSyntax == '\0') && (*pBuf == '\0'))
00223 rules[i]->function((PARAMETRE)parameter);
00224
00225 delete parameter;
00226 }
00227
00228 CATCH
00229 };
00230 };
00231
00232 #endif