TPCCLIB
Loading...
Searching...
No Matches
intutil.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <ctype.h>
14#include <math.h>
15#include "tpcextensions.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
27 const char *s,
29 int *v
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}
39/*****************************************************************************/
40
41/*****************************************************************************/
46unsigned int factorial(
50 unsigned int n
51) {
52 if(n<1) return(1);
53 if(n>12) return(0);
54 return(n*factorial(n-1));
55}
56/*****************************************************************************/
57
58/*****************************************************************************/
63unsigned long long int lfactorial(
67 unsigned long long int n
68) {
69 if(n<1) return(1);
70 if(n>20) return(0);
71 return(n*lfactorial(n-1));
72}
73/*****************************************************************************/
74
75/*****************************************************************************/
82 INTLIST *l
83) {
84 if(l==NULL) return;
85 l->nr=0; l->_nr=0;
86 l->i=NULL;
87}
94 INTLIST *l
95) {
96 if(l==NULL) return;
97 free(l->i);
98 intlistInit(l);
99}
100/*****************************************************************************/
101
102/*****************************************************************************/
110 INTLIST *l,
112 const int v,
114 const int ifnew
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}
130/*****************************************************************************/
131
132/*****************************************************************************/
139 INTLIST *l
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}
147/*****************************************************************************/
148
149/*****************************************************************************/
158 const char *s1,
160 const char *s2,
162 INTLIST *l,
164 const int ifnew
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}
182/*****************************************************************************/
183
184/*****************************************************************************/
193 const char *s1,
195 const char *s2,
197 INTLIST *l,
199 const int ifnew
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}
233/*****************************************************************************/
234
235/*****************************************************************************/
void intlistInit(INTLIST *l)
Definition intutil.c:80
int intlistExpandFromString(const char *s1, const char *s2, INTLIST *l, const int ifnew)
Definition intutil.c:190
unsigned long long int lfactorial(unsigned long long int n)
Definition intutil.c:63
void intlistFree(INTLIST *l)
Definition intutil.c:92
void intlistSort(INTLIST *l)
Definition intutil.c:137
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int intlistAddFromString(const char *s1, const char *s2, INTLIST *l, const int ifnew)
Definition intutil.c:156
int intlistAdd(INTLIST *l, const int v, const int ifnew)
Definition intutil.c:108
unsigned int factorial(unsigned int n)
Definition intutil.c:46
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
Header file for library libtpcextensions.