File: DataPack.h

package info (click to toggle)
capi4hylafax 1%3A01.02.03-10sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,856 kB
  • ctags: 2,966
  • sloc: cpp: 20,126; sh: 7,703; makefile: 215; perl: 36
file content (415 lines) | stat: -rwxr-xr-x 11,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*---------------------------------------------------------------------------*\

    Copyright (C) 2000 AVM GmbH. All rights reserved.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY, without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, and WITHOUT
    ANY LIABILITY FOR ANY DAMAGES arising out of or in connection
    with the use or performance of this software. See the
    GNU General Public License for further details.

\*---------------------------------------------------------------------------*/

#ifndef _DATAPACK_H_
#define _DATAPACK_H_

#include "aTypes.h"
#include "aString.h"
#include "InfoType.h"

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

#ifndef DATA_PACKET_HANDLE_COUNT
#define DATA_PACKET_HANDLE_COUNT    1       // have to be 1 or greater!!!
#endif

#define iWrn_Pack_DestFull          CalculateInfoValue (infoType_Packet, infoKind_Warning, 1)
#define iWrn_Pack_BothFull          CalculateInfoValue (infoType_Packet, infoKind_Warning, 1)
#define iErr_Pack_NotAllowed        CalculateInfoValue (infoType_Packet, infoKind_Error, 1)
#define iErr_Pack_LenToSmall        CalculateInfoValue (infoType_Packet, infoKind_Error, 2)

/*===========================================================================*\
\*===========================================================================*/

class CBasicDataPacketQueue {
public:
    virtual class CDataPacket *RecvPacket (void) = 0;
    virtual void ReturnPacket (class CDataPacket *pPacket) = 0;
};


/*===========================================================================*\
\*===========================================================================*/

class CDataPacket {
public:
    CDataPacket (void);
    virtual ~CDataPacket (void) {}

    // Pointer
    tUByte *GetPointer (void);
    tUByte *GetPointer (tUInt offset);
    tUByte *GetPosPointer (void);
    tUByte *GetLenPointer (void);

    // Length
    tUInt GetLen (void);
    tUInt GetFreeLen (void);
    tUInt GetMaxLen (void);
    void  SetLen (tUInt len);
    void  SetLenToMax (void);
    void  AddLen (tUInt len);
    void  SubLen (tUInt len);
    tBool LenIsMax (void);
    tBool IsEmpty (void);

    // Position
    tUInt GetPos (void);
    tUInt DiffPosToLen (void);
    tUInt DiffPosToMaxLen (void);
    tBool SetPos (tUInt pos);
    void  SetPosToLen (void);
    tBool AddPos (tUInt pos);
    tBool PosIsLen (void);
    tBool PosIsMax (void);

    // Handle
    void *GetHandle (tUInt number = 0); // number < DATA_PACKET_HANDLE_COUNT
    void SetHandle (void *handle, tUInt number = 0); // number < DATA_PACKET_HANDLE_COUNT

    // Queue
    CDataPacket *GetNextPacket (void);
    void SetNextPacket (CDataPacket *pPacket);
    void ReturnPacket (void);
    void SetPacketQueue (CBasicDataPacketQueue *pQueue);

    // Copy
    tSInfo Copy (CDataPacket *pPacket); // return: i_Okay, iWrn_Pack_DestFull, iWrn_Pack_BothFull

    // Resize
    virtual tSInfo Resize (tUInt NewMaxLen);

protected:
    void SetPointer (tUByte *DataPointer);
    void SetMaxLen (tUInt MaxLen);

private:
    CBasicDataPacketQueue *m_pQueue;
    CDataPacket           *m_pNext;
    tUByte                *m_Pointer;
    tUInt                  m_MaxLen;
    tUInt                  m_Len;
    tUInt                  m_Pos;
    void                  *m_Handle[DATA_PACKET_HANDLE_COUNT];
};


/*===========================================================================*\
\*===========================================================================*/

class CDataPacketStore : public CDataPacket {
public:
    CDataPacketStore (void) {}
    CDataPacketStore (tUInt MaxLen);
    ~CDataPacketStore (void);

    tSInfo Resize (tUInt NewMaxLen);
};

class CDataPacketReference : public CDataPacket {
public:
    CDataPacketReference (void) {}
    CDataPacketReference (tUByte *pPointer, tUInt MaxLen, tUInt Len = 0, void *FirstHandle = 0);

    void InitToMax (tUByte *pPointer, tUInt Len);
    void InitToMax (tUByte *pPointer, tUInt Len, void *FirstHandle);
    void InitAll (tUByte *pPointer, tUInt MaxLen, tUInt Len = 0);
    void InitAll (tUByte *pPointer, tUInt MaxLen, void *FirstHandle);
    void InitAll (tUByte *pPointer, tUInt MaxLen, tUInt Len, void *FirstHandle);
};


/*===========================================================================*\
    CDataPacket - inline functions
\*===========================================================================*/

inline CDataPacket::CDataPacket (void)
  : m_pQueue  (0),
    m_pNext   (0),
    m_Pointer (0),
    m_MaxLen  (0),
    m_Len     (0),
    m_Pos     (0) {

    s_memzero (m_Handle, DATA_PACKET_HANDLE_COUNT * sizeof (void *));
}

inline tUByte *CDataPacket::GetPointer (void) {
    return m_Pointer;
}

inline tUByte *CDataPacket::GetPointer (tUInt offset) {
    return m_Pointer + offset;
}

inline tUByte *CDataPacket::GetPosPointer (void) {
    return (m_Pos < m_MaxLen) ? GetPointer (m_Pos) : 0;
}

inline tUByte *CDataPacket::GetLenPointer (void) {
    return GetPointer (m_Len);
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tUInt CDataPacket::GetLen (void) {
    return m_Len;
}

inline tUInt CDataPacket::GetFreeLen (void) {
    return m_MaxLen - m_Len;
}

inline tUInt CDataPacket::GetMaxLen (void) {
    return m_MaxLen;
}

inline void CDataPacket::SetLen (tUInt len) {
    // assert (len <= MaxLen);
    m_Len = len;
    if (m_Pos > len) {
        SetPosToLen();
    }
}

inline void CDataPacket::SetLenToMax (void) {
    m_Len = m_MaxLen;
}

inline void CDataPacket::AddLen (tUInt len) {
    // assert (len + m_Len <= MaxLen);
    m_Len += len;
}

inline void CDataPacket::SubLen (tUInt len) {
    // assert (m_Len >= len);
    m_Len -= len;
    if (m_Pos > m_Len) {
        SetPosToLen();
    }
}

inline tBool CDataPacket::LenIsMax (void) {
    return (m_MaxLen == m_Len) ? vTrue : vFalse;
}

inline tBool CDataPacket::IsEmpty (void) {
    return (m_Len == 0);
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tUInt CDataPacket::GetPos (void) {
    return m_Pos;
}

inline tUInt CDataPacket::DiffPosToLen (void) {
    return m_Len - m_Pos;
}

inline tUInt CDataPacket::DiffPosToMaxLen (void) {
    return m_MaxLen - m_Pos;
}

inline tBool CDataPacket::SetPos (tUInt pos) {
    if (pos < m_Len) {
        m_Pos = pos;
        return vTrue;
    }
    SetPosToLen();
    return vFalse;
}

inline void CDataPacket::SetPosToLen (void) {
    m_Pos = m_Len;
}

inline tBool CDataPacket::AddPos (tUInt pos) {
    m_Pos += pos;
    if (m_Pos <= m_Len) {
        return vTrue;
    }
    SetPosToLen();
    return vFalse;
}

inline tBool CDataPacket::PosIsLen (void) {
    return (m_Pos == m_Len);
}

inline tBool CDataPacket::PosIsMax (void) {
    return (m_Pos == m_MaxLen);
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline void *CDataPacket::GetHandle (tUInt number) {
    return m_Handle[number];
}

inline void CDataPacket::SetHandle (void *handle, tUInt number) {
    m_Handle[number] = handle;
}

inline CDataPacket *CDataPacket::GetNextPacket (void) {
    return m_pNext;
}

inline void CDataPacket::SetNextPacket (CDataPacket *pPacket) {
    m_pNext = pPacket;
}

inline void CDataPacket::ReturnPacket (void) {
    // assert (m_pQueue != 0);
    m_pQueue->ReturnPacket (this);
}

inline void CDataPacket::SetPacketQueue (CBasicDataPacketQueue *pQueue) {
    m_pQueue = pQueue;
}

inline tSInfo CDataPacket::Copy (CDataPacket *pPacket) {
    // assert (pPacket != 0);
    tUInt l1 = GetFreeLen();
    tUInt l2 = pPacket->DiffPosToLen();
    tSInfo fret;
    switch (a_compare (l1, l2)) {
    default:
        // dassert (0);
        // no break!
    case vEqual:
        fret = iWrn_Pack_BothFull;
        break;
    case vLower:
        fret = iWrn_Pack_DestFull;
        break;
    case vHigher:
        l1   = l2;
        fret = i_Okay;
        break;
    }
    s_memcpy (GetLenPointer(), pPacket->GetPosPointer(), l1);
    AddLen (l1);
    pPacket->AddPos (l1);
    return fret;
}

inline tSInfo CDataPacket::Resize (tUInt) {
    return iErr_Pack_NotAllowed;
}

inline void CDataPacket::SetPointer (tUByte *DataPointer) {
    m_Pointer = DataPointer;
}

inline void CDataPacket::SetMaxLen (tUInt MaxLen) {
    m_MaxLen = MaxLen;
    if (m_Len > MaxLen) {
        SetLen (MaxLen);
    }
}


/*===========================================================================*\
    Other CDataPacket Classes
\*===========================================================================*/

inline CDataPacketStore::CDataPacketStore (tUInt MaxLen) {
    // dassert (MaxLen > 0);
    tUByte *Buffer = new tUByte [MaxLen];
    if (Buffer) {
        SetPointer (Buffer);
        SetMaxLen (MaxLen);
    }
}

inline CDataPacketStore::~CDataPacketStore (void) {
    tUByte *Buffer = GetPointer();
    SetPointer (0);
    delete [] Buffer;
}

inline tSInfo CDataPacketStore::Resize (tUInt NewMaxLen) {
    if (NewMaxLen < GetLen()) {
        return iErr_Pack_LenToSmall;
    }
    tUByte *Buffer = 0;
    if (NewMaxLen) {
        Buffer = new tUByte [NewMaxLen];
        if (!Buffer) {
            return iErr_OutOfMemory;
        }
    }
    tUByte *oldBuf = GetPointer();
    // dassert ((GetLen() == 0) || (oldBuf != 0));
    s_memcpy (oldBuf, Buffer, GetLen());
    SetPointer (Buffer);
    SetMaxLen (NewMaxLen);
    delete [] oldBuf;
    return i_Okay;
}

inline CDataPacketReference::CDataPacketReference (tUByte *pPointer, tUInt MaxLen, tUInt Len, void *FirstHandle) {
    // dassert (DATA_PACKET_HANDLE_COUNT >= 1);
    InitAll (pPointer, MaxLen, Len, FirstHandle);
}

inline void CDataPacketReference::InitToMax (tUByte *pPointer, tUInt Len) {
    InitAll (pPointer, Len, Len);
}

inline void CDataPacketReference::InitToMax (tUByte *pPointer, tUInt Len, void *FirstHandle) {
    // dassert (DATA_PACKET_HANDLE_COUNT >= 1);
    InitAll (pPointer, Len, Len, FirstHandle);
}

inline void CDataPacketReference::InitAll (tUByte *pPointer, tUInt MaxLen, tUInt Len) {
    SetPointer (pPointer);
    SetMaxLen (MaxLen);
    SetPos (0);
    SetLen (Len);
}

inline void CDataPacketReference::InitAll (tUByte *pPointer, tUInt MaxLen, void *FirstHandle) {
    // dassert (DATA_PACKET_HANDLE_COUNT >= 1);
    SetPointer (pPointer);
    SetMaxLen (MaxLen);
    SetPos (0);
    SetLen (0);
    SetHandle (FirstHandle);
}

inline void CDataPacketReference::InitAll (tUByte *pPointer, tUInt MaxLen, tUInt Len, void *FirstHandle) {
    // dassert (DATA_PACKET_HANDLE_COUNT >= 1);
    SetPointer (pPointer);
    SetMaxLen (MaxLen);
    SetPos (0);
    SetLen (Len);
    SetHandle (FirstHandle);
}


/*===========================================================================*\
\*===========================================================================*/

#endif