TPCCLIB
Loading...
Searching...
No Matches
intex.c File Reference

Expansion of positive integers specified in a string. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

void intInit (INT_list *l)
 
void intEmpty (INT_list *l)
 
int intExpand (char *text, INT_list *list)
 
int _intexadd (INT_list *list, int a)
 
INT_list intMerge (INT_list *list1, INT_list *list2)
 
int integerListInit (INTEGER_LIST *l)
 
int integerListEmpty (INTEGER_LIST *l)
 
int integerListAdd (INTEGER_LIST *l, int v, int ifnew)
 
int integerListSort (INTEGER_LIST *l)
 
int integerListAddFromString (const char *s1, const char *s2, INTEGER_LIST *l, const int ifnew)
 
int integerListExpandFromString (const char *s1, const char *s2, INTEGER_LIST *l, const int ifnew)
 

Detailed Description

Expansion of positive integers specified in a string.

Author
Vesa Oikonen, Calle Laakkonen

Definition in file intex.c.

Function Documentation

◆ _intexadd()

int _intexadd ( INT_list * list,
int a )

int _intexadd(int *list, int a) ; local function. Deprecated.

Parameters
list
a

Definition at line 92 of file intex.c.

92 {
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}
int * i
Definition libtpcmisc.h:322

Referenced by intExpand().

◆ integerListAdd()

int integerListAdd ( INTEGER_LIST * l,
int v,
int ifnew )

Add one integer to INTEGER_LIST.

See also
integerListInit, integerListSort, integerListAddFromString, integerListExpandFromString.
Returns
Returns the number of added integers (0 or 1), or <0 in case of an error.
Parameters
lPointer to initiated list
vInteger value to add
ifnewAdd integer to the list only if it is new (0=no, 1=yes)

Definition at line 190 of file intex.c.

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}

Referenced by imgMaskRoiNr(), integerListAddFromString(), and integerListExpandFromString().

◆ integerListAddFromString()

int integerListAddFromString ( const char * s1,
const char * s2,
INTEGER_LIST * l,
const int ifnew )

Read a list of integer values from given string with given delimiters.

See also
integerListInit, integerListSort, integerListAdd, integerListExpandFromString.
Returns
The number of added integer values, or <0 in case of an error.
Author
Vesa Oikonen
Parameters
s1Pointer to string from which the integers are read, for example "2,3,6,8".
s2String containing character delimiters, for example ", ".
lPointer to INTEGER_LIST struct; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes)

Definition at line 242 of file intex.c.

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}
int atoi_with_check(const char *int_as_string, int *result_value)
Definition decpoint.c:238
int integerListAdd(INTEGER_LIST *l, int v, int ifnew)
Definition intex.c:190
int strTokenNCpy(const char *str1, const char *str2, int i, char *str3, int count)
Definition strext.c:45
int strTokenNr(const char *str1, const char *str2)
Definition strext.c:17

◆ integerListEmpty()

int integerListEmpty ( INTEGER_LIST * l)

Free the memory allocated in the INTEGER_LIST struct.

See also
integerListInit, integerListSort, integerListAdd.
Returns
Returns <>0 in case of an error.
Parameters
lPointer to INTEGER_LIST struct

Definition at line 175 of file intex.c.

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}

Referenced by imgMaskRoiNr().

◆ integerListExpandFromString()

int integerListExpandFromString ( const char * s1,
const char * s2,
INTEGER_LIST * l,
const int ifnew )

Read ranges and individual integer values from given string with given delimiters.

See also
integerListInit, integerListSort, integerListAdd, integerListAddFromString.
Returns
The number of added integer values, or <0 in case of an error.
Author
Vesa Oikonen
Parameters
s1Pointer to string from which the integers are read, for example "0-8,12,32-28" or "0..8, 12, 28..34".
s2String containing character delimiters, for example ", ".
lPointer to INTEGER_LIST struct; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes)

Definition at line 278 of file intex.c.

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}

◆ integerListInit()

int integerListInit ( INTEGER_LIST * l)

Call this (once) before using INTEGER_LIST struct for the first time.

See also
integerListEmpty, integerListSort, integerListAddFromString, integerListExpandFromString.
Returns
Returns <>0 in case of an error.
Parameters
lPointer to INTEGER_LIST struct

Definition at line 161 of file intex.c.

164 {
165 if(l==NULL) return -1;
166 l->nr=l->_allocNr=0;
167 l->list=NULL;
168 return 0;
169}

◆ integerListSort()

int integerListSort ( INTEGER_LIST * l)

Sort INTEGER_LIST

See also
integerListInit, integerListAdd, integerListAddFromString, integerListExpandFromString.
Returns
Returns <>0 in case of an error.
Parameters
lPointer to INTEGER_LIST struct

Definition at line 219 of file intex.c.

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}

Referenced by imgMaskRoiNr().

◆ intEmpty()

void intEmpty ( INT_list * l)

Free the memory allocated in the INT_LIST struct.

Parameters
lPointer to INT_LIST struct

Definition at line 23 of file intex.c.

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}

Referenced by intExpand().

◆ intExpand()

int intExpand ( char * text,
INT_list * list )

Existing list is freed and all data is cleared. Deprecated. Expanded integers are listed in list.i[] in increasing order.

Parameters
textInteger expressions to be expanded, e.g. 0-8,12,34-28
listPointer for int list data
Returns
0 if ok and at least one integer is listed.
See also
integerListAddFromString, integerListExpandFromString.

Definition at line 43 of file intex.c.

43 {
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}
void intEmpty(INT_list *l)
Definition intex.c:23
int _intexadd(INT_list *list, int a)
Definition intex.c:92
void swap(void *orig, void *new, int size)
Definition swap.c:31

◆ intInit()

void intInit ( INT_list * l)

Call this (once) before using INT_LIST struct for the first time.

Parameters
lPointer to INT_LIST struct

Definition at line 12 of file intex.c.

15 {
16 if(l==NULL) return;
17 l->nr=0;
18 l->i=NULL;
19 return;
20}

◆ intMerge()

INT_list intMerge ( INT_list * list1,
INT_list * list2 )

Merges two lists and returns the result. (the originals are not touched) Duplicate entries are removed. Deprecated.

Parameters
list1The first list
list2The second list
Returns
pointer to the new combined list.

Definition at line 124 of file intex.c.

124 {
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}