Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd42e746

Browse files
еще эксперементы
1 parent987cf19 commitd42e746

File tree

3 files changed

+266
-11
lines changed

3 files changed

+266
-11
lines changed

‎Makefile‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ ifeq ($(origin CC),default)
77
endif
88

99
.PHONY: all
10-
all: test
10+
all: test cpp_test
1111
@echo All done!
1212

1313

1414
test:$(LIB_OBJS) test.o
1515
$(CC)$(LDFLAGS)$^ -o$@$(LDLIBS)
1616

17+
cpp_test:$(LIB_OBJS) cpp_test.o
18+
$(CXX)$(LDFLAGS)$^ -o$@$(LDLIBS)
19+
1720
%.o:%.cpp$(DEPS)
18-
$(CC)$(CFLAGS)$<
21+
$(CXX) -c -g$(CFLAGS)$<
22+
23+
%.o:%.c$(DEPS)
24+
$(CC) -c -g$(CXXFLAGS)$<
1925

2026
.PHONY: clean
2127
clean:
22-
rm -f*.otest
28+
rm -f*.otest cpp_test
2329
@echo Clean done!

‎cpp_test.cpp‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include<string.h>
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
5+
#include<string>
6+
7+
8+
typedefstructwflMemCtx
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+
typedefstructwflBlobDsc
29+
{
30+
structwflMemCtx * 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+
returnmalloc( 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+
returnNULL;/*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)returnNULL;
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)returnNULL;
117+
118+
a2 =wflShiftDouble(blob);
119+
if (! a2)returnNULL;
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+
}
152+
153+
char blob_data[]="aaalkjdhfs89345yu3ifhjew;lkhf4;lt;o34ithp;eriuwtgp;etup568p34tuewritwe;rtgj;ewoty;4w85tyeihwritgzzz";
154+
155+
intmain()
156+
{
157+
wflMemCtx * mctx;
158+
wflBlobDsc blob;
159+
wflBlobDsc * b2;
160+
char* str;
161+
162+
mctx =wflCreateMemCtx();
163+
164+
blob.mctx = mctx;
165+
blob.data = blob_data;
166+
blob.begin =0;
167+
blob.end =strlen(blob_data)-1;
168+
169+
170+
wflBlobDump(&blob);
171+
printf("-----\n");
172+
b2 =wflShiftN(&blob,3);
173+
174+
str =wflShiftDouble(&blob);
175+
176+
printf("\n aaa = %s\n-----\n",str);
177+
178+
str =wflShiftPgPoint(&blob);
179+
180+
printf("\n aaa = %s\n-----\n",str);
181+
182+
str =wflShiftPgPath(&blob);
183+
184+
printf("\n zzz = %s\n-----\n",str);
185+
186+
wflBlobDump(&blob);
187+
wflBlobDump(b2);
188+
189+
wflDestroyMemCtx(mctx);
190+
}

‎test.c‎

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,30 @@ void
3737
wflBlobDump(wflBlobDsc*blob)
3838
{
3939
intlength=blob->end-blob->begin+1 ;
40-
char*str=malloc(length+1);// second +1 is for \0
40+
char*str=(char*)malloc(length+1);// second +1 is for \0
4141
// FIXME проверка null
4242
str[0]='\0';
4343

44-
45-
46-
strncat(str,blob->data+blob->begin,length);// second +1 is for \0
44+
strncat(str,blob->data+blob->begin,length);
4745

4846
printf("%s\n",str);
4947
free(str);
5048
}
5149

5250
void*
53-
wflMalloc(size_tsize )
51+
wflMalloc(wflMemCtx*mctx,size_tsize )
5452
{
5553
/*just that simple for now*/
5654
returnmalloc(size );
5755
}
5856

57+
void
58+
wflFree(wflMemCtx*mctx,void*ptr)
59+
{
60+
/*just that simple for now*/
61+
free(ptr);
62+
}
63+
5964

6065
wflBlobDsc*
6166
wflShiftN(wflBlobDsc*blob,size_tn)
@@ -65,18 +70,64 @@ wflShiftN(wflBlobDsc* blob, size_t n)
6570
if (blob->begin+n>blob->end)
6671
returnNULL;/*not enough data*/
6772

68-
new_blob=wflMalloc(sizeof(wflBlobDsc));
73+
new_blob=(wflBlobDsc*)wflMalloc(blob->mctx,sizeof(wflBlobDsc));
6974

7075
new_blob->data=blob->data;
7176
new_blob->begin=blob->begin;
7277
new_blob->end=blob->begin+n-1;
78+
new_blob->mctx=blob->mctx;
7379

7480
blob->begin+=n;
7581

7682
returnnew_blob;
7783
}
7884

85+
char*
86+
wflShiftDouble(wflBlobDsc*blob)
87+
{
88+
charbuf[1000];
89+
intret,length;
90+
double*d;
91+
char*res;
92+
wflBlobDsc*b2=wflShiftN(blob,sizeof(double));
93+
d= (double*)b2->data;
94+
if (!d)returnNULL;
95+
96+
printf("%.999g",*d);
97+
ret=snprintf(buf,1000,"%.999g",*d);
98+
// FIXME анализировать ret
99+
length=strlen(buf);
100+
res= (char*)wflMalloc(blob->mctx,length+1);
101+
memcpy(res,buf,length+1);
102+
wflFree(blob->mctx,b2);
103+
returnres;
104+
}
79105

106+
char*
107+
wflShiftPgPoint(wflBlobDsc*blob)
108+
{
109+
charbuf[1000];
110+
intret,length;
111+
char*res;
112+
char*a1,*a2;
113+
114+
a1=wflShiftDouble(blob);
115+
if (!a1)returnNULL;
116+
117+
a2=wflShiftDouble(blob);
118+
if (!a2)returnNULL;
119+
120+
ret=snprintf(buf,1000,"(%s, %s)",a1,a2);
121+
// FIXME анализировать ret
122+
123+
length=strlen(buf);
124+
res= (char*)wflMalloc(blob->mctx,length+1);
125+
memcpy(res,buf,length+1);
126+
wflFree(blob->mctx,a1);
127+
wflFree(blob->mctx,a2);
128+
129+
returnres;
130+
}
80131

81132
charblob_data[]="aaalkjdhfs89345yu3ifhjew;lkhf4;lt;o34ithp;eriuwtgp;etup568p34tuewritwe;rtgj;ewoty;4w85tyeihwritgzzz";
82133

@@ -85,6 +136,7 @@ int main()
85136
wflMemCtx*mctx;
86137
wflBlobDscblob;
87138
wflBlobDsc*b2;
139+
char*str;
88140

89141
mctx=wflCreateMemCtx();
90142

@@ -94,10 +146,17 @@ int main()
94146
blob.end=strlen(blob_data)-1;
95147

96148

97-
98149
wflBlobDump(&blob);
99150
printf("-----\n");
100-
b2=wflShiftN(&blob,9993);
151+
b2=wflShiftN(&blob,3);
152+
153+
str=wflShiftDouble(&blob);
154+
155+
printf("\n aaa = %s\n-----\n",str);
156+
157+
str=wflShiftPgPoint(&blob);
158+
159+
printf("\n aaa = %s\n-----\n",str);
101160

102161

103162
wflBlobDump(&blob);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp