8000 Move libwfl functions to separate file · postgrespro/libblobstamper@01c1be4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01c1be4

Browse files
Move libwfl functions to separate file
1 parent d42e746 commit 01c1be4

File tree

4 files changed

+232
-145
lines changed

4 files changed

+232
-145
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ all: test cpp_test
1414
test: $(LIB_OBJS) test.o
1515
$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
1616

17-
cpp_test: $(LIB_OBJS) cpp_test.o
17+
cpp_test: $(LIB_OBJS) cpp_test.o libwaffleizer.o
1818
$(CXX) $(LDFLAGS) $^ -o $@ $(LDLIBS)
1919

2020
%.o: %.cpp $(DEPS)

cpp_test.cpp

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -4,151 +4,8 @@
44

55
#include <string>
66

7+
#include "libwaffleizer.h"
78

8-
typedef struct wflMemCtx
9-
{
10-
//Nothing for now;
11-
}wflMemCtx ;
12-
13-
14-
wflMemCtx static_ctx;
15-
16-
wflMemCtx *
17-
wflCreateMemCtx()
18-
{
19-
return &static_ctx;
20-
}
21-
22-
23-
void
24-
wflDestroyMemCtx(wflMemCtx * mctx)
25-
{
26-
}
27-
28-
typedef struct wflBlobDsc
29-
{
30-
struct wflMemCtx * mctx;
31-
char * data;
32-
int begin;
33-
int end;
34-
}wflBlobDsc;
35-
36-
37-
void
38-
wflBlobDump(wflBlobDsc* blob)
39-
{
40-
int length = blob->end - blob->begin + 1 ;
41-
char * str =(char *) malloc(length + 1); // second +1 is for \0
42-
// FIXME проверка null
43-
str[0]='\0';
44-
45-
strncat(str, blob->data + blob->begin, length);
46-
47-
printf("%s\n",str);
48-
free(str);
49-
}
50-
51-
void*
52-
wflMalloc(wflMemCtx * mctx, size_t size )
53-
{
54-
/*just that simple for now*/
55-
return malloc( size );
56-
}
57-
58-
void
59-
wflFree(wflMemCtx * mctx, void* ptr)
60-
{
61-
/*just that simple for now*/
62-
free(ptr);
63-
}
64-
65-
66-
wflBlobDsc*
67-
wflShiftN(wflBlobDsc* blob, size_t n)
68-
{
69-
wflBlobDsc* new_blob;
70-
// FIXME null check here;
71-
if (blob->begin + n > blob->end)
72-
return NULL; /*not enough data*/
73-
74-
new_blob = (wflBlobDsc*) wflMalloc(blob->mctx, sizeof(wflBlobDsc));
75-
76-
new_blob->data = blob->data;
77-
new_blob->begin = blob->begin;
78-
new_blob->end = blob->begin + n - 1;
79-
new_blob->mctx = blob->mctx;
80-
81-
blob->begin += n;
82-
83-
return new_blob;
84-
}
85-
86-
char *
87-
wflShiftDouble(wflBlobDsc* blob)
88-
{
89-
char buf[1000];
90-
int ret, length;
91-
double * d;
92-
char * res;
93-
wflBlobDsc * b2 = wflShiftN(blob, sizeof(double));
94-
if (! b2) return NULL;
95-
96-
d = (double *)( (char*) b2->data + blob->begin);
97-
98-
ret = snprintf(buf,1000,"%.999g",*d);
99-
// FIXME анализировать ret
100-
length = strlen(buf);
101-
res = (char*) wflMalloc(blob->mctx,length+1);
102-
memcpy(res,buf,length+1);
103-
wflFree(blob->mctx, b2);
104-
return res;
105-
}
106-
107-
char *
108-
wflShiftPgPoint(wflBlobDsc* blob)
109-
{
110-
char buf[1000];
111-
int ret, length;
112-
char *res;
113-
char *a1, *a2;
114-
115-
a1 = wflShiftDouble(blob);
116-
if (! a1) return NULL;
117-
118-
a2 = wflShiftDouble(blob);
119-
if (! a2) return NULL;
120-
121-
ret = snprintf(buf,1000,"(%s, %s)",a1, a2);
122-
// FIXME анализировать ret
123-
124-
length = strlen(buf);
125-
res = (char*) wflMalloc(blob->mctx,length+1);
126-
memcpy(res,buf,length + 1);
127-
wflFree(blob->mctx, a1);
128-
wflFree(blob->mctx, a2);
129-
130-
return res;
131-
}
132-
133-
134-
char *
135-
wflShiftPgPath(wflBlobDsc* blob)
136-
{
137-
std::string res = "";
138-
char *pc, *pres;
139-
140-
while (pc = wflShiftPgPoint(blob))
141-
{
142-
if (!res.empty()) res = res + ", ";
143-
res = res + pc;
144-
wflFree(blob->mctx, pc);
145-
}
146-
res = "[" + res + "]";
147-
pres = (char*) wflMalloc(blob->mctx, res.size() + 1);
148-
memcpy(pres,res.c_str(), res.size() + 1);
149-
return pres;
150-
151-
}
1529

15310
char blob_data[]="aaalkjdhfs89345yu3ifhjew;lkhf4;lt;o34ithp;eriuwtgp;etup568p34tuewritwe;rtgj;ewoty;4w85tyeihwritgzzz";
15411

libwaffleizer.cpp

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#include <string.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#include <string>
6+
7+
#include "libwaffleizer.h"
8+
9+
wflMemCtx static_ctx;
10+
11+
wflMemCtx *
12+
wflCreateMemCtx()
13+
{
14+
return &static_ctx;
15+
}
16+
17+
18+
void
19+
wflDestroyMemCtx(wflMemCtx * mctx)
20+
{
21+
}
22+
23+
24+
void
25+
wflBlobDump(wflBlobDsc* blob)
26+
{
27+
int length = blob->end - blob->begin + 1 ;
28+
char * str =(char *) malloc(length + 1); // second +1 is for \0
29+
// FIXME проверка null
30+
str[0]='\0';
31+
32+
strncat(str, blob->data + blob->begin, length);
33+
34+
printf("%s\n",str);
35+
free(str);
36+
}
37+
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)
55+
{
56+
wflBlobDsc* new_blob;
57+
// FIXME null check here;
58+
if (blob->begin + n > blob->end)
59+
return NULL; /*not enough data*/
60+
61+
new_blob = (wflBlobDsc*) wflMalloc(blob->mctx, sizeof(wflBlobDsc));
62+
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;
67+
68+
blob->begin += n;
69+
70+
return new_blob;
71+
}
72+
73+
char *
74+
wflShiftDouble(wflBlobDsc* blob)
75+
{
76+
char buf[10000];
77+
int ret, length;
78+
double * d;
79+
char * res;
80+
wflBlobDsc * b2 = wflShiftN(blob, sizeof(double));
81+
if (! b2) return NULL;
82+
83+
d = (double *)( (char*)b2->data + b2->begin);
84+
85+
ret = snprintf(buf,10000,"%.999g",*d);
86+
// FIXME анализировать ret
87+
length = strlen(buf);
88+
res = (char*) wflMalloc(blob->mctx,length+1);
89+
memcpy(res, buf, length+1);
90+
wflFree(blob->mctx, b2);
91+
return res;
92+
}
93+
94+
char *
95+
wflShiftPgPoint(wflBlobDsc* blob)
96+
{
97+
char buf[10000];
98+
int ret, length;
99+
char *res;
100+
char *a1, *a2;
101+
102+
a1 = wflShiftDouble(blob);
103+
if (! a1) return NULL;
104+
105+
a2 = wflShiftDouble(blob);
106+
if (! a2)
107+
{
108+
wflFree(blob->mctx, a1);
109+
return NULL;
110+
}
111+
ret = snprintf(buf,10000,"(%s, %s)",a1, a2);
112+
// FIXME анализировать ret
113+
114+
length = strlen(buf);
115+
res = (char*) wflMalloc(blob->mctx,length+1);
116+
memcpy(res,buf,length + 1);
117+
wflFree(blob->mctx, a1);
118+
wflFree(blob->mctx, a2);
119+
120+
return res;
121+
}
122+
123+
124+
char *
125+
wflShiftPgPath(wflBlobDsc* blob)
126+
{
127+
std::string res = "";
128+
char *pc, *pres;
129+
130+
while (pc = wflShiftPgPoint(blob))
131+
{
132+
if (!res.empty()) res = res + ", ";
133+
res = res + pc;
134+
wflFree(blob->mctx, pc);
135+
}
136+
res = "(" + res + ")";
137+
pres = (char*) wflMalloc(blob->mctx, res.size() + 1);
138+
memcpy(pres,res.c_str(), res.size() + 1);
139+
return pres;
140+
141+
}
142+
143+
extern "C" {
144+
int
145+
poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
146+
{
147+
char str[]="124567";
148+
149+
wflMemCtx * mctx;
150+
wflBlobDsc blob;
151+
wflBlobDsc * b2;
152+
char *r1, *r2;
153+
154+
mctx = wflCreateMemCtx();
155+
156+
blob.mctx = mctx;
157+
blob.data = in;
158+
blob.begin = 0;
159+
blob.end = in_size;
160+
161+
162+
r1 = wflShiftPgPoint(&blob);
163+
164+
if (! r1)
165+
{
166+
fprintf(stderr,"Problema\n");
167+
return 1;
168+
}
169+
170+
r2 = wflShiftPgPath(& blob);
171+
172+
if (! r2)
173+
{
174+
fprintf(stderr,"Problema\n");
175+
return 1;
176+
}
177+
178+
*res1 = (char *) malloc(strlen(r1)+1);
179+
memcpy(*res1, r1,strlen(r1) + 1);
180+
free(r1);
181+
182+
*res2 = (char *) malloc(strlen(r2)+1);
183+
memcpy(*res2, r2,strlen(r2) + 1);
184+
free(r2);
185+
186+
return 0;
187+
}
188+
189+
}

libwaffleizer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
typedef struct wflMemCtx
3+
{
4+
//Nothing for now;
5+
}wflMemCtx ;
6+
7+
extern wflMemCtx static_ctx;
8+
9+
wflMemCtx * wflCreateMemCtx();
10+
void wflDestroyMemCtx(wflMemCtx * mctx);
11+
12+
13+
typedef struct wflBlobDsc
14+
{
15+
struct wflMemCtx * mctx;
16+
char * data;
17+
int begin;
18+
int end;
19+
}wflBlobDsc;
20+
21+
22+
void wflBlobDump(wflBlobDsc* blob);
23+
void* wflMalloc(wflMemCtx * mctx, size_t size);
24+
void wflFree(wflMemCtx * mctx, void* ptr);
25+
26+
wflBlobDsc* wflShiftN(wflBlobDsc* blob, size_t n);
27+
char * wflShiftDouble(wflBlobDsc* blob);
28+
char * wflShiftPgPoint(wflBlobDsc* blob);
29+
char * wflShiftPgPath(wflBlobDsc* blob);
30+
31+
32+
33+
34+
35+
36+
37+
extern "C" {
38+
39+
int poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2);
40+
41+
}

0 commit comments

Comments
 (0)
0