//读取一个单词 const char* CXML::ReadWord(const char* xml,char* word,int bufsize,TAG **ppTag) { assert(bufsize>0 && word); char* tempWord = word; *tempWord = '\0'; xml = SkipBlank(xml); //名称可以以字母,数字,下划线,[,]开头, 中间可以夹杂 点, 冒号, 破折号,[]. if( xml && *xml && ( IsAlphaNum((unsigned char) *xml) || IsAlpha((unsigned char) *xml) || *xml == '_' || *xml == '[' || *xml == ']') ) { while( xml && *xml && ( IsAlphaNum( (unsigned char ) *xml ) || *xml == '_' || *xml == '-' || *xml == '.' || *xml == ':' || *xml == '[' || *xml == ']') ) { *tempWord++ = *xml++; } *tempWord = '\0'; } return SkipBlank(xml); }
void BeginParse(struct out *firstOUT, struct info *in) { const char *gfwStart, *gfwEnd; gfwEnd = gfwStart = in->decodeGFWlist; char *buf = (char*)malloc(sizeof(in->proxy) + 48 * sizeof(char)); sprintf(buf, "{+forward-override{%s .}}\n", in->proxy); appendtoOUT(firstOUT, buf); free(buf); gfwEnd = NextLine(gfwStart);//skip the first line :[AutoProxy 0.2.9] do { gfwStart = SkipBlank(gfwEnd); gfwEnd = NextLine(gfwStart); // apple appple apppple\n apppple...... // ^ ^ // | | // start end if (*gfwStart == '\0' || *gfwEnd == '\0') {//meet the end of in->decodeGFWlist break; } else { appendtoOUT(firstOUT, parseALine(gfwStart, gfwEnd)); } } while (1); }
//读取元素的属性 '>'结束 const char* CXML::readAttrib(Element &e,const char* xml) { char szBuf[80]; Attribute attrib; TAG *tag = 0; do{ xml = ReadWord(xml,szBuf,80,&tag); //if(tag) // return (this->*(tag->parseFun))(xml); if(!szBuf[0]) break; attrib.SetName( szBuf ); xml = SkipBlank(xml); if(*xml != '=') throw XMLException("属性赋值不正确,缺少=号"); xml++; xml = SkipBlank(xml); if(*xml != '"') throw XMLException("属性赋值不正确,缺少\"号"); xml++; //read value xml = ReadText(xml,attrib.GetValue(),"\""); //if(!szBuf[0]) // throw XMLException("属性赋值不正确"); //attrib.value=szBuf; //if(*xml != '"') // throw XMLException("属性赋值不正确"); xml++; e.attrib.push_back(attrib); xml = SkipBlank(xml); }while(1); return xml; }
//元素结束 <![CDATA[ const char* CXML::parseCDataBegin(const char* xml) { xml = SkipTo(xml,"<![CDATA["); //读值 const char* xmlNext = Find(xml,"]]>"); if(!xmlNext) throw XMLException("CDATA未正确关闭"); Element* pElement = new DataElement; if(xmlNext > xml) { size_t n = xmlNext-xml; pElement->value.append(xml,n); } m_pcr->AddSubElement(pElement); return SkipBlank(xmlNext+3); }
//解析xml文本 bool CXML::Parse(const char* xmlText,size_t size/*=0*/) { Clear( ); if(xmlText == 0) return false; if(size) m_xmlEndTemp = xmlText+size; else m_xmlEndTemp = xmlText+strlen(xmlText); xmlText = Find(xmlText,"<"); if(xmlText == 0) throw XMLException("无标签"); const char* msg = xmlText; TAG *tag = 0; try{ while(*msg && msg<m_xmlEndTemp) { tag = 0; msg = ReadTag(msg,&tag); if(tag == 0) break; msg = SkipBlank((this->*(tag->parseFun))(msg)); } } catch( const XMLException& e ) { if(msg<m_xmlEndTemp){ std::string strmsg = e.what(); size_t len = m_xmlEndTemp>msg? m_xmlEndTemp-msg : 0; if( tag!=0 ) { if(len>80) len=80; strmsg += ". 在:"; strmsg.append(msg,len); strmsg += "....附近!"; } std::ofstream file_dump; SYSTEMTIME sysTime; GetLocalTime(&sysTime); char _name[256]; sprintf_s( _name,"%s_%d_%d_%d_.txt","xml_exception",sysTime.wHour,sysTime.wSecond,sysTime.wMinute ); file_dump.open(_name,std::ios::out); if(file_dump.is_open()) file_dump.write(xmlText,(std::streamsize)strlen(xmlText)); file_dump.close(); m_xmlEndTemp = 0; throw XMLException(strmsg.c_str()); } } m_xmlEndTemp = 0; if(m_pcr != &m_root) throw XMLException("标签未正确关闭"); if(m_pcr->subElement.size()!=1) throw XMLException("有且只能有一个根元素"); return true; }