~ubuntu-branches/ubuntu/wily/xmoto/wily-proposed

« back to all changes in this revision

Viewing changes to src/DBuffer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-09-14 21:01:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060914210120-bvr7yu9rafb3fivp
Tags: 0.2.1-1
* New upstream release.
* Removed manpages and desktop files from the Debian package as they are now
  provided upstream.
* Make the package binNMUable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 *  Generic data buffer stuff.
24
24
 */
25
25
#include "DBuffer.h"
 
26
#include "arch/SwapEndian.h"
 
27
#include <algorithm>
26
28
 
27
29
namespace vapp {
28
30
 
53
55
    m_bOwnData = false;
54
56
  }
55
57
 
56
 
  void DBuffer::writeBuf(const char *pcBuf,int nBufSize) {
 
58
  template <typename _ConstIter>
 
59
  void DBuffer::writeBuf(_ConstIter pcBuf,int nBufSize) {
57
60
    if(isOutput() && nBufSize > 0) {    
58
61
      /* Start writing into the part */
59
62
      int nToWrite = nBufSize,i = 0;
73
76
        int nWrite = nRem < nBufSize-i ? nRem : nBufSize-i;
74
77
        
75
78
        /* Write it */
76
 
        memcpy(&m_Parts[m_nCurPart]->pcBuffer[m_Parts[m_nCurPart]->nPtr],&pcBuf[i],nWrite);
 
79
        std::copy(pcBuf, pcBuf + nWrite,
 
80
          &m_Parts[m_nCurPart]->pcBuffer[m_Parts[m_nCurPart]->nPtr]);
 
81
        pcBuf += nWrite;
77
82
        i += nWrite;
78
83
        m_Parts[m_nCurPart]->nPtr += nWrite;
79
84
        nToWrite -= nWrite;      
80
85
      }
81
86
    }   
82
87
  }
 
88
  
 
89
  // Instantiations
 
90
  template void DBuffer::writeBuf(const char *, int);
 
91
  template void DBuffer::writeBuf(char *, int);
 
92
  
 
93
  void DBuffer::writeBuf_LE(const char *pcBuf,int nBufSize) {
 
94
    if (SwapEndian::bigendien) {
 
95
      writeBuf(std::reverse_iterator<const char *>(pcBuf + nBufSize), nBufSize);
 
96
    } else {
 
97
      writeBuf(pcBuf, nBufSize);
 
98
    }
 
99
  }
83
100
 
84
101
  void DBuffer::_NewPart(void) {
85
102
    DBufferPart *p = new DBufferPart;
89
106
    m_Parts.push_back( p );
90
107
  }
91
108
 
92
 
  void DBuffer::readBuf(char *pcBuf,int nBufSize) {
 
109
  template <typename _Iter>
 
110
  void DBuffer::readBuf(_Iter pcBuf,int nBufSize) {
93
111
    if(isInput() && nBufSize > 0) {
94
112
      /* Remaining in input buffer? */
95
113
      if(m_nSize - m_nReadPtr < nBufSize) {
96
114
        /* TODO: error */
97
 
        memset(pcBuf,0,nBufSize);
 
115
        std::fill_n(pcBuf, nBufSize, 0);
98
116
      }
99
117
      else {
100
118
        /* Read and advance ptr */
101
 
        memcpy(pcBuf,&m_pcData[m_nReadPtr],nBufSize);
 
119
        std::copy(&m_pcData[m_nReadPtr], &m_pcData[m_nReadPtr] + nBufSize,
 
120
          pcBuf);
102
121
        m_nReadPtr += nBufSize;
103
122
      }
104
123
    }
105
124
  }
106
125
 
 
126
  // Instantiations
 
127
  template void DBuffer::readBuf(char *, int);
 
128
 
 
129
  void DBuffer::readBuf_LE(char *pcBuf,int nBufSize) {
 
130
    readBuf(SwapEndian::LittleIter(pcBuf, nBufSize), nBufSize);
 
131
  }
 
132
 
107
133
  int DBuffer::numRemainingBytes(void) {  
108
134
    if(isInput()) {
109
135
      return m_nSize - m_nReadPtr;
140
166
    return NULL;
141
167
  }
142
168
 
143
 
};
 
169
  void DBuffer::write(std::string s) {
 
170
    *this << s.length();
 
171
    this->writeBuf(s.c_str(), s.length());
 
172
  }
 
173
  
 
174
  void DBuffer::read(std::string &s) {
 
175
    int n;
 
176
    char c[256];
 
177
    *this >> n;
 
178
    if(n <= 0) {
 
179
      throw Exception("Unable to read the string !");
 
180
    }
 
181
    this->readBuf(c, n);
 
182
    c[n] = '\0';
 
183
    s = c;
 
184
  }
144
185
 
 
186
}