-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataChunk.cpp
More file actions
142 lines (125 loc) · 3.14 KB
/
DataChunk.cpp
File metadata and controls
142 lines (125 loc) · 3.14 KB
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
/******************************************************************************
* @file DataChunk.cpp
*
* @brief
*
* @date 24-11-2010
* @author Rafal Kukla
******************************************************************************
* Copyright (C) 2010 Rafal Kukla ( rkdevel@gmail.com )
* This file is a part of rs232testng project and is released
* under the terms of the license contained in the file LICENSE
******************************************************************************
*/
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <QByteArray>
#include "DataChunk.h"
#include "debug.h"
DataChunk::~DataChunk()
{
clearData();
}
DataChunk::DataChunk(const QString& fromString, ChunkDataType asType )
{
type = asType;
objType = OBJ_QSTRING;
strObj = new QString( fromString );
}
DataChunk::DataChunk(const QByteArray& fromByteArray, ChunkDataType asType )
{
type = asType;
objType = OBJ_QBYTEARRAY;
rawObj = new QByteArray( fromByteArray );
}
DataChunk::DataChunk(const unsigned char* data, size_t size, ChunkDataType asType )
{
type = asType;
objType = OBJ_QBYTEARRAY;
rawObj = new QByteArray( reinterpret_cast<const char*>(data), size );
}
DataChunk::DataChunk(const char* text, ChunkDataType asType )
{
type = asType;
objType = OBJ_QBYTEARRAY;
rawObj = new QByteArray( text, strlen(text) );
}
QString DataChunk::toString()
{
if ( objType == OBJ_QSTRING )
{
return *strObj;
}
else if ( objType == OBJ_QBYTEARRAY )
{
/*
switch(type)
{
case DT_RAW:
case DT_ASCII: return QString::fromAscii(rawObj->constData,rawObj->size() );
case DT_STRING:
case DT_HTML: return OBJ_QSTRING;
}
*/
return QString( *rawObj );
}
return QString();
}
QByteArray DataChunk::toByteArray()
{
if ( objType == OBJ_QSTRING )
{
return strObj->toAscii();
}
else if ( objType == OBJ_QBYTEARRAY )
{
return QByteArray();
}
return QByteArray();
}
void DataChunk::clearData()
{
if(objPtr)
{
switch(objType)
{
case OBJ_QBYTEARRAY: delete rawObj; break;
case OBJ_QSTRING: delete strObj; break;
default: ;
}
objPtr = NULL;
}
#if 0
if ( (bufSize>embeddSize) && bufPtr)
{
free(bufPtr);
bufPtr = NULL;
}
bufSize = 0;
#endif
}
#if 0
void DataChunk::setData(const unsigned char* data, size_t size)
{
if (size < embeddSize )
{
clearData();
// Just copy into embedded buffer
if (size) memcpy(embeddBuf, data, size);
}
else if ( size <= ( bufSize+32 ) )
{
// Don't bother reallocation
memcpy(bufPtr, data, size);
}
else
{
clearData();
bufPtr = (unsigned char*) malloc(size);
if (!bufPtr) THROW( std::bad_alloc() );
memcpy(bufPtr, data, size);
}
bufSize = size;
}
#endif