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

Working with integer values. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int atoiCheck (const char *s, int *v)
 
unsigned int factorial (unsigned int n)
 
unsigned long long int lfactorial (unsigned long long int n)
 
void intlistInit (INTLIST *l)
 
void intlistFree (INTLIST *l)
 
int intlistAdd (INTLIST *l, const int v, const int ifnew)
 
void intlistSort (INTLIST *l)
 
int intlistAddFromString (const char *s1, const char *s2, INTLIST *l, const int ifnew)
 
int intlistExpandFromString (const char *s1, const char *s2, INTLIST *l, const int ifnew)
 

Detailed Description

Working with integer values.

Author
Vesa Oikonen

Definition in file intutil.c.

Function Documentation

◆ atoiCheck()

int atoiCheck ( const char * s,
int * v )

Verifies that argument string at least seems like a valid integer. '\0' must follow the integer part in the string. Optional result integer value is set to 0 if string was not valid value.

See also
atofCheck, intlistAddFromString, strIsValidNumber
Returns
0 if successful, and 1 in case of an error.
Parameters
sString which is converted to an integer; string must not contain any space characters.
vPointer to the integer; enter NULL, if not needed.

Definition at line 25 of file intutil.c.

30 {
31 if(v!=NULL) *v=0;
32 if(s==NULL || strnlen(s, 2)<1) return(1);
33 errno=0; char *tail;
34 *v=strtol(s, &tail, 10);
35 if(errno) {*v=0; return(2);}
36 if(*tail) {*v=0; return(3);}
37 return 0;
38}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by intlistAddFromString(), and tacReadQView().

◆ factorial()

unsigned int factorial ( unsigned int n)

Calculate factorial of given number.

See also
lfactorial, igam, igamc
Returns
Returns factorial n!, or zero if factorial would cause wrap-around.
Parameters
nInteger n, from which the factorial is calculated.
Note
Wrap-around will occur if n>12.

Definition at line 46 of file intutil.c.

51 {
52 if(n<1) return(1);
53 if(n>12) return(0);
54 return(n*factorial(n-1));
55}
unsigned int factorial(unsigned int n)
Definition intutil.c:46

Referenced by factorial().

◆ intlistAdd()

int intlistAdd ( INTLIST * l,
const int v,
const int ifnew )

Add one integer to the integer array in INTLIST structure.

See also
intlistInit, intlistFree, intlistSort, intlistAddFromString, strncatInt
Author
Vesa Oikonen
Returns
Returns the nr of added integers (either 0 or 1), or <0 in case of an error.
Parameters
lPointer to initiated or filled INTLIST structure.
vInteger value to add.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 108 of file intutil.c.

115 {
116 if(l==NULL) return(-1);
117 /* If only new value is to be added, then check that this is new */
118 if(ifnew!=0) {for(int i=0; i<l->nr; i++) if(l->i[i]==v) return(0);}
119 /* Add value to list if there is space left, and quit */
120 if(l->_nr>l->nr) {l->i[l->nr++]=v; return(1);}
121 /* Allocate more space */
122 if(l->_nr==0) l->i=(int*)malloc(10*sizeof(int));
123 else l->i=(int*)realloc(l->i, (10+l->_nr)*sizeof(int));
124 if(l->i==NULL) {l->nr=l->_nr=0; return(-2);}
125 l->_nr+=10;
126 /* Add value to list */
127 l->i[l->nr++]=v;
128 return(1);
129}

Referenced by intlistAddFromString(), and intlistExpandFromString().

◆ intlistAddFromString()

int intlistAddFromString ( const char * s1,
const char * s2,
INTLIST * l,
const int ifnew )

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

See also
strTokenNr, strTokenNCpy, intlistAdd, intlistInit, intlistFree, intlistSort, intlistExpandFromString.
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 INTLIST structure; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 156 of file intutil.c.

165 {
166 if(l==NULL) return(-1);
167 if(s1==NULL || s2==NULL) return(0);
168
169 /* Get the nr of tokens */
170 int i, j, m, n, v;
171 n=strTokenNr((char*)s1, s2); if(n<1) return(0);
172 /* Read the values */
173 char tmp[128];
174 for(i=j=0; i<n; i++) {
175 if(strTokenNCpy(s1, s2, 1+i, tmp, 128)<1) return(-2);
176 if(atoiCheck(tmp, &v)) return(-3);
177 m=intlistAdd(l, v, ifnew); if(m<0) return(-4);
178 j+=m;
179 }
180 return(j);
181}
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int intlistAdd(INTLIST *l, const int v, const int ifnew)
Definition intutil.c:108
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53

◆ intlistExpandFromString()

int intlistExpandFromString ( const char * s1,
const char * s2,
INTLIST * l,
const int ifnew )

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

See also
strTokenNCpy, intlistAddFromString, intlistInit, intlistFree, intlistSort, strIsValidNumber
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 INTLIST struct; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 190 of file intutil.c.

200 {
201 if(l==NULL) return(-1);
202 if(s1==NULL || s2==NULL) return(0);
203
204 /* Get the nr of tokens */
205 int n=strTokenNr((char*)s1, s2); if(n<1) return(0);
206 /* Read the values */
207 char tmp[128], tmp2[128], *t, *tail;
208 int i, j, m, first, last, sw;
209 for(i=j=0; i<n; i++) {
210 if(strTokenNCpy(s1, s2, 1+i, tmp, 128)<1) return(-2);
211 t=tmp; errno=0;
212 first=strtol(t, &tail, 10); if(errno) return(-3);
213 if(*tail) {
214 strcpy(tmp2, tail); t=tmp2;
215 if(*t=='-') t++;
216 else if(*t=='.') {t++; if(*t=='.') t++; else return(-4);}
217 else return(-5);
218 if(!*t) return(-6);
219 last=strtol(t, &tail, 10); if(errno) return(-7);
220 if(*tail) return(-8);
221 } else {
222 last=first;
223 }
224
225 if(first>last) {sw=first; first=last; last=sw;}
226 for(int v=first; v<=last; v++) {
227 m=intlistAdd(l, v, ifnew); if(m<0) return(-10);
228 j+=m;
229 }
230 }
231 return(j);
232}

Referenced by parSelectParameters(), and parSelectTACs().

◆ intlistFree()

void intlistFree ( INTLIST * l)

Free memory allocated for INTLIST. All data is cleared.

See also
intlistInit
Author
Vesa Oikonen
Parameters
lPointer to INTLIST structure

Definition at line 92 of file intutil.c.

95 {
96 if(l==NULL) return;
97 free(l->i);
98 intlistInit(l);
99}
void intlistInit(INTLIST *l)
Definition intutil.c:80

Referenced by parSelectParameters(), and parSelectTACs().

◆ intlistInit()

void intlistInit ( INTLIST * l)

Initiate the INTLIST structure before any use.

See also
intlistFree, intlistAdd, intlistSort
Author
Vesa Oikonen
Parameters
lPointer to INTLIST.

Definition at line 80 of file intutil.c.

83 {
84 if(l==NULL) return;
85 l->nr=0; l->_nr=0;
86 l->i=NULL;
87}

Referenced by intlistFree(), parSelectParameters(), and parSelectTACs().

◆ intlistSort()

void intlistSort ( INTLIST * l)

Sort the integer array in INTLIST structure.

See also
intlistInit, intlistFree, intlistAdd
Author
Vesa Oikonen
Parameters
lPointer to INTLIST structure.

Definition at line 137 of file intutil.c.

140 {
141 if(l==NULL || l->i==NULL || l->nr<2) return;
142 int i, j, v;
143 for(i=0; i<l->nr; i++) for(j=i+1; j<l->nr; j++) {
144 if(l->i[i]>l->i[j]) {v=l->i[i]; l->i[i]=l->i[j]; l->i[j]=v;}
145 }
146}

Referenced by parSelectParameters(), and parSelectTACs().

◆ lfactorial()

unsigned long long int lfactorial ( unsigned long long int n)

Calculate factorial of given number.

See also
factorial, igam, igamc
Returns
Returns factorial n!, or zero if factorial would cause wrap-around.
Parameters
nInteger n, from which the factorial is calculated.
Note
Wrap-around will occur if n>20.

Definition at line 63 of file intutil.c.

68 {
69 if(n<1) return(1);
70 if(n>20) return(0);
71 return(n*lfactorial(n-1));
72}
unsigned long long int lfactorial(unsigned long long int n)
Definition intutil.c:63

Referenced by lfactorial(), mfEvalInt(), and mfEvalY().