Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/Vcmisc/vcstrip.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vchdr.h"
21
22
/* Stripping head and tail data.
23
**
24
** Written by Kiem-Phong Vo ([email protected])
25
*/
26
27
#define ST_LINE 01 /* dealing with lines of text */
28
#define ST_HEAD 02 /* specifying header to strip */
29
#define ST_TAIL 04 /* specifying tail to strip */
30
31
typedef struct _strip_s
32
{ int type;
33
ssize_t head; /* amount to strip from head */
34
ssize_t tail; /* amount to strip from tail */
35
} Strip_t;
36
37
static Vcmtarg_t _Stripargs[] =
38
{ { "nl", "Data is line-oriented", (Void_t*)ST_LINE },
39
{ "head", "Header to strip is 'head=amount'", (Void_t*)ST_HEAD },
40
{ "tail", "Tail to strip is 'tail=amount'", (Void_t*)ST_TAIL },
41
{ 0, "Not stripping anything, only processing 'nl' if specified", (Void_t*)0 }
42
};
43
44
#if __STD_C
45
static ssize_t strip(Vcodex_t* vc, const Void_t* data, size_t size, Void_t** out)
46
#else
47
static ssize_t strip(vc, data, size, out)
48
Vcodex_t* vc;
49
const Void_t* data;
50
size_t size;
51
Void_t** out;
52
#endif
53
{
54
ssize_t z, dsz, hsz, tsz;
55
Vcchar_t *dt, *edt, *hdt, *tdt, *output;
56
Strip_t *st;
57
Vcio_t io;
58
59
if(!vc || !(st = vcgetmtdata(vc,Strip_t*)) )
60
return -1;
61
62
/* set valid interval of data to be processed */
63
edt = (dt = (Vcchar_t*)data) + (dsz = (ssize_t)size);
64
if(!(st->type&ST_LINE) )
65
vc->undone = 0;
66
else /* dealing with lines of text */
67
{ for(; edt > dt; --edt)
68
if(edt[-1] == '\n')
69
break;
70
vc->undone = size - (edt-dt); /* size of partial line */
71
dsz = edt - dt; /* size of data to be processed */
72
}
73
74
/* find an appropriate head amount to strip */
75
hdt = dt; hsz = 0;
76
if(st->type&ST_LINE)
77
{ for(z = st->head; z > 0; --z)
78
{ for(; dt < edt; ++dt)
79
if(*dt == '\n')
80
break;
81
if((dt += 1) >= edt)
82
break;
83
}
84
hsz = dt - hdt;
85
}
86
else
87
{ if((hsz = st->head) > dsz)
88
hsz = dsz;
89
dt += hsz;
90
}
91
dsz -= hsz;
92
93
/* find an appropriate tail amount to strip */
94
tdt = edt; tsz = 0;
95
if(st->type&ST_LINE)
96
{ for(z = st->tail; z > 0; --z)
97
{ if((tdt -= 1) <= dt)
98
break;
99
for(; tdt > dt; --tdt)
100
if(tdt[-1] == '\n')
101
break;
102
}
103
tsz = edt - tdt;
104
}
105
else
106
{ if((tsz = st->tail) > dsz)
107
tsz = dsz;
108
tdt -= tsz;
109
}
110
dsz -= tsz;
111
112
/* recode the middle part of the data */
113
if(dsz > 0 && vcrecode(vc, &dt, &dsz, 0, 0) < 0 )
114
RETURN(-1);
115
116
/* allocate output buffer */
117
z = vcsizeu(hsz) + hsz + vcsizeu(tsz) + tsz + vcsizeu(dsz) + dsz;
118
if(!(output = vcbuffer(vc, NIL(Vcchar_t*), z, 0)) )
119
RETURN(-1);
120
121
/* write out the transformed data */
122
vcioinit(&io, output, z);
123
vcioputu(&io, hsz);
124
vcioputs(&io, hdt, hsz);
125
vcioputu(&io, tsz);
126
vcioputs(&io, tdt, tsz);
127
vcioputu(&io, dsz);
128
vcioputs(&io, dt, dsz);
129
130
if(out)
131
*out = (Void_t*)output;
132
return z;
133
}
134
135
136
#if __STD_C
137
static ssize_t unstrip(Vcodex_t* vc, const Void_t* data, size_t size, Void_t** out)
138
#else
139
static ssize_t unstrip(vc, data, size, out)
140
Vcodex_t* vc;
141
const Void_t* data;
142
size_t size;
143
Void_t** out;
144
#endif
145
{
146
Vcchar_t *hdt, *tdt, *dt, *output;
147
ssize_t hsz, tsz, dsz, z;
148
Vcio_t io;
149
150
if(!vc)
151
return -1;
152
vc->undone = 0;
153
154
vcioinit(&io, (Vcchar_t*)data, size);
155
156
if((hsz = vciogetu(&io)) < 0 || hsz > vciomore(&io))
157
RETURN(-1);
158
hdt = vcionext(&io); vcioskip(&io, hsz);
159
160
if((tsz = vciogetu(&io)) < 0 || tsz > vciomore(&io))
161
RETURN(-1);
162
tdt = vcionext(&io); vcioskip(&io, tsz);
163
164
if((dsz = vciogetu(&io)) < 0 || dsz > vciomore(&io))
165
RETURN(-1);
166
dt = vcionext(&io); vcioskip(&io, dsz);
167
if(dsz > 0 && vcrecode(vc, &dt, &dsz, 0, 0) < 0 )
168
RETURN(-1);
169
170
z = hsz + dsz + tsz;
171
if(!(output = vcbuffer(vc, NIL(Vcchar_t*), z, 0)) )
172
RETURN(-1);
173
174
memcpy(output, hdt, hsz);
175
memcpy(output+hsz, dt, dsz);
176
memcpy(output+hsz+dsz, tdt, tsz);
177
178
if(out)
179
*out = output;
180
181
return z;
182
}
183
184
#if __STD_C
185
static int stripevent(Vcodex_t* vc, int type, Void_t* params)
186
#else
187
static int stripevent(vc, type, params)
188
Vcodex_t* vc;
189
int type;
190
Void_t* params;
191
#endif
192
{
193
Strip_t *st;
194
Vcmtarg_t *arg;
195
char *data, val[1024];
196
197
if(type == VC_OPENING)
198
{ if(!(st = calloc(1, sizeof(Strip_t))) )
199
return -1;
200
201
for(data = (char*)params; data; )
202
{ data = vcgetmtarg(data, val, sizeof(val), _Stripargs, &arg);
203
switch(TYPECAST(int,arg->data) )
204
{
205
case ST_LINE:
206
st->type = ST_LINE;
207
break;
208
case ST_HEAD:
209
st->head = (ssize_t)vcatoi(val);
210
break;
211
case ST_TAIL:
212
st->tail = (ssize_t)vcatoi(val);
213
break;
214
}
215
}
216
217
vcsetmtdata(vc, st);
218
}
219
else if(type == VC_CLOSING)
220
{ if((st = vcgetmtdata(vc, Strip_t*)) )
221
free(st);
222
223
vcsetmtdata(vc, NIL(Void_t*));
224
}
225
226
return 0;
227
}
228
229
Vcmethod_t _Vcstrip =
230
{ strip,
231
unstrip,
232
stripevent,
233
"strip", "Strip head and tail data.",
234
"[-version?strip (AT&T Research) 2009-03-12]" USAGE_LICENSE,
235
_Stripargs,
236
1024*1024,
237
0
238
};
239
240
VCLIB(Vcstrip)
241
242