8000 Blob is now cpp object, and get rid of memctx · postgrespro/libblobstamper@4d8423c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d8423c

Browse files
Blob is now cpp object, and get rid of memctx
1 parent 622085e commit 4d8423c

File tree

3 files changed

+53
-75
lines changed

3 files changed

+53
-75
lines changed

cpp_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ char blob_data[]="aaalkjdhfs89345yu3ifhjew;lkhf4;lt;o34ithp;eriuwtgp;etup568p34t
1111

1212
int main()
1313
{
14+
15+
Blob bl(blob_data,strlen(blob_data));
16+
1417
char *res1, *res2;
1518

1619
poly_contain_prepare(blob_data,strlen(blob_data),&res1, &res2);

libblobstamper.cpp

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,68 @@
66

77
#include "libblobstamper.h"
88

9-
wflMemCtx static_ctx;
109

11-
wflMemCtx *
12-
wflCreateMemCtx()
10+
Blob::Blob (char * data_in, int size_in)
1311
{
14-
return &static_ctx;
12+
data = data_in;
13+
size = size_in;
14+
begin = 0;
15+
end = size;
1516
}
1617

17-
18-
void
19-
wflDestroyMemCtx(wflMemCtx * mctx)
18+
bool
19+
Blob::isEmpty ()
2020
{
21+
if (! data) return true;
22+
return false;
2123
}
2224

23-
2425
void
25-
wflBlobDump(wflBlobDsc* blob)
26+
wflBlobDump(Blob blob)
2627
{
27-
int length = blob->end - blob->begin + 1 ;
28+
int length = blob.end - blob.begin + 1 ;
2829
char * str =(char *) malloc(length + 1); // second +1 is for \0
2930
// FIXME проверка null
3031
str[0]='\0';
3132

32-
strncat(str, blob->data + blob->begin, length);
33+
strncat(str, blob.data + blob.begin, length);
3334

3435
printf("%s\n",str);
3536
free(str);
3637
}
3738

38-
void*
39-
wflMalloc(wflMemCtx * mctx, size_t size)
40-
{
41-
/*just that simple for now*/
42-
return malloc( size );
43-
}
44-
45-
void
46-
wflFree(wflMemCtx * mctx, void* ptr)
47-
{
48-
/*just that simple for now*/
49-
free(ptr);
50-
}
51-
52-
53-
wflBlobDsc*
54-
wflShiftN(wflBlobDsc* blob, size_t n)
39+
Blob
40+
wflShiftN(Blob &blob, size_t n)
5541
{
56-
wflBlobDsc* new_blob;
57-
// FIXME null check here;
58-
if (blob->begin + n > blob->end)
59-
return NULL; /*not enough data*/
42+
if (blob.begin + n > blob.end)
43+
{
44+
Blob empty(NULL, -1);
45+
return empty; /*not enough data*/
46+
}
6047

61-
new_blob = (wflBlobDsc*) wflMalloc(blob->mctx, sizeof(wflBlobDsc));
48+
Blob new_blob(blob.data, blob.size);
6249

63-
new_blob->data = blob->data;
64-
new_blob->begin = blob->begin;
65-
new_blob->end = blob->begin + n - 1;
66-
new_blob->mctx = blob->mctx;
50+
new_blob.begin = blob.begin;
51+
new_blob.end = blob.begin + n - 1;
6752

68-
blob->begin += n;
53+
blob.begin += n;
6954

7055
return new_blob;
7156
}
7257

7358
std::string
74-
wflShiftDouble(wflBlobDsc* blob)
59+
wflShiftDouble(Blob &blob)
7560
{
7661
int ret;
7762
double * d;
7863
char * resc;
7964
std::string res;
8065

8166

82-
wflBlobDsc * b2 = wflShiftN(blob, sizeof(double));
83-
if (! b2) return "";
67+
Blob b2 = wflShiftN(blob, sizeof(double));
68+
if (b2.isEmpty()) return "";
8469

85-
d = (double *)( (char*)b2->data + b2->begin);
86-
wflFree(blob->mctx, b2);
70+
d = (double *)( (char*)b2.data + b2.begin);
8771

8872
int size_s = snprintf( nullptr, 0, "%.999g", *d) + 1;
8973
if (size_s <= 0)
@@ -112,7 +96,7 @@ wflShiftDouble(wflBlobDsc* blob)
11296
}
11397

11498
std::string
115-
wflShiftPgPoint(wflBlobDsc* blob)
99+
wflShiftPgPoint(Blob &blob)
116100
{
117101
std::string res = "";
118102
std::string x, y;
@@ -129,7 +113,7 @@ wflShiftPgPoint(wflBlobDsc* blob)
129113

130114

131115
std::string
132-
wflShiftPgPath(wflBlobDsc* blob)
116+
wflShiftPgPath(Blob &blob)
133117
{
134118
std::string res = "";
135119
std::string point;
@@ -154,29 +138,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
154138
{
155139
*res1 = NULL;
156140
*res2 = NULL;
157-
wflMemCtx * mctx;
158-
wflBlobDsc blob;
159-
wflBlobDsc * b2;
160141

161142
std::string r1, r2;
143+
Blob blob(in, in_size);
162144

163-
mctx = wflCreateMemCtx();
164-
165-
blob.mctx = mctx;
166-
blob.data = in;
167-
blob.begin = 0;
168-
blob.end = in_size;
169-
170-
171-
r1 = wflShiftPgPoint(&blob);
145+
r1 = wflShiftPgPoint(blob);
172146

173147
if (r1.empty())
174148
{
175149
fprintf(stderr,"Problema1\n");
176150
return 1;
177151
}
178152

179-
r2 = wflShiftPgPath(& blob);
153+
r2 = wflShiftPgPath(blob);
180154

181155
if (r2.empty())
182156
{
@@ -196,34 +170,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
196170
int
197171
poly_center_prepare(char* in, int in_size, char ** res2)
198172
{
199-
// *res1 = NULL;
200173
*res2 = NULL;
201-
wflMemCtx * mctx;
202-
wflBlobDsc blob;
203-
wflBlobDsc * b2;
204174

205175
std::string r2;
176+
Blob blob(in, in_size);
206177

207-
mctx = wflCreateMemCtx();
208-
209-
blob.mctx = mctx;
210-
blob.data = in;
211-
blob.begin = 0;
212-
blob.end = in_size;
213-
214-
215-
216-
r2 = wflShiftPgPath(& blob);
178+
r2 = wflShiftPgPath(blob);
217179

218180
if (r2.empty())
219181
{
220182
fprintf(stderr,"Problema2\n");
221183
return 1;
222184
}
223185

224-
// *res1 = (char *) malloc(strlen(r1.c_str()) + 1);
225-
// memcpy(*res1, r1.c_str(), strlen(r1.c_str()) + 1);
226-
227186
*res2 = (char *) malloc(strlen(r2.c_str())+1);
228187
memcpy(*res2, r2.c_str(), strlen(r2.c_str()) + 1);
229188

libblobstamper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11

2+
3+
class Blob
4+
{
5+
private:
6+
public:
7+
Blob(char * data, int size);
8+
bool isEmpty ();
9+
10+
char * data; /*FIZME потом сделать private*/
11+
int size;
12+
int begin;
13+
int end;
14+
15+
};
16+
17+
218
typedef struct wflMemCtx
319
{
420
//Nothing for now;

0 commit comments

Comments
 (0)
0