6
6
7
7
#include " libblobstamper.h"
8
8
9
- wflMemCtx static_ctx;
10
9
11
- wflMemCtx *
12
- wflCreateMemCtx ()
10
+ Blob::Blob (char * data_in, int size_in)
13
11
{
14
- return &static_ctx;
12
+ data = data_in;
13
+ size = size_in;
14
+ begin = 0 ;
15
+ end = size;
15
16
}
16
17
17
-
18
- void
19
- wflDestroyMemCtx (wflMemCtx * mctx)
18
+ bool
19
+ Blob::isEmpty ()
20
20
{
21
+ if (! data) return true ;
22
+ return false ;
21
23
}
22
24
23
-
24
25
void
25
- wflBlobDump (wflBlobDsc* blob)
26
+ wflBlobDump (Blob blob)
26
27
{
27
- int length = blob-> end - blob-> begin + 1 ;
28
+ int length = blob. end - blob. begin + 1 ;
28
29
char * str =(char *) malloc (length + 1 ); // second +1 is for \0
29
30
// FIXME проверка null
30
31
str[0 ]=' \0 ' ;
31
32
32
- strncat (str, blob-> data + blob-> begin , length);
33
+ strncat (str, blob. data + blob. begin , length);
33
34
34
35
printf (" %s\n " ,str);
35
36
free (str);
36
37
}
37
38
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)
55
41
{
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
+ }
60
47
61
- new_blob = (wflBlobDsc*) wflMalloc ( blob-> mctx , sizeof (wflBlobDsc) );
48
+ Blob new_blob ( blob. data , blob. size );
62
49
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 ;
67
52
68
- blob-> begin += n;
53
+ blob. begin += n;
69
54
70
55
return new_blob;
71
56
}
72
57
73
58
std::string
74
- wflShiftDouble (wflBlobDsc* blob)
59
+ wflShiftDouble (Blob & blob)
75
60
{
76
61
int ret;
77
62
double * d;
78
63
char * resc;
79
64
std::string res;
80
65
81
66
82
- wflBlobDsc * b2 = wflShiftN (blob, sizeof (double ));
83
- if (! b2 ) return " " ;
67
+ Blob b2 = wflShiftN (blob, sizeof (double ));
68
+ if (b2. isEmpty () ) return " " ;
84
69
85
- d = (double *)( (char *)b2->data + b2->begin );
86
- wflFree (blob->mctx , b2);
70
+ d = (double *)( (char *)b2.data + b2.begin );
87
71
88
72
int size_s = snprintf ( nullptr , 0 , " %.999g" , *d) + 1 ;
89
73
if (size_s <= 0 )
@@ -112,7 +96,7 @@ wflShiftDouble(wflBlobDsc* blob)
112
96
}
113
97
114
98
std::string
115
- wflShiftPgPoint (wflBlobDsc* blob)
99
+ wflShiftPgPoint (Blob & blob)
116
100
{
117
101
std::string res = " " ;
118
102
std::string x, y;
@@ -129,7 +113,7 @@ wflShiftPgPoint(wflBlobDsc* blob)
129
113
130
114
131
115
std::string
132
- wflShiftPgPath (wflBlobDsc* blob)
116
+ wflShiftPgPath (Blob & blob)
133
117
{
134
118
std::string res = " " ;
135
119
std::string point;
@@ -154,29 +138,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
154
138
{
155
139
*res1 = NULL ;
156
140
*res2 = NULL ;
157
- wflMemCtx * mctx;
158
- wflBlobDsc blob;
159
- wflBlobDsc * b2;
160
141
161
142
std::string r1, r2;
143
+ Blob blob (in, in_size);
162
144
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);
172
146
173
147
if (r1.empty ())
174
148
{
175
149
fprintf (stderr," Problema1\n " );
176
150
return 1 ;
177
151
}
178
152
179
- r2 = wflShiftPgPath (& blob);
153
+ r2 = wflShiftPgPath (blob);
180
154
181
155
if (r2.empty ())
182
156
{
@@ -196,34 +170,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
196
170
int
197
171
poly_center_prepare (char * in, int in_size, char ** res2)
198
172
{
199
- // *res1 = NULL;
200
173
*res2 = NULL ;
201
- wflMemCtx * mctx;
202
- wflBlobDsc blob;
203
- wflBlobDsc * b2;
204
174
205
175
std::string r2;
176
+ Blob blob (in, in_size);
206
177
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);
217
179
218
180
if (r2.empty ())
219
181
{
220
182
fprintf (stderr," Problema2\n " );
221
183
return 1 ;
222
184
}
223
185
224
- // *res1 = (char *) malloc(strlen(r1.c_str()) + 1);
225
- // memcpy(*res1, r1.c_str(), strlen(r1.c_str()) + 1);
226
-
227
186
*res2 = (char *) malloc (strlen (r2.c_str ())+1 );
228
187
memcpy (*res2, r2.c_str (), strlen (r2.c_str ()) + 1 );
229
188
0 commit comments