8000 Introduce concept of Stamp. Implement Stapms for binary and sting rep… · postgrespro/libblobstamper@14637b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14637b7

Browse files
Introduce concept of Stamp. Implement Stapms for binary and sting representation of double
1 parent bf3f011 commit 14637b7

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed

libblobstamper.cpp

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ Blob::ShiftBytes(size_t n)
105105
return new_blob;
106106
}
107107

108+
void *
109+
Blob::ShiftSingleStampBin(StampGeneric& stmp)
110+
{
111+
return stmp.Extract(*this);
112+
}
113+
114+
std::string
115+
Blob::ShiftSingleStampStr(StampGeneric& stmp)
116+
{
117+
return stmp.ExtractStr(*this);
118+
}
119+
120+
108121
std::string
109122
wflShiftDouble(Blob &blob)
110123
{
@@ -151,10 +164,12 @@ wflShiftPgPoint(Blob &blob)
151164
std::string res = "";
152165
std::string x, y;
153166

154-
x = wflShiftDouble(blob);
167+
StampStrDouble stamp;
168+
169+
x = blob.ShiftSingleStampStr(stamp);
155170
if (x.empty()) return "";
156171

157-
y = wflShiftDouble(blob);
172+
y = blob.ShiftSingleStampStr(stamp);
158173
if (y.empty()) return "";
159174

160175
res = (std::string) "(" + x +", " + y + ")";
@@ -181,3 +196,58 @@ wflShiftPgPath(Blob &blob)
181196
res = "(" + res + ")";
182197
return res;
183198
}
199+
200+
201+
StampBinDouble::StampBinDouble()
202+
{
203+
min_size = sizeof(double);
204+
max_size = sizeof(double);
205+
is_fixed_size = true;
206+
}
207+
208+
void *
209+
StampBinDouble::Extract(Blob &blob)
210+
{
211+
Blob blob2 = blob.ShiftBytes(min_size);
212+
if (blob2.isEmpty())
213+
return NULL;
214+
void *res = malloc(min_size);
215+
memcpy(res, blob2.data + blob2.begin, min_size);
216+
return res;
217+
}
218+
219+
std::string
220+
StampStrDouble::ExtractStr(Blob &blob)
221+
{
222+
std::string res = "";
223+
double *pd = (double *)this->Extract(blob);
224+
if (! pd)
225+
return res;
226+
227+
int size_s = snprintf( nullptr, 0, "%.999g", *pd) + 1;
228+
if (size_s <= 0)
229+
{
230+
printf("ai-ai-ai\n");
231+
return "";
232+
}
233+
234+
char * resc =(char *) malloc(size_s);
235+
if (! resc)
236+
{
237+
printf("oh-oh-oh\n");
238+
return "";
239+
}
240+
241+
int ret = snprintf(resc,size_s,"%.999g", *pd);
242+
if (ret <= 0)
243+
{
244+
printf("oi-oi-oi\n");
245+
free(resc);
246+
return "";
247+
}
248+
res = resc;
249+
free(resc);
250+
free(pd);
251+
return res;
252+
}
253+

libblobstamper.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

22
#include <string>
3+
#include <stdio.h>
4+
5+
class StampGeneric;
6+
37
class Blob
48
{
59
private:
@@ -9,16 +13,54 @@ class Blob
913
void Dump();
1014
Blob ShiftBytes(size_t n);
1115

12-
char * data; /*FIXME потом сделать private*/
16+
void * ShiftSingleStampBin(StampGeneric &stmp);
17+
std::string ShiftSingleStampStr(StampGeneric &stmp);
18+
19+
char* data; /*FIXME потом сделать private*/
1320
int size;
1421
int begin;
1522
int end;
1623
};
1724

25+
class StampGeneric
26+
{
27+
protected:
28+
bool is_fixed_size;
29+
int min_size;
30+
int max_size;
31+
public:
32+
bool isFixedSize() {return is_fixed_size;}
33+
int minSize() {return min_size;}
34+
int maxSize() {return max_size;}
35+
36+
virtual void * Extract(Blob &blob) {printf ("1111111\n"); return NULL;}
37+
virtual std::string ExtractStr(Blob &blob) {printf ("22222\n"); return "";}
38+
39+
};
40+
41+
class StampBinDouble: public StampGeneric
42+
{
43+
public:
44+
StampBinDouble();
45+
void * Extract(Blob &blob) override;
46+
47+
48+
};
49+
50+
51+
class StampStrDouble: public StampBinDouble
52+
{
53+
public:
54+
// StampStrDouble();
55+
StampStrDouble() : StampBinDouble() {}
56+
57+
std::string ExtractStr(Blob &blob) override;
58+
59+
};
60+
1861
Blob wflShiftN(Blob &blob, size_t n);
1962
std::string wflShiftDouble(Blob &blob);
2063
std::string wflShiftPgPoint(Blob &blob);
2164
std::string wflShiftPgPath(Blob &blob);
2265

23-
24-
66+
void hexdump(void *pAddressIn, long lSize);

test_libblobstamper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,22 @@ main(void)
3232
printf("got non-empty result. SOMETHING IS REALLY WRONG!!!!\n");
3333

3434

35+
StampBinDouble stmp_double;
36+
37+
void* res = bl.ShiftSingleStampBin(stmp_double);
38+
39+
hexdump(res,sizeof(double));
40+
free(res);
41+
42+
StampStrDouble stmp_str_double;
43+
44+
std::string str = stmp_str_double.ExtractStr(bl);
45+
46+
printf("-------- %s\n", str.c_str());
47+
48+
str = wflShiftPgPoint(bl);
49+
50+
printf("++++++++ %s\n", str.c_str());
51+
3552
return 0;
3653
}

0 commit comments

Comments
 (0)
0