QXml.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:17k
源码类别:

DVD

开发平台:

C/C++

  1. #include <assert.h>
  2. #include "QXml.h"
  3. #include "QXmlParser.h"
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. QXml NewQXml()
  7. {
  8.    QXml self;
  9.    self.ToRoot = QXmlNodeToRoot;
  10.    self.ParseFile = QXmlParseFile;
  11.    self.ParseBuff = QXmlParseBuff;
  12.    self.SaveToFile = QXmlSaveToFile;
  13.    self.GetErrText = QXmlGetErrText;
  14.    self.GetDeclareData = QXmlGetDeclareData;
  15.    self.ReSet = QXmlReSet;
  16.    self.Release = QXmlRelease;
  17.    self.m_listDeclare = NewQFieldList();
  18.    self.m_nodeRoot = MallocQXmlNode();
  19.    self.m_nErrorCode = XML_OK;
  20.    self.m_nRow = 0;
  21.    self.m_nCol = 0;
  22.    return self;
  23. }
  24. QXml *MallocQXml()
  25. {
  26.    QXml *self = (QXml *)KB_OSPMalloc(sizeof(QXml));
  27.    if(!self) return NULL;
  28.    
  29.    self->ToRoot = QXmlNodeToRoot;
  30.    self->ParseFile = QXmlParseFile;
  31.    self->ParseBuff = QXmlParseBuff;
  32.    self->SaveToFile = QXmlSaveToFile;
  33.    self->GetErrText = QXmlGetErrText;
  34.    self->GetDeclareData = QXmlGetDeclareData;
  35.    self->ReSet = QXmlReSet;
  36.    self->Release = QXmlRelease;
  37.    self->m_listDeclare = NewQFieldList();
  38.    self->m_nodeRoot = MallocQXmlNode();
  39.    self->m_nRow = 0;
  40.    self->m_nCol = 0;
  41.    self->m_nErrorCode = XML_OK;
  42.    return self;
  43. }
  44. QXmlNode *QXmlNodeToRoot (QXml *self)
  45. {
  46.    assert(self);
  47.    return self->m_nodeRoot;
  48. }
  49. int QXmlParseFile (QXml *self,char *fileName)
  50. {
  51.    QXmlParser parser = NewQXmlParser();
  52.    int fd;
  53.    assert(self);
  54.    if((fd = open(fileName,O_RDONLY)) == -1)
  55.    {
  56.       return XML_IO_OPEN_FILE_ERROR;
  57.    }
  58.    parser.FromFile(&parser,fd);
  59.    parser.Parse(&parser,self);
  60.    parser.Release(&parser);
  61.    close(fd);   
  62.    return self->m_nErrorCode;
  63. }
  64. int QXmlParseBuff (QXml *self,char *buff)
  65. {
  66.    QXmlParser  parser = NewQXmlParser();
  67.    if(!buff) 
  68.    {
  69.       parser.Release(&parser);
  70.       return XML_DATA_EMPTY;
  71.    }
  72.    if(parser.FromBuff(&parser,buff) == XML_OK)
  73.    {
  74.       parser.Parse(&parser,self);
  75.    }
  76.    return self->m_nErrorCode;
  77. }
  78. int QXmlSaveToFile (QXml *self,char *fileName)
  79. {
  80.    QString str = NewQString();
  81.    QXmlNode *node = NULL;
  82.    char *buff;
  83.    int fd;
  84. #ifdef WIN32
  85.    if( (fd = open(fileName,O_RDWR|O_CREAT|O_TRUNC) )  == -1)
  86. #else
  87.    if( (fd = open(fileName,O_RDWR|O_CREAT|O_TRUNC,0666) )  == -1)
  88. #endif
  89.    {
  90.      self->m_nErrorCode = XML_IO_OPEN_FILE_ERROR;
  91.      return self->m_nErrorCode;
  92.    }
  93.    node = self->ToRoot(self);
  94.    
  95.    self->GetDeclareData(self,&str);
  96.    while(node)
  97.    {
  98.       node->GetNodeData(node,&str,0);
  99.       node = node->ToNextNode(node,NULL);
  100.    }
  101.    
  102.     buff = str.m_dataBuff;
  103.    while(*buff)
  104.    {
  105.       write(fd,&(*buff),1);
  106.       buff++;
  107.    }
  108.    str.Release(&str);
  109.    close(fd);
  110.    return self->m_nErrorCode;
  111. }
  112. int QXmlGetDeclareData (QXml *self,QString *data)
  113. {
  114.    assert(self);
  115.    if(!data) return XML_DATA_EMPTY;
  116.    if(!self->m_listDeclare.m_listField.IsEmpty(&self->m_listDeclare.m_listField))
  117.    {
  118.       data->AppendString(data,"<?xml ");
  119.       if(self->m_listDeclare.m_listField.MoveToHead(&self->m_listDeclare.m_listField) == QLIST_OK)
  120.       {
  121.          QField *field;
  122.          do
  123.          {
  124.             field = (QField *)self->m_listDeclare.m_listField.GetData(&self->m_listDeclare.m_listField);
  125.             
  126.             data->AppendString(data,field->name.m_dataBuff);
  127.             
  128.             data->AppendString(data,"=");
  129.             if(field->value.Include(&field->value,'"'))
  130.             {
  131.                data->AppendString(data,"'");
  132.                QXmlDeGetEntity(&field->value);
  133.                data->AppendString(data,field->value.m_dataBuff);
  134.                data->AppendString(data,"'");
  135.             }else
  136.             {
  137.                data->AppendString(data,""");
  138.                QXmlDeGetEntity(&field->value);
  139.                data->AppendString(data,field->value.m_dataBuff);
  140.                data->AppendString(data,""");   
  141.             }
  142.             data->AppendString(data," ");   
  143.          }while(self->m_listDeclare.m_listField.MoveNext(&self->m_listDeclare.m_listField) == QLIST_OK);
  144.       }
  145.       data->AppendString(data,"?>n");
  146.    }
  147.    return XML_OK;
  148. }
  149. char *QXmlGetErrText (QXml *self)
  150. {
  151.    return NULL;
  152. }
  153. QXmlNode *MallocQXmlNode()
  154. {
  155.    QXmlNode *self = (QXmlNode *)KB_OSPMalloc(sizeof(QXmlNode));
  156.    if(!self) return NULL;
  157.    self->ToParent = QXmlNodeToParent;
  158.    self->ToChild = QXmlNodeToChild;
  159.    self->ToNextNode = QXmlNodeToNextNode;
  160.    self->ToPrevNode = QXmlNodeToPrevNode;
  161.    self->ToFirstChild = QXmlNodeToFirstChild;
  162.    self->ToLastChild =  QXmlNodeToLastChild;
  163.    self->AddChild = QXmlNodeAddChild;
  164.    self->AddNodeToChild = QXmlNodeAddNodeToChild;
  165.    self->SetNodeName = QXmlNodeSetNodeName;
  166.    self->SetNodeValue = QXmlNodeSetNodeValue;
  167.    self->GetNodeName = QXmlNodeGetNodeName;
  168.    self->GetNodeValue = QXmlNodeGetNodeValue;
  169.    self->GetNodeType = QXmlNodeGetNodeType;
  170.    self->GetNodeData = QXmlNodeGetNodeData;
  171.    self->SetAttrib = QXmlNodeSetAttrib;
  172.    self->GetAttrib = QXmlNodeGetAttrib;
  173.    self->RemoveAttrib = QXmlNodeRemoveAttrib;
  174.    self->Destory = QXmlNodeDestory;
  175.    self->Release = QXmlNodeRelease;
  176.    self->m_szName = NewQString();
  177.    self->m_szValue = NewQString();
  178.    self->m_nType = NODE;
  179.    self->m_listAttrib = NewQFieldList();
  180.    self->m_nodeChild = NULL;
  181.    self->m_nodeNext = NULL;
  182.    self->m_nodePrev = NULL;
  183.    self->m_nodeParent = NULL;
  184.    return self;
  185. }
  186. QXmlNode *QXmlNodeToParent (QXmlNode *self)
  187. {
  188.    assert(self);
  189.    return self->m_nodeParent;
  190.    
  191. }
  192. QXmlNode *QXmlNodeToChild (QXmlNode *self,char *name)
  193. {
  194.    QXmlNode *tempNode;
  195.    assert(self);
  196.    tempNode = self->ToFirstChild(self);
  197.    if(name == NULL) return tempNode;
  198.    while(tempNode)
  199.    {
  200.       if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
  201.          return tempNode;
  202.       tempNode = tempNode->m_nodeNext;
  203.    }
  204.    return NULL;
  205. }
  206. QXmlNode *QXmlNodeToNextNode (QXmlNode *self,char *name)
  207. {
  208.    QXmlNode *tempNode;
  209.    assert(self);
  210.    tempNode = self->m_nodeNext;
  211.    if(name == NULL) return tempNode;
  212.    while(tempNode)
  213.    {
  214.       if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
  215.          return tempNode;
  216.       tempNode = tempNode->m_nodeNext;
  217.    }
  218.    return NULL;
  219. }
  220. QXmlNode *QXmlNodeToPrevNode (QXmlNode *self,char *name)
  221. {
  222.    QXmlNode *tempNode;
  223.    assert(self);
  224.    tempNode = self->m_nodePrev;
  225.    if(name == NULL) return tempNode;
  226.    while(tempNode)
  227.    {
  228.       if(tempNode->m_szName.Equal(&tempNode->m_szName,name))
  229.          return tempNode;
  230.       tempNode = tempNode->m_nodePrev;
  231.    }
  232.    return NULL;
  233. }
  234. QXmlNode *QXmlNodeToFirstChild (QXmlNode *self)
  235. {
  236.    QXmlNode *tempNode;
  237.    assert(self);
  238.    tempNode = self->m_nodeChild;
  239.    return tempNode;
  240. }
  241. QXmlNode *QXmlNodeToLastChild (QXmlNode *self)
  242.    QXmlNode *tempNode;
  243.    assert(self);
  244.    tempNode = self->m_nodeChild;
  245.    if(tempNode == NULL) return NULL;
  246.    while(tempNode->m_nodeNext)
  247.    {
  248.       tempNode = tempNode->m_nodeNext;
  249.    }
  250.    return tempNode;
  251. }
  252. BOOL QXmlNodeAddChild (QXmlNode *self,char *name ,char *value)
  253. {
  254.    QXmlNode *tempNode = NULL;
  255.    QXmlNode *tempChild = NULL;
  256.    assert(self);
  257.    if(name == NULL)return FALSE; 
  258.    
  259.    tempNode = MallocQXmlNode();
  260.    if(!tempNode) return FALSE;
  261.    tempNode->m_szName.AppendString(&tempNode->m_szName,name);
  262.    tempNode->m_szValue.AppendString(&tempNode->m_szValue,value);
  263.    tempChild = self->ToLastChild(self);
  264.    if(!tempChild) 
  265.       self->m_nodeChild = tempNode;
  266.    else
  267.       tempChild->m_nodeNext = tempNode;
  268.    return TRUE;
  269. }
  270. BOOL QXmlNodeAddNodeToChild (QXmlNode *self,QXmlNode *node)
  271. {
  272.    QXmlNode *tempChild;
  273.    assert(self);
  274.    
  275.    tempChild = self->ToLastChild(self);
  276.    if(!tempChild) 
  277.       self->m_nodeChild = node;
  278.    else
  279.    {
  280.       node->m_nodePrev = tempChild;
  281.       tempChild->m_nodeNext = node;
  282.    }
  283.    return TRUE;
  284. }
  285. BOOL QXmlNodeSetNodeName (QXmlNode *self , char *name)
  286. {
  287.    assert(self);
  288.    if(name == NULL)return FALSE; 
  289.    
  290.    self->m_szName.ReSet(&self->m_szName,name);
  291.    return TRUE;
  292. }
  293. BOOL QXmlNodeSetNodeValue (QXmlNode *self , char *value)
  294. {
  295.    assert(self);
  296.    if(value == NULL)return FALSE; 
  297.    
  298.    self->m_szValue.ReSet(&self->m_szValue,value);
  299.    return TRUE;
  300. }
  301. char *QXmlNodeGetNodeName (QXmlNode *self)
  302. {
  303.    assert(self);
  304.    return self->m_szName.m_dataBuff;
  305. }
  306. char *QXmlNodeGetNodeValue (QXmlNode *self)
  307. {
  308.    assert(self);
  309.    return self->m_szValue.m_dataBuff;
  310. }
  311. int QXmlNodeGetNodeData (QXmlNode *self,QString *data ,int depth)
  312. {
  313.    int i;
  314.    QString depthText;
  315.    assert(self);
  316.    if(!data) return -1;
  317.    //if(self->m_szName.IsEmpty(&self->m_szName)) return -1;
  318.    depthText = NewQString();
  319.    //case NODE:转换value,保存属性名,转换属性值,value
  320.    //case COMMENT:保存字段名
  321.    //case HOLD:保存属性名,转换属性值,value,
  322.    for(i = 0 ;i < depth ; i++) 
  323.    {
  324.       depthText.AppendString(&depthText,"    ");
  325.    }
  326.    if(self->m_nType == COMMENT)
  327.    {
  328.       /*注释 <!-- -->*/
  329.       data->AppendString(data,depthText.m_dataBuff);
  330.    //   QXmlDeGetEntity(&self->m_szName);
  331.       data->AppendString(data,commentHeader);
  332.       data->AppendString(data,self->m_szName.m_dataBuff);
  333.       data->AppendString(data,commentEnd);
  334.       data->AppendChar(data,'n');
  335.    }else if(self->m_nType == NODE || self->m_nType == HOLD)
  336.    {
  337.    //   if(self->m_nType == HOLD){ printf("exitn"); exit(1);}
  338.       data->AppendString(data,depthText.m_dataBuff);
  339.       data->AppendChar(data,'<');
  340.       data->AppendString(data,self->m_szName.m_dataBuff);
  341.       if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
  342.       {
  343.          QField *field;
  344.          do
  345.          {
  346.             field = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
  347.             data->AppendChar(data,' ');
  348.             
  349.             data->AppendString(data,field->name.m_dataBuff);
  350.             
  351.             data->AppendString(data, "=");
  352.             if(field->value.Include(&field->value,'"'))
  353.             {
  354.                data->AppendChar(data,''');
  355.                QXmlDeGetEntity(&field->value);
  356.                data->AppendString(data,field->value.m_dataBuff);
  357.                data->AppendChar(data,''');
  358.             }else
  359.             {
  360.                data->AppendChar(data,'"');
  361.                QXmlDeGetEntity(&field->value);
  362.                data->AppendString(data,field->value.m_dataBuff);
  363.                data->AppendChar(data,'"');   
  364.             }
  365.          }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
  366.       }
  367.       if(self->m_szValue.IsEmpty(&self->m_szValue)
  368.          && !self->m_nodeChild)
  369.       {
  370.          
  371.          data->AppendString(data,"/>n");
  372.       }
  373.       else
  374.       { 
  375.          //有值或子项
  376.          data->AppendString(data,">n");
  377.          data->AppendString(data,depthText.m_dataBuff);
  378.          if(!self->m_szValue.IsEmpty(&self->m_szValue))
  379.          {
  380.             if(self->m_nType == NODE )
  381.             {
  382.             //   printf("[%s]n",self->m_szValue.m_dataBuff);
  383.                QXmlDeGetEntity(&self->m_szValue);
  384.             }
  385.             
  386.             if (self->m_nType == HOLD )
  387.             {
  388.                data->AppendString(data,cdataHeader);
  389.                data->AppendString(data,self->m_szValue.m_dataBuff);
  390.                data->AppendString(data,cdataEnd);
  391.             }else
  392.             {
  393.                data->AppendString(data,self->m_szValue.m_dataBuff);
  394.             }
  395.             data->AppendChar(data,'n');
  396.          }
  397.          if(self->m_nodeChild)
  398.          {
  399.             QXmlNode *child = self->m_nodeChild;
  400.             while(child)
  401.             {
  402.                child->GetNodeData(child,data,depth+1);
  403.                child = child->m_nodeNext;
  404.             }
  405.             data->AppendChar(data,'n');
  406.          }
  407.          data->AppendString(data,depthText.m_dataBuff);
  408.          data->AppendString(data,"</");
  409.          data->AppendString(data,self->m_szName.m_dataBuff);
  410.          data->AppendString(data,">n");   
  411.       }
  412.    }
  413.    depthText.Release(&depthText);
  414.    return XML_OK;
  415. }
  416. int QXmlNodeGetNodeType (QXmlNode *self)
  417. {
  418.    assert(self);
  419.    return self->m_nType;
  420.    
  421. }
  422. BOOL QXmlNodeSetAttrib (QXmlNode *self , char *name , char * value)
  423. {
  424.    QField *pAttrib = NULL;
  425.    assert(self);
  426.    if(name == NULL)
  427.    {
  428.       return FALSE;
  429.    }
  430.    if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
  431.    {
  432.       QField *pAttrib;
  433.       do
  434.       {
  435.          pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
  436.          if(pAttrib->name.Equal(&pAttrib->name,name))
  437.          {
  438.             pAttrib->value.ReSet(&pAttrib->value,value);
  439.             return TRUE;
  440.          }
  441.          }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
  442.    }
  443.    pAttrib = MallocQField();
  444.    pAttrib->name.AppendString(&pAttrib->name,name);
  445.    pAttrib->value.AppendString(&pAttrib->value,value);
  446.    self->m_listAttrib.m_listField.Add(&self->m_listAttrib.m_listField,pAttrib);
  447.    
  448.    return TRUE;   
  449. }
  450. char *QXmlNodeGetAttrib (QXmlNode *self , char *name)
  451. {
  452.    assert(self);
  453.    if(name == NULL)
  454.    {
  455.       return NULL;
  456.    }
  457.    if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
  458.    {
  459.       QField *pAttrib;
  460.       do
  461.       {
  462.          pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
  463.          if(pAttrib->name.Equal(&pAttrib->name,name))
  464.          {
  465.             return pAttrib->value.m_dataBuff;
  466.          }
  467.          }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
  468.    }
  469.    return NULL;
  470. }
  471. BOOL QXmlNodeRemoveAttrib (QXmlNode *self , char *name)
  472. {
  473.    assert(self);
  474.    if(name == NULL)
  475.    {
  476.       return FALSE;
  477.    }
  478.    if(self->m_listAttrib.m_listField.MoveToHead(&self->m_listAttrib.m_listField) == QLIST_OK)
  479.    {
  480.       QField *pAttrib;
  481.       do
  482.       {
  483.          pAttrib = (QField *)self->m_listAttrib.m_listField.GetData(&self->m_listAttrib.m_listField);
  484.          if(pAttrib->name.Equal(&pAttrib->name,name))
  485.          {
  486.             return    self->m_listAttrib.m_listField.Remove(&self->m_listAttrib.m_listField) == QLIST_OK ?TRUE:FALSE;
  487.          }
  488.          }while(self->m_listAttrib.m_listField.MoveNext(&self->m_listAttrib.m_listField) == QLIST_OK);
  489.    }
  490.    return FALSE;
  491. }
  492. void QXmlNodeDestory (QXmlNode *self)
  493. {
  494.    assert(self);
  495.    //如果是root节点(parentNode == NULL),清空所有节点
  496.    //如果是第一个节点,就把父节点的第一个节点为下一个节点
  497.    //如果不是第一节点,就将上一个节点的下一个节点。指向下一个节点
  498.    if(self->m_nodeParent != NULL)
  499.    {
  500.       if(self->m_nodePrev == NULL)
  501.       {
  502.          self->m_nodeParent->m_nodeChild = self->m_nodeNext;
  503.       }
  504.       else
  505.       {
  506.          self->m_nodePrev->m_nodeNext = self->m_nodeNext;
  507.       }
  508.    }
  509.    
  510.    self->Release(self);
  511. }
  512. int QXmlDeGetEntity (QString *str)
  513. {
  514.    int i=0;
  515.    char c;
  516.    QString newStr = NewQString();
  517.    if(str == NULL) return -1;
  518.    newStr.AllocBuffer(&newStr,str->m_nDataLength);
  519.    while( i < str->m_nDataLength )
  520.    {
  521.       c = str->m_dataBuff[i];
  522.       if ( c == '&' )
  523.       {
  524.          newStr.AppendString(&newStr,entity[0].str);
  525.       }
  526.       else if ( c == '<' )
  527.       {
  528.          newStr.AppendString(&newStr,entity[1].str);
  529.       }
  530.       else if ( c == '>' )
  531.       {
  532.          newStr.AppendString(&newStr,entity[2].str);
  533.       
  534.       }
  535.       else if ( c == '"' )
  536.       {
  537.          newStr.AppendString(&newStr,entity[3].str);
  538.       
  539.       }
  540.       else if ( c == ''' )
  541.       {
  542.          newStr.AppendString(&newStr,entity[4].str);      
  543.       }
  544.       else
  545.       {
  546.          newStr.AppendChar(&newStr,c);
  547.       }
  548.       ++i;
  549.    }   
  550.    str->ReSet(str,newStr.m_dataBuff);
  551.    newStr.Release(&newStr);
  552.    return 1;
  553. }
  554. void QXmlReSet (QXml *self)
  555. {
  556.    assert(self);
  557.    self->Release(self);
  558.    self->m_nodeRoot = MallocQXmlNode();
  559. }
  560. void QXmlRelease (QXml *self)
  561. {
  562.    QXmlNode *tempNode;
  563.    QXmlNode *tempNodeNext;
  564.    QXmlNode *child;
  565.    QXmlNode *tempChild;
  566.    assert(self);
  567.    
  568.    self->m_listDeclare.Release(&self->m_listDeclare);
  569.    self->m_nCol = 0;
  570.    self->m_nErrorCode = XML_OK;
  571.    self->m_nRow = 0;
  572.    if(self->m_nodeRoot)
  573.    {
  574.       child = self->m_nodeRoot->m_nodeChild;
  575.       
  576.       while(child)
  577.       {
  578.          tempChild = child;
  579.          child = child->m_nodeNext;
  580.       
  581.          
  582.          tempChild->Release(tempChild);
  583.          KB_OSPFree(tempChild);
  584.          tempChild = NULL;
  585.       }
  586.       self->m_nodeRoot->m_nodeChild = NULL;
  587.       tempNode = self->m_nodeRoot->m_nodeNext;
  588.       while(tempNode)
  589.       {
  590.          tempNodeNext = tempNode;
  591.          tempNode = tempNode->m_nodeNext;
  592.       
  593.          tempNodeNext->Release(tempNodeNext);         
  594.          KB_OSPFree(tempNodeNext);
  595.          tempNodeNext = NULL;         
  596.       }      
  597.       self->m_nodeRoot->Release(self->m_nodeRoot);
  598.       KB_OSPFree(self->m_nodeRoot);
  599.       self->m_nodeRoot = NULL;
  600.    }
  601. }
  602. void QXmlNodeRelease (QXmlNode *self)
  603. {
  604.    QXmlNode *child;
  605.    QXmlNode *tempChild;
  606.    assert(self);
  607.    self->m_szName.Release(&self->m_szName);
  608.    self->m_szValue.Release(&self->m_szValue);
  609.    
  610.    // 应该处理子节点的数据 :(
  611.    child = self->m_nodeChild;
  612.    while(child)
  613.    {
  614.       tempChild = child;
  615.       child = child->m_nodeNext;
  616.       tempChild->Release(tempChild);
  617.       KB_OSPFree(tempChild);
  618.       tempChild = NULL;
  619.    }
  620.    self->m_listAttrib.Release(&self->m_listAttrib);
  621. }