QXml.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:17k
- #include <assert.h>
- #include "QXml.h"
- #include "QXmlParser.h"
- #include <stdlib.h>
- #include <unistd.h>
- QXml NewQXml()
- {
- QXml self;
- self.ToRoot = QXmlNodeToRoot;
- self.ParseFile = QXmlParseFile;
- self.ParseBuff = QXmlParseBuff;
- self.SaveToFile = QXmlSaveToFile;
- self.GetErrText = QXmlGetErrText;
- self.GetDeclareData = QXmlGetDeclareData;
- self.ReSet = QXmlReSet;
- self.Release = QXmlRelease;
- self.m_listDeclare = NewQFieldList();
- self.m_nodeRoot = MallocQXmlNode();
- self.m_nErrorCode = XML_OK;
- self.m_nRow = 0;
- self.m_nCol = 0;
- return self;
- }
- QXml *MallocQXml()
- {
- QXml *self = (QXml *)KB_OSPMalloc(sizeof(QXml));
- if(!self) return NULL;
-
- self->ToRoot = QXmlNodeToRoot;
- self->ParseFile = QXmlParseFile;
- self->ParseBuff = QXmlParseBuff;
- self->SaveToFile = QXmlSaveToFile;
- self->GetErrText = QXmlGetErrText;
- self->GetDeclareData = QXmlGetDeclareData;
- self->ReSet = QXmlReSet;
- self->Release = QXmlRelease;
- self->m_listDeclare = NewQFieldList();
- self->m_nodeRoot = MallocQXmlNode();
- self->m_nRow = 0;
- self->m_nCol = 0;
- self->m_nErrorCode = XML_OK;
- return self;
- }
- QXmlNode *QXmlNodeToRoot (QXml *self)
- {
- assert(self);
- return self->m_nodeRoot;
- }
- int QXmlParseFile (QXml *self,char *fileName)
- {
- QXmlParser parser = NewQXmlParser();
- int fd;
- assert(self);
- if((fd = open(fileName,O_RDONLY)) == -1)
- {
- return XML_IO_OPEN_FILE_ERROR;
- }
- parser.FromFile(&parser,fd);
- parser.Parse(&parser,self);
- parser.Release(&parser);
- close(fd);
- return self->m_nErrorCode;
- }
- int QXmlParseBuff (QXml *self,char *buff)
- {
- QXmlParser parser = NewQXmlParser();
- if(!buff)
- {
- parser.Release(&parser);
- return XML_DATA_EMPTY;
- }
- if(parser.FromBuff(&parser,buff) == XML_OK)
- {
- parser.Parse(&parser,self);
- }
- return self->m_nErrorCode;
- }
- int QXmlSaveToFile (QXml *self,char *fileName)
- {
- QString str = NewQString();
- QXmlNode *node = NULL;
- char *buff;
- int fd;
- #ifdef WIN32
- if( (fd = open(fileName,O_RDWR|O_CREAT|O_TRUNC) ) == -1)
- #else
- if( (fd = open(fileName,O_RDWR|O_CREAT|O_TRUNC,0666) ) == -1)
- #endif
- {
- self->m_nErrorCode = XML_IO_OPEN_FILE_ERROR;
- return self->m_nErrorCode;
- }
- node = self->ToRoot(self);
-
- self->GetDeclareData(self,&str);
- while(node)
- {
- node->GetNodeData(node,&str,0);
- node = node->ToNextNode(node,NULL);
- }
-
- buff = str.m_dataBuff;
- while(*buff)
- {
- write(fd,&(*buff),1);
- buff++;
- }
- str.Release(&str);
- close(fd);
- return self->m_nErrorCode;
- }
- int QXmlGetDeclareData (QXml *self,QString *data)
- {
- assert(self);
- if(!data) return XML_DATA_EMPTY;
- if(!self->m_listDeclare.m_listField.IsEmpty(&self->m_listDeclare.m_listField))
- {
- data->AppendString(data,"<?xml ");
- if(self->m_listDeclare.m_listField.MoveToHead(&self->m_listDeclare.m_listField) == QLIST_OK)
- {
- QField *field;
- do
- {
- field = (QField *)self->m_listDeclare.m_listField.GetData(&self->m_listDeclare.m_listField);
-
- data->AppendString(data,field->name.m_dataBuff);
-
- data->AppendString(data,"=");
- if(field->value.Include(&field->value,'"'))
- {
- data->AppendString(data,"'");
- QXmlDeGetEntity(&field->value);
- data->AppendString(data,field->value.m_dataBuff);
- data->AppendString(data,"'");
- }else
- {
- data->AppendString(data,""");
- QXmlDeGetEntity(&field->value);
- data->AppendString(data,field->value.m_dataBuff);
- data->AppendString(data,""");
- }
- data->AppendString(data," ");
- }while(self->m_listDeclare.m_listField.MoveNext(&self->m_listDeclare.m_listField) == QLIST_OK);
- }
- data->AppendString(data,"?>n");
- }
- return XML_OK;
- }
- char *QXmlGetErrText (QXml *self)
- {
- return NULL;
- }
- QXmlNode *MallocQXmlNode()
- {
- QXmlNode *self = (QXmlNode *)KB_OSPMalloc(sizeof(QXmlNode));
- if(!self) return NULL;
- self->ToParent = QXmlNodeToParent;
- self->ToChild = QXmlNodeToChild;
- self->ToNextNode = QXmlNodeToNextNode;
- self->ToPrevNode = QXmlNodeToPrevNode;
- self->ToFirstChild = QXmlNodeToFirstChild;
- self->ToLastChild = QXmlNodeToLastChild;
- self->AddChild = QXmlNodeAddChild;
- self->AddNodeToChild = QXmlNodeAddNodeToChild;
- self->SetNodeName = QXmlNodeSetNodeName;
- self->SetNodeValue = QXmlNodeSetNodeValue;
- self->GetNodeName = QXmlNodeGetNodeName;
- self->GetNodeValue = QXmlNodeGetNodeValue;
- self->GetNodeType = QXmlNodeGetNodeType;
- self->GetNodeData = QXmlNodeGetNodeData;
- self->SetAttrib = QXmlNodeSetAttrib;
- self->GetAttrib = QXmlNodeGetAttrib;
- self->RemoveAttrib = QXmlNodeRemoveAttrib;
- self->Destory = QXmlNodeDestory;
- self->Release = QXmlNodeRelease;
- self->m_szName = NewQString();
- self->m_szValue = NewQString();
- self->m_nType = NODE;
- self->m_listAttrib = NewQFieldList();
- self->m_nodeChild = NULL;
- self->m_nodeNext = NULL;
- self->m_nodePrev = NULL;
- self->m_nodeParent = NULL;
- return self;
- }
- QXmlNode *QXmlNodeToParent (QXmlNode *self)
- {
- assert(self);
- return self->m_nodeParent;
-
- }
- QXmlNode *QXmlNodeToChild (QXmlNode *self,char *name)
- {
- QXmlNode *tempNode;
- assert(self);
- tempNode = self->ToFirstChild(self);
- if(name == NULL) return tempNode;
- while(tempNode)
- {
- if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
- return tempNode;
- tempNode = tempNode->m_nodeNext;
- }
- return NULL;
- }
- QXmlNode *QXmlNodeToNextNode (QXmlNode *self,char *name)
- {
- QXmlNode *tempNode;
- assert(self);
- tempNode = self->m_nodeNext;
- if(name == NULL) return tempNode;
- while(tempNode)
- {
- if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
- return tempNode;
- tempNode = tempNode->m_nodeNext;
- }
- return NULL;
- }
- QXmlNode *QXmlNodeToPrevNode (QXmlNode *self,char *name)
- {
- QXmlNode *tempNode;
- assert(self);
- tempNode = self->m_nodePrev;
- if(name == NULL) return tempNode;
- while(tempNode)
- {
- if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
- return tempNode;
- tempNode = tempNode->m_nodePrev;
- }
- return NULL;
- }
- QXmlNode *QXmlNodeToFirstChild (QXmlNode *self)
- {
- QXmlNode *tempNode;
- assert(self);
- tempNode = self->m_nodeChild;
- return tempNode;
- }
- QXmlNode *QXmlNodeToLastChild (QXmlNode *self)
- {
- QXmlNode *tempNode;
- assert(self);
- tempNode = self->m_nodeChild;
- if(tempNode == NULL) return NULL;
- while(tempNode->m_nodeNext)
- {
- tempNode = tempNode->m_nodeNext;
- }
- return tempNode;
- }
- BOOL QXmlNodeAddChild (QXmlNode *self,char *name ,char *value)
- {
- QXmlNode *tempNode = NULL;
- QXmlNode *tempChild = NULL;
- assert(self);
- if(name == NULL)return FALSE;
-
- tempNode = MallocQXmlNode();
- if(!tempNode) return FALSE;
- tempNode->m_szName.AppendString(&tempNode->m_szName,name);
- tempNode->m_szValue.AppendString(&tempNode->m_szValue,value);
- tempChild = self->ToLastChild(self);
- if(!tempChild)
- self->m_nodeChild = tempNode;
- else
- tempChild->m_nodeNext = tempNode;
- return TRUE;
- }
- BOOL QXmlNodeAddNodeToChild (QXmlNode *self,QXmlNode *node)
- {
- QXmlNode *tempChild;
- assert(self);
-
- tempChild = self->ToLastChild(self);
- if(!tempChild)
- self->m_nodeChild = node;
- else
- {
- node->m_nodePrev = tempChild;
- tempChild->m_nodeNext = node;
- }
- return TRUE;
- }
- BOOL QXmlNodeSetNodeName (QXmlNode *self , char *name)
- {
- assert(self);
- if(name == NULL)return FALSE;
-
- self->m_szName.ReSet(&self->m_szName,name);
- return TRUE;
- }
- BOOL QXmlNodeSetNodeValue (QXmlNode *self , char *value)
- {
- assert(self);
- if(value == NULL)return FALSE;
-
- self->m_szValue.ReSet(&self->m_szValue,value);
- return TRUE;
- }
- char *QXmlNodeGetNodeName (QXmlNode *self)
- {
- assert(self);
- return self->m_szName.m_dataBuff;
- }
- char *QXmlNodeGetNodeValue (QXmlNode *self)
- {
- assert(self);
- return self->m_szValue.m_dataBuff;
- }
- int QXmlNodeGetNodeData (QXmlNode *self,QString *data ,int depth)
- {
- int i;
- QString depthText;
- assert(self);
- if(!data) return -1;
- //if(self->m_szName.IsEmpty(&self->m_szName)) return -1;
- depthText = NewQString();
- //case NODE:转换value,保存属性名,转换属性值,value
- //case COMMENT:保存字段名
- //case HOLD:保存属性名,转换属性值,value,
- for(i = 0 ;i < depth ; i++)
- {
- depthText.AppendString(&depthText," ");
- }
- if(self->m_nType == COMMENT)
- {
- /*注释 <!-- -->*/
- data->AppendString(data,depthText.m_dataBuff);
- // QXmlDeGetEntity(&self->m_szName);
- data->AppendString(data,commentHeader);
- data->AppendString(data,self->m_szName.m_dataBuff);
- data->AppendString(data,commentEnd);
- data->AppendChar(data,'n');
- }else if(self->m_nType == NODE || self->m_nType == HOLD)
- {
- // if(self->m_nType == HOLD){ printf("exitn"); exit(1);}
- data->AppendString(data,depthText.m_dataBuff);
- data->AppendChar(data,'<');
- data->AppendString(data,self->m_szName.m_dataBuff);
- if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
- {
- QField *field;
- do
- {
- field = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
- data->AppendChar(data,' ');
-
- data->AppendString(data,field->name.m_dataBuff);
-
- data->AppendString(data, "=");
- if(field->value.Include(&field->value,'"'))
- {
- data->AppendChar(data,''');
- QXmlDeGetEntity(&field->value);
- data->AppendString(data,field->value.m_dataBuff);
- data->AppendChar(data,''');
- }else
- {
- data->AppendChar(data,'"');
- QXmlDeGetEntity(&field->value);
- data->AppendString(data,field->value.m_dataBuff);
- data->AppendChar(data,'"');
- }
- }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
- }
- if(self->m_szValue.IsEmpty(&self->m_szValue)
- && !self->m_nodeChild)
- {
-
- data->AppendString(data,"/>n");
- }
- else
- {
- //有值或子项
- data->AppendString(data,">n");
- data->AppendString(data,depthText.m_dataBuff);
- if(!self->m_szValue.IsEmpty(&self->m_szValue))
- {
- if(self->m_nType == NODE )
- {
- // printf("[%s]n",self->m_szValue.m_dataBuff);
- QXmlDeGetEntity(&self->m_szValue);
- }
-
- if (self->m_nType == HOLD )
- {
- data->AppendString(data,cdataHeader);
- data->AppendString(data,self->m_szValue.m_dataBuff);
- data->AppendString(data,cdataEnd);
- }else
- {
- data->AppendString(data,self->m_szValue.m_dataBuff);
- }
- data->AppendChar(data,'n');
- }
- if(self->m_nodeChild)
- {
- QXmlNode *child = self->m_nodeChild;
- while(child)
- {
- child->GetNodeData(child,data,depth+1);
- child = child->m_nodeNext;
- }
- data->AppendChar(data,'n');
- }
- data->AppendString(data,depthText.m_dataBuff);
- data->AppendString(data,"</");
- data->AppendString(data,self->m_szName.m_dataBuff);
- data->AppendString(data,">n");
- }
- }
- depthText.Release(&depthText);
- return XML_OK;
- }
- int QXmlNodeGetNodeType (QXmlNode *self)
- {
- assert(self);
- return self->m_nType;
-
- }
- BOOL QXmlNodeSetAttrib (QXmlNode *self , char *name , char * value)
- {
- QField *pAttrib = NULL;
- assert(self);
- if(name == NULL)
- {
- return FALSE;
- }
- if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
- {
- QField *pAttrib;
- do
- {
- pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
- if(pAttrib->name.Equal(&pAttrib->name,name))
- {
- pAttrib->value.ReSet(&pAttrib->value,value);
- return TRUE;
- }
- }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
- }
- pAttrib = MallocQField();
- pAttrib->name.AppendString(&pAttrib->name,name);
- pAttrib->value.AppendString(&pAttrib->value,value);
- self->m_listAttrib.m_listField.Add(&self->m_listAttrib.m_listField,pAttrib);
-
- return TRUE;
- }
- char *QXmlNodeGetAttrib (QXmlNode *self , char *name)
- {
- assert(self);
- if(name == NULL)
- {
- return NULL;
- }
- if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
- {
- QField *pAttrib;
- do
- {
- pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
- if(pAttrib->name.Equal(&pAttrib->name,name))
- {
- return pAttrib->value.m_dataBuff;
- }
- }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
- }
- return NULL;
- }
- BOOL QXmlNodeRemoveAttrib (QXmlNode *self , char *name)
- {
- assert(self);
- if(name == NULL)
- {
- return FALSE;
- }
- if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
- {
- QField *pAttrib;
- do
- {
- pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
- if(pAttrib->name.Equal(&pAttrib->name,name))
- {
- return self->m_listAttrib.m_listField.Remove(&self->m_listAttrib.m_listField) == QLIST_OK ?TRUE:FALSE;
- }
- }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
- }
- return FALSE;
- }
- void QXmlNodeDestory (QXmlNode *self)
- {
- assert(self);
- //如果是root节点(parentNode == NULL),清空所有节点
- //如果是第一个节点,就把父节点的第一个节点为下一个节点
- //如果不是第一节点,就将上一个节点的下一个节点。指向下一个节点
- if(self->m_nodeParent != NULL)
- {
- if(self->m_nodePrev == NULL)
- {
- self->m_nodeParent->m_nodeChild = self->m_nodeNext;
- }
- else
- {
- self->m_nodePrev->m_nodeNext = self->m_nodeNext;
- }
- }
-
- self->Release(self);
- }
- int QXmlDeGetEntity (QString *str)
- {
- int i=0;
- char c;
- QString newStr = NewQString();
- if(str == NULL) return -1;
- newStr.AllocBuffer(&newStr,str->m_nDataLength);
- while( i < str->m_nDataLength )
- {
- c = str->m_dataBuff[i];
- if ( c == '&' )
- {
- newStr.AppendString(&newStr,entity[0].str);
- }
- else if ( c == '<' )
- {
- newStr.AppendString(&newStr,entity[1].str);
- }
- else if ( c == '>' )
- {
- newStr.AppendString(&newStr,entity[2].str);
-
- }
- else if ( c == '"' )
- {
- newStr.AppendString(&newStr,entity[3].str);
-
- }
- else if ( c == ''' )
- {
- newStr.AppendString(&newStr,entity[4].str);
- }
- else
- {
- newStr.AppendChar(&newStr,c);
- }
- ++i;
- }
- str->ReSet(str,newStr.m_dataBuff);
- newStr.Release(&newStr);
- return 1;
- }
- void QXmlReSet (QXml *self)
- {
- assert(self);
- self->Release(self);
- self->m_nodeRoot = MallocQXmlNode();
- }
- void QXmlRelease (QXml *self)
- {
- QXmlNode *tempNode;
- QXmlNode *tempNodeNext;
- QXmlNode *child;
- QXmlNode *tempChild;
- assert(self);
-
- self->m_listDeclare.Release(&self->m_listDeclare);
- self->m_nCol = 0;
- self->m_nErrorCode = XML_OK;
- self->m_nRow = 0;
- if(self->m_nodeRoot)
- {
- child = self->m_nodeRoot->m_nodeChild;
-
- while(child)
- {
- tempChild = child;
- child = child->m_nodeNext;
-
-
- tempChild->Release(tempChild);
- KB_OSPFree(tempChild);
- tempChild = NULL;
- }
- self->m_nodeRoot->m_nodeChild = NULL;
- tempNode = self->m_nodeRoot->m_nodeNext;
- while(tempNode)
- {
- tempNodeNext = tempNode;
- tempNode = tempNode->m_nodeNext;
-
- tempNodeNext->Release(tempNodeNext);
- KB_OSPFree(tempNodeNext);
- tempNodeNext = NULL;
- }
- self->m_nodeRoot->Release(self->m_nodeRoot);
- KB_OSPFree(self->m_nodeRoot);
- self->m_nodeRoot = NULL;
- }
- }
- void QXmlNodeRelease (QXmlNode *self)
- {
- QXmlNode *child;
- QXmlNode *tempChild;
- assert(self);
- self->m_szName.Release(&self->m_szName);
- self->m_szValue.Release(&self->m_szValue);
-
- // 应该处理子节点的数据 :(
- child = self->m_nodeChild;
- while(child)
- {
- tempChild = child;
- child = child->m_nodeNext;
- tempChild->Release(tempChild);
- KB_OSPFree(tempChild);
- tempChild = NULL;
- }
- self->m_listAttrib.Release(&self->m_listAttrib);
- }