TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmisc
intex.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
#include "
libtpcmisc.h
"
7
/*****************************************************************************/
8
9
/*****************************************************************************/
10
12
void
intInit
(
14
INT_list
*l
15
) {
16
if
(l==NULL)
return
;
17
l->
nr
=0;
18
l->
i
=NULL;
19
return
;
20
}
21
23
void
intEmpty
(
25
INT_list
*l
26
) {
27
if
(l==NULL)
return
;
28
if
(l->
nr
>0) free(l->
i
);
29
l->
nr
=0;
30
l->
i
=NULL;
31
return
;
32
}
33
43
int
intExpand
(
char
*text,
INT_list
*list) {
44
int
j;
45
char
*p, *t;
46
int
first, last,
swap
, intMax=65536;
47
48
/* Check the arguments */
49
if
(strlen(text)<1)
return
(1);
50
intEmpty
(list);
51
52
/* Expand */
53
p=strtok(text,
" ,;.&\t\n\r\0"
);
54
while
(p!=NULL) {
55
t=p; first=last=-1;
56
while
((*t!=
'-'
) && (!isdigit((
int
)*t)) && (*t)) t++;
57
if
(*t==
'-'
) {
58
while
((!isdigit((
int
)*t)) && (*t)) t++;
59
if
(isdigit((
int
)*t)) {first=0; last=atoi(t);}
60
}
else
if
(isdigit((
int
)*t)) {
61
first=atoi(t);
/*if (first==0) first=1;*/
62
while
((isdigit((
int
)*t)) && (*t)) t++;
63
if
(*t ==
'-'
) {
64
t++;
while
((!isdigit((
int
)*t)) && (*t)) t++;
65
if
(isdigit((
int
)*t)) last=atoi(t);
else
last=intMax;
66
}
67
}
68
if
((first>=0) && (last>=0)) {
69
if
(first>last) {
swap
=first; first=last; last=
swap
;}
70
if
(last>intMax) {
if
(first<=intMax) last=intMax;
else
last=0;}
71
for
(j=first; j<=last && list->
nr
<intMax; j++)
72
if
(
_intexadd
(list, j)<0)
return
(2);
73
}
else
if
(first>=0) {
74
if
(first<=intMax)
if
(
_intexadd
(list, first)<0)
return
(3);
75
}
else
if
(last>=0) {
76
if
(last>=intMax)
if
(
_intexadd
(list, last)<0)
return
(4);
77
}
78
p=strtok(NULL,
" ,;.&\t\n\r\0"
);
79
}
80
if
(list->
nr
<1)
return
(1);
81
return
(0);
82
}
83
/*****************************************************************************/
84
85
/*****************************************************************************/
92
int
_intexadd
(
INT_list
*list,
int
a) {
93
int
i, j, n;
94
95
/* Check if list is yet empty */
96
if
(list->
nr
==0) {
97
list->
i
=(
int
*)malloc(
sizeof
(
int
));
if
(list->
i
==NULL)
return
(-1);
98
/* Put the first integer to list and return */
99
list->
nr
=1; list->
i
[0]=a;
return
(1);
100
}
101
n=list->
nr
;
102
/* Check through the existing list */
103
for
(i=0; i<n; i++) {
104
/* if it already is listed, just return */
105
if
(list->
i
[i]==a)
return
(0);
106
/* make room for this integer */
107
list->
i
=(
int
*)realloc(list->
i
, (n+1)*
sizeof
(
int
));
if
(list==NULL)
return
(-1);
108
if
(list->
i
[i]>a) {
for
(j=n-1; j>=i; j--) list->
i
[j+1]=list->
i
[j];
break
;}
109
}
110
list->
i
[i]=a; list->
nr
=n+1;
111
return
(1);
112
}
113
/*****************************************************************************/
114
115
/*****************************************************************************/
124
INT_list
intMerge
(
INT_list
*list1,
INT_list
*list2) {
125
int
r,count=0,l1=0,l2=0;
126
INT_list
newlist;
127
int
*tmplist;
128
int
found;
129
tmplist=(
int
*)malloc(
sizeof
(
int
)*(list1->
nr
+list2->
nr
));
130
while
(l1<list1->nr || l2<list2->nr) {
131
if
(l1<list1->nr) {
132
tmplist[count]=list1->
i
[l1];
133
count++;
134
l1++;
135
}
136
found=0;
137
if
(l2<list2->nr) {
138
for
(r=0;r<count;r++) {
139
if
(tmplist[r]==list2->
i
[l2]) found++;
140
}
141
if
(found<1) {
142
tmplist[count]=list2->
i
[l2]; count++;
143
}
144
l2++;
145
}
146
}
147
newlist.
i
=(
int
*)malloc(
sizeof
(
int
)*count);
148
memcpy(newlist.
i
,tmplist,count*
sizeof
(
int
));
149
newlist.
nr
=count;
150
free(tmplist);
151
return
newlist;
152
}
153
154
/*****************************************************************************/
155
156
/*****************************************************************************/
161
int
integerListInit
(
163
INTEGER_LIST
*l
164
) {
165
if
(l==NULL)
return
-1;
166
l->
nr
=l->
_allocNr
=0;
167
l->
list
=NULL;
168
return
0;
169
}
170
175
int
integerListEmpty
(
177
INTEGER_LIST
*l
178
) {
179
if
(l==NULL)
return
-1;
180
if
(l->
_allocNr
>0) free(l->
list
);
181
l->
nr
=l->
_allocNr
=0;
182
l->
list
=NULL;
183
return
0;
184
}
185
190
int
integerListAdd
(
192
INTEGER_LIST
*l,
194
int
v,
196
int
ifnew
197
) {
198
int
i;
199
if
(l==NULL)
return
(-1);
200
201
/* If only new value is to be added, then check that this is new */
202
if
(ifnew!=0) {
for
(i=0; i<l->
nr
; i++)
if
(l->
list
[i]==v)
return
(0);}
203
/* Add value to list if there is space left, and quit */
204
if
(l->
_allocNr
>l->
nr
) {l->
list
[l->
nr
++]=v;
return
(1);}
205
/* Allocate more space */
206
if
(l->
_allocNr
==0) l->
list
=(
int
*)malloc(10*
sizeof
(
int
));
207
else
l->
list
=(
int
*)realloc(l->
list
, (10+l->
_allocNr
)*
sizeof
(
int
));
208
if
(l->
list
==NULL) {l->
nr
=l->
_allocNr
=0;
return
(-2);}
209
l->
_allocNr
+=10;
210
/* Add value to list */
211
l->
list
[l->
nr
++]=v;
212
return
(1);
213
}
214
219
int
integerListSort
(
221
INTEGER_LIST
*l
222
) {
223
int
i, j, v;
224
225
if
(l==NULL)
return
-1;
226
if
(l->
nr
<2)
return
0;
227
for
(i=0; i<l->
nr
; i++)
for
(j=i+1; j<l->
nr
; j++) {
228
if
(l->
list
[i]>l->
list
[j]) {
229
v=l->
list
[i]; l->
list
[i]=l->
list
[j]; l->
list
[j]=v;
230
}
231
}
232
return
0;
233
}
234
/*****************************************************************************/
235
236
/*****************************************************************************/
242
int
integerListAddFromString
(
245
const
char
*s1,
247
const
char
*s2,
249
INTEGER_LIST
*l,
251
const
int
ifnew
252
) {
253
if
(l==NULL)
return
(-1);
254
if
(s1==NULL || s2==NULL)
return
(0);
255
256
/* Get the nr of tokens */
257
int
i, j, m, n, v;
258
n=
strTokenNr
((
char
*)s1, s2);
if
(n<1)
return
(0);
259
/* Read the values */
260
char
tmp[128];
261
for
(i=j=0; i<n; i++) {
262
if
(
strTokenNCpy
(s1, s2, 1+i, tmp, 128)<1)
return
(-2);
263
if
(
atoi_with_check
(tmp, &v))
return
(-3);
264
m=
integerListAdd
(l, v, ifnew);
if
(m<0)
return
(-4);
265
j+=m;
266
}
267
return
(j);
268
}
269
/*****************************************************************************/
270
271
/*****************************************************************************/
278
int
integerListExpandFromString
(
281
const
char
*s1,
283
const
char
*s2,
285
INTEGER_LIST
*l,
287
const
int
ifnew
288
) {
289
if
(l==NULL)
return
(-1);
290
if
(s1==NULL || s2==NULL)
return
(0);
291
292
/* Get the nr of tokens */
293
int
n=
strTokenNr
((
char
*)s1, s2);
if
(n<1)
return
(0);
294
/* Read the values */
295
char
tmp[128], tmp2[128], *t, *tail;
296
int
i, j, m, first, last, sw;
297
for
(i=j=0; i<n; i++) {
298
if
(
strTokenNCpy
(s1, s2, 1+i, tmp, 128)<1)
return
(-2);
299
t=tmp; errno=0;
300
first=strtol(t, &tail, 10);
if
(errno)
return
(-3);
301
if
(*tail) {
302
strcpy(tmp2, tail); t=tmp2;
303
if
(*t==
'-'
) t++;
304
else
if
(*t==
'.'
) {t++;
if
(*t==
'.'
) t++;
else
return
(-4);}
305
else
return
(-5);
306
if
(!*t)
return
(-6);
307
last=strtol(t, &tail, 10);
if
(errno)
return
(-7);
308
if
(*tail)
return
(-8);
309
}
else
{
310
last=first;
311
}
312
313
if
(first>last) {sw=first; first=last; last=sw;}
314
for
(
int
v=first; v<=last; v++) {
315
m=
integerListAdd
(l, v, ifnew);
if
(m<0)
return
(-10);
316
j+=m;
317
}
318
}
319
return
(j);
320
}
321
/*****************************************************************************/
322
323
/*****************************************************************************/
atoi_with_check
int atoi_with_check(const char *int_as_string, int *result_value)
Definition
decpoint.c:238
integerListEmpty
int integerListEmpty(INTEGER_LIST *l)
Definition
intex.c:175
integerListInit
int integerListInit(INTEGER_LIST *l)
Definition
intex.c:161
integerListSort
int integerListSort(INTEGER_LIST *l)
Definition
intex.c:219
integerListAddFromString
int integerListAddFromString(const char *s1, const char *s2, INTEGER_LIST *l, const int ifnew)
Definition
intex.c:242
intMerge
INT_list intMerge(INT_list *list1, INT_list *list2)
Definition
intex.c:124
intExpand
int intExpand(char *text, INT_list *list)
Definition
intex.c:43
integerListExpandFromString
int integerListExpandFromString(const char *s1, const char *s2, INTEGER_LIST *l, const int ifnew)
Definition
intex.c:278
intInit
void intInit(INT_list *l)
Definition
intex.c:12
intEmpty
void intEmpty(INT_list *l)
Definition
intex.c:23
_intexadd
int _intexadd(INT_list *list, int a)
Definition
intex.c:92
integerListAdd
int integerListAdd(INTEGER_LIST *l, int v, int ifnew)
Definition
intex.c:190
libtpcmisc.h
Header file for libtpcmisc.
strTokenNCpy
int strTokenNCpy(const char *str1, const char *str2, int i, char *str3, int count)
Definition
strext.c:45
strTokenNr
int strTokenNr(const char *str1, const char *str2)
Definition
strext.c:17
swap
void swap(void *orig, void *new, int size)
Definition
swap.c:31
INT_list
Definition
libtpcmisc.h:318
INT_list::nr
int nr
Definition
libtpcmisc.h:320
INT_list::i
int * i
Definition
libtpcmisc.h:322
INTEGER_LIST
Definition
libtpcmisc.h:327
INTEGER_LIST::nr
int nr
Definition
libtpcmisc.h:329
INTEGER_LIST::list
int * list
Definition
libtpcmisc.h:333
INTEGER_LIST::_allocNr
int _allocNr
Definition
libtpcmisc.h:331
Generated on
for TPCCLIB by
1.17.0