Skip to content

Commit eb9236f

Browse files
use std::string everywhere now
1 parent 2253816 commit eb9236f

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

libwaffleizer.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,43 +70,61 @@ wflShiftN(wflBlobDsc* blob, size_t n)
7070
return new_blob;
7171
}
7272

73-
char *
73+
std::string
7474
wflShiftDouble(wflBlobDsc* blob)
7575
{
7676
char buf[10000];
7777
int ret, length;
7878
double * d;
79-
char * res;
79+
char * resc;
80+
std::string res;
81+
82+
8083
wflBlobDsc * b2 = wflShiftN(blob, sizeof(double));
81-
if (! b2) return NULL;
84+
if (! b2) return "";
8285

8386
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);
9087
wflFree(blob->mctx, b2);
88+
89+
int size_s = snprintf( nullptr, 0, "%.999g", *d) + 1;
90+
if (size_s <= 0)
91+
{
92+
printf("ai-ai-ai\n");
93+
return "";
94+
}
95+
96+
resc =(char *) malloc(size_s);
97+
if (! resc)
98+
{
99+
printf("oh-oh-oh\n");
100+
return "";
101+
}
102+
103+
ret = snprintf(resc,size_s,"%.999g", *d);
104+
if (ret <= 0)
105+
{
106+
printf("oi-oi-oi\n");
107+
free(resc);
108+
return "";
109+
}
110+
res = resc;
111+
free(resc);
91112
return res;
92113
}
93114

94115
std::string
95116
wflShiftPgPoint(wflBlobDsc* blob)
96117
{
97118
std::string res = "";
98-
char *a1, *a2;
119+
std::string x, y;
99120

100-
a1 = wflShiftDouble(blob);
101-
if (! a1) return res;
121+
x = wflShiftDouble(blob);
122+
if (x.empty()) return "";
102123

103-
a2 = wflShiftDouble(blob);
104-
if (! a2)
105-
{
106-
wflFree(blob->mctx, a1);
107-
return res;
108-
}
109-
res = (std::string) "(" + a1 +", " + a2 + ")";
124+
y = wflShiftDouble(blob);
125+
if (y.empty()) return "";
126+
127+
res = (std::string) "(" + x +", " + y + ")";
110128
return res;
111129
}
112130

libwaffleizer.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ void* wflMalloc(wflMemCtx * mctx, size_t size);
2424
void wflFree(wflMemCtx * mctx, void* ptr);
2525

2626
wflBlobDsc* wflShiftN(wflBlobDsc* blob, size_t n);
27-
char * wflShiftDouble(wflBlobDsc* blob);
27+
std::string wflShiftDouble(wflBlobDsc* blob);
2828
std::string wflShiftPgPoint(wflBlobDsc* blob);
2929
std::string wflShiftPgPath(wflBlobDsc* blob);
3030

3131

3232

33-
34-
35-
36-
3733
extern "C" {
3834

3935
int poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2);

0 commit comments

Comments
 (0)