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

DVD

开发平台:

C/C++

  1. #include <assert.h>
  2. #include "QXmlIO.h"
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. /*
  6.    getchar 用于从源读取数据
  7.    getbuff 调用getchar来保持临时数据
  8.    skipchar 用于从buff中删除数据
  9. */
  10. QXmlIO   NewQXmlIO()
  11. {
  12.    QXmlIO self;
  13.    self.FromFile = QXmlIOFromFile;
  14.    self.FromBuff = QXmlIOFromBuff;
  15.    self.GetBuff = QXmlIOGetBuff;
  16.    self.SkipWhiteSpace = QXmlIOSkipWhiteSpace;
  17.    self.SkipChar = QXmlIOSkipChar;
  18.    self.IsEmpty = QXmlIOIsEmpty;
  19.    self.Release = QXmlIORelease;
  20.    self.GetChar = NULL;
  21.    self.m_nRow = 0;
  22.    self.m_nCol = 0;
  23.    self.m_bDataOver = FALSE;
  24.    self.m_szTempBuff = NewQString();
  25.    return self;
  26. }
  27. QXmlIO   *MallocQXmlIO()
  28. {
  29.    QXmlIO *self = (QXmlIO *)KB_OSPMalloc(sizeof(QXmlIO));
  30.    if(!self) return NULL;
  31.    self->FromFile = QXmlIOFromFile;
  32.    self->FromBuff = QXmlIOFromBuff;
  33.    self->GetBuff = QXmlIOGetBuff;
  34.    self->SkipWhiteSpace = QXmlIOSkipWhiteSpace;
  35.    self->SkipChar = QXmlIOSkipChar;
  36.    self->IsEmpty = QXmlIOIsEmpty;
  37.    self->Release = QXmlIORelease;
  38.    self->GetChar = NULL;
  39.    self->m_nRow = 0;
  40.    self->m_nCol = 0;
  41.    self->m_bDataOver = FALSE;
  42.    self->m_szTempBuff = NewQString();
  43.    return self;
  44. }
  45. int   QXmlIOFromFile ( QXmlIO *self,int fd)
  46. {
  47.    assert(self);
  48.    if(fd == -1) return -1;
  49.    self->m_nFd = fd;
  50.    self->GetChar = QXmlIOGetFileChar;
  51.    return 1;
  52. }
  53. int QXmlIOFromBuff ( QXmlIO *self,char *buff)
  54. {
  55.    assert(self);
  56.    if(!buff) return -1;
  57.    self->m_pBuff = buff;
  58.    self->GetChar = QXmlIOGetBuffChar;
  59.    return 1;
  60. }
  61. int QXmlIOGetFileChar ( QXmlIO *self)
  62. {
  63.    char buf;
  64.    assert(self);
  65.    
  66.    if(read(self->m_nFd,&buf,sizeof(buf))<=0)
  67.    {
  68.       self->m_bDataOver = TRUE;
  69.       return 0;
  70.    }
  71.    if(buf == 'n' || buf =='r')
  72.    {
  73.       self->m_nRow ++;
  74.       self->m_nCol = 0;
  75.    }
  76.    else
  77.    {
  78.       self->m_nCol ++;
  79.    }
  80.    return buf;
  81. }
  82. int QXmlIOGetBuffChar ( QXmlIO *self)
  83. {
  84.    char buf;
  85.    assert(self);
  86.    if(!self->m_pBuff)
  87.    {
  88.       self->m_bDataOver = TRUE;
  89.       return 0;
  90.    }
  91.    buf = *self->m_pBuff++;
  92.    if(buf == 'n' || buf =='r')
  93.    {
  94.       self->m_nRow ++;
  95.       self->m_nCol = 0;
  96.    }
  97.    else
  98.    {
  99.       self->m_nCol ++;
  100.    }
  101.    return buf;
  102. }
  103. int QXmlIOGetBuff ( QXmlIO *self , char *buff,int len)
  104. {
  105.    char c;
  106.    int getlen = 0;
  107.    assert(self);
  108.    if(buff == NULL) return 0;
  109.    // 如果缓冲中的数据小于len,则需要重新申请数据
  110.    if(self->m_szTempBuff.IsEmpty(&self->m_szTempBuff)
  111.       ||self->m_szTempBuff.GetLength(&self->m_szTempBuff) < len)
  112.    {
  113.       c = self->GetChar(self);
  114.       if(!c) self->m_bDataOver = TRUE;
  115.       while(c)
  116.       {  
  117.          self->m_szTempBuff.AppendChar(&self->m_szTempBuff,c);
  118.  if (self->m_szTempBuff.m_nDataLength > len + XML_BUFF_DATA_LENGTH)
  119.  {
  120.     //thanks wangshujuan 
  121.     break;
  122.  }
  123.          c = self->GetChar(self);
  124.       }
  125.    }
  126.    if(len > self->m_szTempBuff.m_nDataLength) 
  127.       len = self->m_szTempBuff.m_nDataLength;
  128.    while( getlen < len)
  129.    {
  130.       *buff = self->m_szTempBuff.m_dataBuff[getlen];
  131.       getlen ++;
  132.       buff ++;
  133.    }   
  134.    return getlen;
  135. }
  136. void   QXmlIOSkipWhiteSpace (QXmlIO *self)
  137. {
  138.    char c;
  139.    assert(self);
  140.    self->m_szTempBuff.TrimLeft(&self->m_szTempBuff);
  141.    if(self->m_szTempBuff.IsEmpty(&self->m_szTempBuff))
  142.    {
  143.       c = self->GetChar(self);
  144.       if(!c) self->m_bDataOver = TRUE;
  145.       while(c && self->m_szTempBuff.m_nDataLength < XML_BUFF_DATA_LENGTH)
  146.       {
  147.          if(c!='t' || c!='n' || c!=' ' || c!='r')
  148.             self->m_szTempBuff.AppendChar(&self->m_szTempBuff,c);
  149.          c = self->GetChar(self);
  150.       }
  151.       if(c!='t' || c!='n' || c!=' ' || c!='r') self->m_szTempBuff.AppendChar(&self->m_szTempBuff,c);
  152.    }
  153. }
  154. int      QXmlIOSkipChar ( QXmlIO *self,int n)
  155. {
  156.    char c ;
  157.    assert(self);
  158.    if(n >= self->m_szTempBuff.GetLength(&self->m_szTempBuff))
  159.    {
  160.       /*缓冲区中的数据并不多*/
  161.       c = self->GetChar(self);
  162.       if(!c) self->m_bDataOver = TRUE;
  163.       while(c && self->m_szTempBuff.m_nDataLength < n + XML_BUFF_DATA_LENGTH)
  164.       {
  165.          self->m_szTempBuff.AppendChar(&self->m_szTempBuff,c);
  166.          c = self->GetChar(self);   
  167.       }
  168.       if(c) self->m_szTempBuff.AppendChar(&self->m_szTempBuff,c);
  169.    }
  170.    return self->m_szTempBuff.Delete(&self->m_szTempBuff,0,n);
  171. }
  172. // 是否有数据
  173. BOOL   QXmlIOIsEmpty ( QXmlIO *self)
  174. {
  175.    assert(self);
  176.    return self->m_bDataOver && self->m_szTempBuff.IsEmpty(&self->m_szTempBuff);   
  177. }
  178. void   QXmlIORelease ( QXmlIO *self)
  179. {
  180.    assert(self);   
  181.    self->m_szTempBuff.Release(&self->m_szTempBuff);
  182. }