forked from shouxieai/tensorRT_Pro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin_binary_io.cpp
More file actions
174 lines (144 loc) · 3.13 KB
/
Copy pathplugin_binary_io.cpp
File metadata and controls
174 lines (144 loc) · 3.13 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
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
#include "plugin_binary_io.hpp"
#include "ilogger.hpp"
#include <string.h>
namespace Plugin{
using namespace std;
BinIO::~BinIO(){
close();
}
bool BinIO::opened(){
if (flag_ == MemoryRead)
return memoryRead_ != nullptr;
else if (flag_ == MemoryWrite)
return true;
return false;
}
void BinIO::close(){
if (flag_ == MemoryRead) {
memoryRead_ = nullptr;
memoryCursor_ = 0;
memoryLength_ = -1;
}
else if (flag_ == MemoryWrite) {
memoryWrite_.clear();
memoryCursor_ = 0;
memoryLength_ = -1;
}
}
string BinIO::readData(int numBytes){
string output;
output.resize(numBytes);
int readlen = read((void*)output.data(), output.size());
output.resize(readlen);
return output;
}
int BinIO::read(void* pdata, size_t length){
if (flag_ == MemoryRead) {
if (memoryLength_ != -1) {
if (memoryLength_ < memoryCursor_ + length) {
int remain = memoryLength_ - memoryCursor_;
if (remain > 0) {
memcpy(pdata, memoryRead_ + memoryCursor_, remain);
memoryCursor_ += remain;
return remain;
}
else {
return -1;
}
}
}
memcpy(pdata, memoryRead_ + memoryCursor_, length);
memoryCursor_ += length;
return length;
}
else {
return -1;
}
}
bool BinIO::eof(){
if (!opened()) return true;
if (flag_ == MemoryRead){
return this->memoryCursor_ >= this->memoryLength_;
}
else if (flag_ == MemoryWrite){
return false;
}
else {
opstate_ = false;
INFO("Unsupport flag: %d", flag_);
return true;
}
}
int BinIO::write(const void* pdata, size_t length){
if (flag_ == MemoryWrite) {
memoryWrite_.append((char*)pdata, (char*)pdata + length);
return length;
}
else {
return -1;
}
}
int BinIO::writeData(const string& data){
return write(data.data(), data.size());
}
BinIO& BinIO::operator >> (string& value){
//read
int length = 0;
(*this) >> length;
value = readData(length);
return *this;
}
int BinIO::readInt(){
int value = 0;
(*this) >> value;
return value;
}
float BinIO::readFloat(){
float value = 0;
(*this) >> value;
return value;
}
BinIO& BinIO::operator << (const string& value){
//write
(*this) << (int)value.size();
writeData(value);
return *this;
}
BinIO& BinIO::operator << (const char* value){
int length = strlen(value);
(*this) << (int)length;
write(value, length);
return *this;
}
BinIO& BinIO::operator << (const vector<string>& value){
(*this) << (int)value.size();
for (int i = 0; i < value.size(); ++i){
(*this) << value[i];
}
return *this;
}
BinIO& BinIO::operator >> (vector<string>& value){
int num;
(*this) >> num;
value.resize(num);
for (int i = 0; i < value.size(); ++i)
(*this) >> value[i];
return *this;
}
bool BinIO::openMemoryRead(const void* ptr, int memoryLength) {
close();
if (!ptr) return false;
memoryRead_ = (const char*)ptr;
memoryCursor_ = 0;
memoryLength_ = memoryLength;
flag_ = MemoryRead;
return true;
}
void BinIO::openMemoryWrite() {
close();
memoryWrite_.clear();
memoryCursor_ = 0;
memoryLength_ = -1;
flag_ = MemoryWrite;
}
}; // namespace Plugin