?? xmlwf.c
字號:
//#include "CommonData.h"
#include "StringProcess.h"
#include "CharSet.h"
#include "XMLwf.h"
#include "NetWork.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define XML_ASSERT(x) if(x==NULL){((PXML_Parser)parser)->parserErrorHandler(((PXML_Parser)parser)->userData,"Error_in_system:Out_Of_memory");return NULL;}
#define XML_ASSERT_VOID(x) if(x==NULL){((PXML_Parser)parser)->parserErrorHandler(((PXML_Parser)parser)->userData,"Error_in_system:Out_Of_memory");return;}
/*start_attribute define*/
/*define a element structure of the attibute and the operations about it.*/
typedef struct attElement{
PXML_String name;
PXML_String value;
struct attElement* next;
} AttriPairs;
typedef AttriPairs* PAttriPairs;
void AttriPairsFree(PAttriPairs p)
{
free(p->name);
free(p->value);
free(p);
}
PAttriPairs AttriPairsCreate(const XML_Char* name, const XML_Char* value)
{
enum XML_STR_PRO_Error t;
PAttriPairs pAP = malloc(sizeof(AttriPairs));
if(pAP == NULL) return NULL;
pAP->name = CreateXML_String();
if(pAP->name == NULL) {
free(pAP);
return NULL;
}
pAP->value = CreateXML_String();
if(pAP->value == NULL){
free(pAP);
return NULL;
}
t = XML_StrPCharCat(pAP->name, name);
if(t != STRING_SUCCESS ){
free(pAP);
return NULL;
}
t = XML_StrPCharCat(pAP->value, value);
if(t != STRING_SUCCESS ){
free(pAP);
return NULL;
}
pAP->next = NULL;
return pAP;
}
/*define attibutes structures and the operations on it.*/
typedef struct{
int size;
PAttriPairs pairsHead;
PAttriPairs pairsTail;
} Attributes;
typedef Attributes* PAttributes;
PAttributes AsPAttributes(void* p)
{
return (PAttributes)p;
}
void IniAttributes(PAttributes p)
{
p->size = 0;
p->pairsHead = NULL;
p->pairsTail = NULL;
}
PAttributes AtrributesCreate()
{
PAttributes pAttri = malloc(sizeof(Attributes));
if(!pAttri) return NULL;
IniAttributes(pAttri);
return pAttri;
}
void AttributesFree(PAttributes pAttri)
{
int i = 0;
PAttriPairs current = NULL;
PAttriPairs next = NULL;
if(!pAttri) return;
current = pAttri->pairsHead;
if(current) next = current->next;
for(;i<pAttri->size; i++){
free(current);
current = next;
if(next) next = next->next;
}
free(pAttri);
}
void addAttribute(Patrributes desti, PAttriPairs attri)
{
PAttributes destination = AsPAttributes(desti);
if(!desti || !attri) return;
if(!destination->size){
destination->pairsHead = attri;
destination->pairsTail = attri;
}else{
destination->pairsTail->next = attri;
destination->pairsTail = attri;
}
destination->size++;
}
enum XML_Error addAttriPairs(PAttributes destination, const XML_Char* name, const XML_Char* value)
{
PAttriPairs attri = AttriPairsCreate(name, value);
if(attri == NULL) return ADD_ATTRI_ERROR;
addAttribute(destination, attri);
return XML_SUCCESS;
}
PAttriPairs GetAttributeByIndex(const PAttributes attributes, int index)
{
int i = 0;
PAttriPairs current = NULL;
if(!attributes) return NULL;
if(index >=attributes->size ) return NULL;
current = attributes->pairsHead;
for(;i < index;i++)
current = current->next;
return current;
}
PAttriPairs GetAttributeByName(const PAttributes attributes, const XML_Char* name)
{
int i = 0;
PAttriPairs current = NULL;
if(!attributes) return NULL;
if(!attributes->size) return NULL;
current = attributes->pairsHead;
for(;i < attributes->size;i++){
if(!XML_StrPCharCmp(current->name, name)) return current;
current = current->next;
}
return NULL;
}
const XML_Char* __GetNameFromAttriByIndex(void* attri, int index)
{
PAttriPairs p = NULL;
PAttributes attributes = AsPAttributes(attri);
p = GetAttributeByIndex(attributes, index);
if(p) return p->name->data;
else return NULL;
}
const XML_Char* __GetValueFromAttriByIndex(void* attri, int index)
{
PAttriPairs p = NULL;
const PAttributes attributes = AsPAttributes(attri);
p = GetAttributeByIndex(attributes, index);
if(p) return p->value->data;
else return NULL;
}
const XML_Char* __GetValueByName(void* attri, const XML_Char* name)
{
const PAttributes attributes = AsPAttributes(attri);
PAttriPairs attriPairs = NULL;
attriPairs = GetAttributeByName(attributes, name);
if(!attriPairs) return NULL;
return attriPairs->value->data;
}
//end attribute define
_BOOL IsMemberOf(const XML_Char member, const XML_Char* set, int sizeOfSetElement)
{
int size = sizeOfSetElement;
int i =0;
for(;i < size; i++)
if(member == set[i]) return TRUE;
return FALSE;
}
_BOOL IsMemberOfInt(const int member, const int* set, int sizeOfSetElement)
{
int size = sizeOfSetElement;
int i =0;
for(;i < size; i++)
if(member == set[i]) return TRUE;
return FALSE;
}
//begin the functions to process the recusion down analysis.
int row = 1;
int col = 1;
XML_Char buffer[BUF_SIZE + 2];/* The buffer to store the XML_Char from data source.*/
int cursor = 2; /*The cursor point the current XML_char in use in 'buffer'.*/
int inputType;/*Record the type of input data source.*/
unsigned int sock = -1;/*The SOCKET for data getting from web.*/
FILE* inputStream = NULL;/*The pointer of the stream of file. */
int encodingType = ANSI;/*The type of the file*/
void RefreshBuffer()
{
int i;
unsigned short in;
unsigned char tmpChar;
#ifdef XML_UNICODE
unsigned char tmpBuf[BUF_SIZE];
#else
#endif
buffer[0] = buffer[BUF_SIZE];
buffer[1] = buffer[BUF_SIZE + 1];
if(inputType == XML_FILE_NAME || inputType == XML_FILE_STREAM)
if(encodingType == UNICODE_){
for(i = 2;i<BUF_SIZE + 2; i++){
if(feof(inputStream)) break;
buffer[i] = fgetc(inputStream);
if(feof(inputStream)) break;
in = fgetc(inputStream);
if(in){
buffer[i] = buffer[i]&0x00FF;
in = in<<8;
buffer[i] = buffer[i]|in;
}
}
}else if(encodingType == UNICODE_B){
for(i = 2;i<BUF_SIZE + 2; i++){
if(feof(inputStream)) break;
buffer[i] = fgetc(inputStream);
if(feof(inputStream)) break;
in = fgetc(inputStream);
if(in == 0xFFFF) return;
if(in){
buffer[i] = buffer[i]<<8;
in = in&0x00FF;
buffer[i] = buffer[i]|in;
}
}
}else{
for(i = 2;i<BUF_SIZE + 2; i++){
if(feof(inputStream)) break;
buffer[i] = fgetc(inputStream);
}
}
else if(encodingType == UNICODE_){
for(i = 2;i<BUF_SIZE + 2; i++){
if(!HttpStreamGet(sock, &tmpChar, 1)) break;
buffer[i] = tmpChar;
if(!HttpStreamGet(sock, &tmpChar, 1)) break;
in = tmpChar;
if(in){
buffer[i] = buffer[i]&0x00FF;
in = in<<8;
buffer[i] = buffer[i]|in;
}
}
}else if(encodingType == UNICODE_B){
for(i = 2;i<BUF_SIZE + 2; i++){
if(!HttpStreamGet(sock, &tmpChar, 1)) break;
buffer[i] = tmpChar;
if(!HttpStreamGet(sock, &tmpChar, 1)) break;
in = tmpChar;
if(in){
buffer[i] = buffer[i]<<8;
in = in&0x00FF;
buffer[i] = buffer[i]|in;
}
}
}else{
#ifdef XML_UNICODE
HttpStreamGet(sock, tmpBuf, BUF_SIZE);
for(i = 2;i<BUF_SIZE + 2; i++)
buffer[i] = tmpBuf[i-2];
#else
HttpStreamGet(sock, &buffer[2], BUF_SIZE);
#endif
}
cursor = 2;
}
XML_Char GetChar()
{
if(cursor > BUF_SIZE+1) RefreshBuffer();
col++;
return buffer[cursor++];
}
void GoBack(int step)
{
if(step == 1) cursor--;
else cursor = cursor - 2;
}
void JudgeEncodingType()
{
int tmp = 0;
unsigned char nTmp = 0;
if(inputType == XML_FILE_NAME || inputType == XML_FILE_STREAM){
tmp = fgetc(inputStream);
if(tmp == 0xFF){
tmp = fgetc(inputStream);
if(tmp == 0xFE) encodingType = UNICODE_;
else exit(0);
}else if(tmp == 0xEF){
tmp = fgetc(inputStream);
if(tmp == 0xBB){
tmp = fgetc(inputStream);
if(tmp == 0xBF){ encodingType = UTF_8;}
else exit(0);
}else exit(0);
}else if(tmp == 0xFE){
tmp = fgetc(inputStream);
if(tmp == 0xFF) encodingType = UNICODE_B;
else exit(0);
}else if(tmp == 0xFF){
}else{
buffer[1] = tmp;
cursor = 1;
encodingType= ANSI;
}
}else{
HttpStreamGet(sock, &nTmp, 1);
if(nTmp == 0xFF){
HttpStreamGet(sock, &nTmp, 1);
if(nTmp == 0xFE) encodingType = UNICODE_;
else exit(0);
}else if(nTmp == 0xEF){
HttpStreamGet(sock, &nTmp, 1);
if(nTmp == 0xBB){
HttpStreamGet(sock, &nTmp, 1);
if(nTmp == 0xBF) encodingType = UTF_8;
else exit(0);
}else exit(0);
}else if(nTmp == 0xFE){
HttpStreamGet(sock, &nTmp, 1);
if(nTmp == 0xFF) encodingType = UNICODE_B;
else exit(0);
}else encodingType= ANSI;
}
RefreshBuffer();
if(encodingType == ANSI){
if(inputType == XML_FILE_NAME || inputType == XML_FILE_STREAM) buffer[1] = tmp;
else buffer[1] = nTmp;
cursor = 1;
}
}
int initialGlobalVar(unsigned int s, FILE* pf, int type)
{
if(type<XML_FILE_NAME ||type>XML_URL) return FALSE;
sock = s;
inputStream = pf;
inputType = type;
memset(buffer, '\0', BUF_SIZE+2);
cursor = BUF_SIZE+2;
row = 1;
col = 1;
encodingType= ANSI;
JudgeEncodingType();
return TRUE;
}
void ErrorHappen(Pxml_parser parser, const char* errorInf)
{
FILE* log = fopen("c:\\XMLParser.log", "a+");
if( log != NULL){
fputs(errorInf, log);
fputc('\n', log);
}
fclose(log);
printf("Error in line %d, colomn %d. %s", row, col, errorInf);
if(((PXML_Parser)parser)->parserErrorHandler != NULL)
((PXML_Parser)parser)->parserErrorHandler( ((PXML_Parser)parser)->userData, errorInf);
exit(1);
}
/**
Skip the character in 'space'
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -