1 | /*
|
---|
2 | ++
|
---|
3 | Module Strutil (C)
|
---|
4 | Lib LibsUtil
|
---|
5 | include strutil.h
|
---|
6 |
|
---|
7 | Groupe de fonction de gestion et manipulation
|
---|
8 | des chaines de caracteres.
|
---|
9 | --
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*
|
---|
13 | ++
|
---|
14 | char* itoc(int in, char* s, int nDigits)
|
---|
15 | conversion entier-chaine de caracteres
|
---|
16 | --
|
---|
17 | */
|
---|
18 | /*
|
---|
19 | ++
|
---|
20 | ctoi(const char* cst, int *ipt)
|
---|
21 | convertit une chaine de caractere en entier
|
---|
22 | --
|
---|
23 | */
|
---|
24 | /*
|
---|
25 | ++
|
---|
26 | ctol(const char* cst, long int *lpt)
|
---|
27 | convertit une chaine de caractere en entier long
|
---|
28 | --
|
---|
29 | */
|
---|
30 | /*
|
---|
31 | ++
|
---|
32 | ctof(const char* cst, double *fpt)
|
---|
33 | convertit une chaine de caractere en flottant
|
---|
34 | --
|
---|
35 | */
|
---|
36 | /*
|
---|
37 | ++
|
---|
38 | posc(const char* cst, char sec)
|
---|
39 | trouve la position d'un caractere dans une chaine
|
---|
40 | (premier charactere rencontre)
|
---|
41 | --
|
---|
42 | */
|
---|
43 | /*
|
---|
44 | ++
|
---|
45 | poslc(const char* cst, char sec)
|
---|
46 | trouve la position d'un caractere dans une chaine
|
---|
47 | (dernier charactere rencontre)
|
---|
48 | --
|
---|
49 | */
|
---|
50 | /*
|
---|
51 | ++
|
---|
52 | strip(char cst[], char opt, char bc)
|
---|
53 | permet d'enlever les blancs au debut ou a la fin d'une
|
---|
54 | chaine de caractere
|
---|
55 | --
|
---|
56 | */
|
---|
57 | /*
|
---|
58 | ++
|
---|
59 | padstr(spo,lpa,bc)
|
---|
60 | Cette fonction rajoute des blancs a la fin de la chaine `spo'
|
---|
61 | pour amener la longueur de la chaine a `lpa'. Les caracteres
|
---|
62 | rajoutes a la fin sont `bc'. La fonction retourne le nombre de
|
---|
63 | blancs (caracteres bc) rajoutes a la fin.
|
---|
64 | --
|
---|
65 | */
|
---|
66 | /*
|
---|
67 | ++
|
---|
68 | tomaj(char *cbuff)
|
---|
69 | Changement en majuscule d'une chaine de caracteres
|
---|
70 | --
|
---|
71 | */
|
---|
72 | /*
|
---|
73 | ++
|
---|
74 | tomin(char *cbuff)
|
---|
75 | Changement en minuscule d'une chaine de caracteres
|
---|
76 | --
|
---|
77 | */
|
---|
78 | /*
|
---|
79 | ++
|
---|
80 | efface(char *cbuff,int lbuff,char c)
|
---|
81 | Remplace un caractere avec des blancs
|
---|
82 | --
|
---|
83 | */
|
---|
84 | /*
|
---|
85 | ++
|
---|
86 | csh_parse(const char* x, const char* yp)
|
---|
87 | Compare la chaine de caracteres `x' avec la chaine `yp'
|
---|
88 | comprenant des regles de substitution type c-shell
|
---|
89 | (asterisque et point d'interrogation).
|
---|
90 | --
|
---|
91 | */
|
---|
92 | /*
|
---|
93 | DecArgList()
|
---|
94 | Decoder une liste du type a,b,c-d,e,f-g,h...
|
---|
95 | */
|
---|
96 |
|
---|
97 |
|
---|
98 | #include "strutil.h"
|
---|
99 |
|
---|
100 | /* Fonction itoc() */
|
---|
101 | char* itoc(int in, char* s, int nDigits)
|
---|
102 | {
|
---|
103 | int i,j,n;
|
---|
104 | n=abs(in);
|
---|
105 | for(i=0;i<nDigits;i++)
|
---|
106 | s[i]=' ';
|
---|
107 | s[nDigits-1]='\0';
|
---|
108 | j=nDigits-2;
|
---|
109 | for (i=j; i>=0; i--)
|
---|
110 | {
|
---|
111 | int x = n % 10;
|
---|
112 | n /= 10;
|
---|
113 | s[i] = '0' + x;
|
---|
114 | if(n==0) break;
|
---|
115 | }
|
---|
116 | if(in < 0 )
|
---|
117 | {
|
---|
118 | s[i-1]='-';
|
---|
119 | }
|
---|
120 | return (s);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* Fonction ctoi() */
|
---|
124 | int ctoi(const char* cst, int *ipt)
|
---|
125 | {
|
---|
126 | int i,j,k,l,ls,isgn;
|
---|
127 | ls = strlen(cst)-1;
|
---|
128 | if (ls < 0) return (-1);
|
---|
129 | j = -1;
|
---|
130 | isgn = l = 1;
|
---|
131 | k = 0;
|
---|
132 | if(cst[0] == '-')
|
---|
133 | {
|
---|
134 | j = 0;
|
---|
135 | isgn = -1;
|
---|
136 | }
|
---|
137 | if(cst[0] == '+') j = 0;
|
---|
138 | for(i=ls;i>j;i--)
|
---|
139 | {
|
---|
140 | if(isdigit(cst[i]) == 0) return (-1);
|
---|
141 | k += (cst[i] - '0')*l;
|
---|
142 | l *= 10;
|
---|
143 | }
|
---|
144 | *ipt = k*isgn;
|
---|
145 | return (1);
|
---|
146 | }
|
---|
147 | /* */
|
---|
148 | /* Fonction ctol() */
|
---|
149 | int ctol(const char* cst, long int *lpt)
|
---|
150 | {
|
---|
151 | int i,j,ls,isgn;
|
---|
152 | long k,l;
|
---|
153 | ls = strlen(cst)-1;
|
---|
154 | if (ls < 0) return (-1);
|
---|
155 | j = -1;
|
---|
156 | isgn = l = 1;
|
---|
157 | k = 0;
|
---|
158 | if(cst[0] == '-')
|
---|
159 | {
|
---|
160 | j = 0;
|
---|
161 | isgn = -1;
|
---|
162 | }
|
---|
163 | if(cst[0] == '+') j = 0;
|
---|
164 | for(i=ls;i>j;i--)
|
---|
165 | {
|
---|
166 | if(isdigit(cst[i]) == 0) return (-1);
|
---|
167 | k += (cst[i] - '0')*l;
|
---|
168 | l *= 10;
|
---|
169 | }
|
---|
170 | *lpt = k*isgn;
|
---|
171 | return (1);
|
---|
172 | }
|
---|
173 | /* Fonction ctof() */
|
---|
174 | int ctof(const char* cst, double *fpt)
|
---|
175 | {
|
---|
176 | int i,jb,pv,ls,isgn;
|
---|
177 | double v1,v2;
|
---|
178 | ls = strlen(cst);
|
---|
179 | if (ls < 0) return (-1);
|
---|
180 | jb = -1;
|
---|
181 | isgn = 1;
|
---|
182 | v1 = 0.;
|
---|
183 | v2 = 1.;
|
---|
184 | if (cst[0] == '-')
|
---|
185 | {
|
---|
186 | jb = 0;
|
---|
187 | isgn = -1;
|
---|
188 | }
|
---|
189 | if (cst[0] == '+') jb = 0;
|
---|
190 | if ((pv=posc(cst,'.')) != -1)
|
---|
191 | {
|
---|
192 | v2 = 0.1;
|
---|
193 | for(i=pv+1;i<ls;i++)
|
---|
194 | {
|
---|
195 | if(isdigit(cst[i]) == 0) return (-1);
|
---|
196 | v1 += (cst[i]-'0')*v2;
|
---|
197 | v2 /= 10.;
|
---|
198 | }
|
---|
199 | ls = pv;
|
---|
200 | v2 = 1.;
|
---|
201 | }
|
---|
202 | for(i=ls-1;i>jb;i--)
|
---|
203 | {
|
---|
204 | if(isdigit(cst[i]) == 0) return (-1);
|
---|
205 | v1 += (cst[i]-'0')*v2;
|
---|
206 | v2 *= 10.;
|
---|
207 | }
|
---|
208 | *fpt = (isgn == 1)? v1 : -v1;
|
---|
209 | return (1);
|
---|
210 | }
|
---|
211 | /* Fonction posc() */
|
---|
212 | int posc(const char* cst, char sec)
|
---|
213 | {
|
---|
214 | int i,ls;
|
---|
215 | ls = strlen(cst);
|
---|
216 | for(i=0;i<ls;i++)
|
---|
217 | {
|
---|
218 | if (cst[i] == sec) return(i);
|
---|
219 | }
|
---|
220 | return (-1);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /* Fonction poslc() */
|
---|
224 | int poslc(const char* cst, char sec)
|
---|
225 | {
|
---|
226 | int i,ls;
|
---|
227 | ls = strlen(cst);
|
---|
228 | for(i=ls-1;i>=0;i--)
|
---|
229 | {
|
---|
230 | if (cst[i] == sec) return(i);
|
---|
231 | }
|
---|
232 | return (-1);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* Fonction strip() */
|
---|
236 | int strip(char cst[], char opt, char bc)
|
---|
237 | {
|
---|
238 | int i,j,ls;
|
---|
239 | ls = strlen(cst);
|
---|
240 | if (opt == 'L' || opt == 'B')
|
---|
241 | {
|
---|
242 | i = 0;
|
---|
243 | while(cst[i] == bc) i++;
|
---|
244 | if (i != 0)
|
---|
245 | {
|
---|
246 | for(j=i;j<ls;j++) cst[j-i] = cst[j];
|
---|
247 | ls -= i;
|
---|
248 | cst[ls] = '\0';
|
---|
249 | }
|
---|
250 | }
|
---|
251 | if ((opt == 'T' || opt == 'B') && ls > 0)
|
---|
252 | {
|
---|
253 | i = ls-1;
|
---|
254 | while(cst[i] == bc) i--;
|
---|
255 | cst[i+1] = '\0';
|
---|
256 | }
|
---|
257 | return (0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* Fonction padstr(spo,lpa,bc) */
|
---|
261 | /* Cette fonction rajoute des blancs a la fin de la chaine spo */
|
---|
262 | /* pour amener la longueur de la chaine a lpa. les caracteres */
|
---|
263 | /* rajoutes a la fin sont bc. La fonction retourne le nombre de */
|
---|
264 | /* blancs (caracteres bc) rajoutes a la fin. */
|
---|
265 |
|
---|
266 | int padstr(char spo[], char lpa, int bc)
|
---|
267 | {
|
---|
268 | int li,i;
|
---|
269 | li = strlen(spo);
|
---|
270 | for(i=li;i<lpa;i++) spo[i] = bc;
|
---|
271 | spo[lpa] = '\0';
|
---|
272 | return(lpa-li);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Eric L. 11-03-94 */
|
---|
276 | /* quelques fonctions utiles */
|
---|
277 |
|
---|
278 | void tomaj(char *cbuff)
|
---|
279 | {
|
---|
280 | register int i,l;
|
---|
281 |
|
---|
282 | l=strlen(cbuff);
|
---|
283 | for(i=0; i< l;i++)
|
---|
284 | cbuff[i]=(char) toupper( (int) cbuff[i]);
|
---|
285 | }
|
---|
286 |
|
---|
287 | void tomin(char *cbuff)
|
---|
288 | {
|
---|
289 | register int i,l;
|
---|
290 |
|
---|
291 | l=strlen(cbuff);
|
---|
292 | for(i=0; i< l;i++)
|
---|
293 | cbuff[i]=(char) tolower( (int) cbuff[i]);
|
---|
294 | }
|
---|
295 |
|
---|
296 | void efface(char *cbuff,int lbuff,char c)
|
---|
297 | {
|
---|
298 | register int i;
|
---|
299 |
|
---|
300 | for(i=0; i< lbuff;i++)
|
---|
301 | if(cbuff[i] == c) cbuff[i] = ' ';
|
---|
302 | }
|
---|
303 |
|
---|
304 | char* rep_char(const char *cbuff,char cin,char crep)
|
---|
305 | {
|
---|
306 | register int i,l;
|
---|
307 | char* buff;
|
---|
308 | buff=(char*) cbuff;
|
---|
309 | l=strlen(buff);
|
---|
310 | for(i=0; i< l;i++)
|
---|
311 | if(buff[i] == cin) buff[i] = crep;
|
---|
312 | return(buff);
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Eric L. 23-03-96 */
|
---|
316 |
|
---|
317 | int csh_parse(const char* x, const char* yp)
|
---|
318 | {
|
---|
319 | int i,lenx,lenyp;
|
---|
320 | int ix,iyp;
|
---|
321 | int faux;
|
---|
322 | char cx, cyp;
|
---|
323 |
|
---|
324 | lenx=strlen(x);
|
---|
325 | lenyp=strlen(yp);
|
---|
326 | if(!lenx || !lenyp) return(0);
|
---|
327 | faux=0;
|
---|
328 | ix=0;iyp=0;
|
---|
329 | while(ix < lenx && iyp < lenyp)
|
---|
330 | {
|
---|
331 | cx=x[ix];
|
---|
332 | cyp=yp[iyp];
|
---|
333 | if(cx == cyp)
|
---|
334 | {
|
---|
335 | ix++;
|
---|
336 | iyp++;
|
---|
337 | }
|
---|
338 | else
|
---|
339 | switch(cyp)
|
---|
340 | {
|
---|
341 | case '?':
|
---|
342 | ix++;
|
---|
343 | iyp++;
|
---|
344 | break;
|
---|
345 | case '*':
|
---|
346 | while((cyp=='*' || cyp=='?') && (iyp < lenyp))
|
---|
347 | {
|
---|
348 | iyp++;
|
---|
349 | if(iyp < lenyp) cyp=yp[iyp];
|
---|
350 | }
|
---|
351 | if(iyp < lenyp)
|
---|
352 | {
|
---|
353 | faux=1;
|
---|
354 | for(i = ix; i < lenx ; i++)
|
---|
355 | {
|
---|
356 | cx=x[i];
|
---|
357 | if(cx == cyp)
|
---|
358 | {
|
---|
359 | faux=0;
|
---|
360 | break;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | ix=i+1;
|
---|
364 | if(ix < lenx) cx=x[ix];
|
---|
365 | iyp++;
|
---|
366 | if(iyp < lenyp) cyp=yp[iyp];
|
---|
367 | }
|
---|
368 | break;
|
---|
369 | default:
|
---|
370 | faux=1;
|
---|
371 | }
|
---|
372 | if(faux) return(0);
|
---|
373 | }
|
---|
374 |
|
---|
375 | for( i= iyp; i < lenyp ; i++)
|
---|
376 | {
|
---|
377 | cyp=yp[i];
|
---|
378 | if( cyp != '*' && cyp != '?') return(0);
|
---|
379 | }
|
---|
380 |
|
---|
381 | if(cyp == '*') return(1);
|
---|
382 | if((lenx-ix) == 1 && ( cyp == '?' || cx==cyp)) return(1);
|
---|
383 | for( i= ix; i < lenx ; i++)
|
---|
384 | {
|
---|
385 | cx=x[i];
|
---|
386 | if( cx != cyp) return(0);
|
---|
387 | }
|
---|
388 | return(1);
|
---|
389 | }
|
---|
390 |
|
---|
391 | /*-------------------------------------------------------------------*/
|
---|
392 |
|
---|
393 | /*
|
---|
394 | ++
|
---|
395 | int DecArgList(char *listin, int *Vec, int nVec)
|
---|
396 | Pour decoder une liste de numeros a,b,c-d,e,f-g,h...
|
---|
397 | avec a,b... entiers
|
---|
398 | | Vec est rempli jusqu'a concurrence de nVec elts.
|
---|
399 | | Si Vec==NULL ou nVec<=0 le tableau n'est pas rempli
|
---|
400 | | et seul le nombre d'elements est compte
|
---|
401 | | Return: nombre d'elements rempli
|
---|
402 | --
|
---|
403 | */
|
---|
404 | /* (cmv 7/8/97) */
|
---|
405 | int DecArgList(char *listin, int *Vec, int nVec)
|
---|
406 | {
|
---|
407 | int i,k, num, min, max;
|
---|
408 | char *list = NULL, *listfin, *s;
|
---|
409 |
|
---|
410 | if(strlen(listin) <= 0) return(0);
|
---|
411 | list = (char *) malloc(strlen(listin)+1);
|
---|
412 | if(list==NULL) return(0);
|
---|
413 | strcpy(list,listin); strip(list,'B',' ');
|
---|
414 | if( (k=strlen(list)) <= 0 ) {free(list); return(0);}
|
---|
415 | for(i=0; i<k; i++) if(list[i] == ',') list[i] = ' ';
|
---|
416 | listfin = list + k; *listfin = '\0';
|
---|
417 |
|
---|
418 | s = list; num = 0; k = 0;
|
---|
419 | while(s < listfin) {
|
---|
420 | if ( (k=posc(s,' ')) < 0) k = strlen(s);
|
---|
421 | s[k] = '\0'; strip(s,'B',' ');
|
---|
422 | sscanf(s,"%d-%d",&min,&max);
|
---|
423 | max = min - 1;
|
---|
424 | sscanf(s,"%d-%d",&min,&max);
|
---|
425 | if(max<min) max = min;
|
---|
426 | for(i=min; i<=max; i++) {
|
---|
427 | if( Vec!=NULL && nVec>0 ) {
|
---|
428 | if( num >= nVec )
|
---|
429 | {printf("DecArgList_Erreur: pas assez de place dans le vecteur (%d)\n"
|
---|
430 | ,nVec); free(list); return(num);}
|
---|
431 | Vec[num] = i;
|
---|
432 | }
|
---|
433 | num++;
|
---|
434 | }
|
---|
435 | s = s+k+1;
|
---|
436 | }
|
---|
437 |
|
---|
438 | free(list);
|
---|
439 | return(num);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*-------------------------------------------------------------------*/
|
---|
443 |
|
---|
444 | /*
|
---|
445 | ++
|
---|
446 | char *ExtName(char *cout,char *cin,char t)
|
---|
447 | Pour separer les composantes d'un nom de fichier de type unix.
|
---|
448 | | cout = chaine en sortie
|
---|
449 | | cin = chaine en entree
|
---|
450 | | t = type de racine a extraire
|
---|
451 | | Return = *cout
|
---|
452 | | - Remarque: pas de verification de longeur de la chaine cout!
|
---|
453 | | - Exemple: /home/toto.cc /home/ /home/.cc .cc toto.cc
|
---|
454 | | t='d' : /home /home /home "" ""
|
---|
455 | | t='n' : toto.cc "" .cc .cc toto.cc
|
---|
456 | | t='r' : toto "" "" "" toto
|
---|
457 | | t='t' : cc "" cc cc cc
|
---|
458 | --
|
---|
459 | */
|
---|
460 | /* (cmv 24/4/98) */
|
---|
461 | char *ExtName(char *cout,char *cin,char t)
|
---|
462 | {
|
---|
463 | int is=-1,ip=-1,i,j;
|
---|
464 | int len = strlen(cin);
|
---|
465 | /* cas ou le nom se termine par un / qui n'est pas au debut*/
|
---|
466 | if(len>1) if(cin[len-1]=='/') len--;
|
---|
467 |
|
---|
468 | for(i=0;i<len;i++) {
|
---|
469 | if(cin[i]=='/') is=i; /* position du dernier / */
|
---|
470 | if(cin[i]=='.' && ip<0) ip=i; /* position du 1er point */
|
---|
471 | cout[i]=cin[i];
|
---|
472 | }
|
---|
473 | cout[len]='\0';
|
---|
474 |
|
---|
475 | /* mauvais nom de fichier */
|
---|
476 | if(is>=0&&ip>=0&&ip<=is) {cout[0]='\0'; return cout;}
|
---|
477 |
|
---|
478 | /* traitement des differents cas */
|
---|
479 | switch(t) {
|
---|
480 | case 'd':
|
---|
481 | if(is==0) { /* cas /... */
|
---|
482 | if(ip<0) ip=len-1; /* cas /home */
|
---|
483 | else ip=0; /* cas /toto.c */
|
---|
484 | } else if(is<0) { /* cas toto.c */
|
---|
485 | is=ip=-1;
|
---|
486 | } else { /* cas /home/.../... */
|
---|
487 | ip=is-1; is=0;
|
---|
488 | }
|
---|
489 | break;
|
---|
490 | case 'n':
|
---|
491 | if(is==0) { /* cas /... */
|
---|
492 | if(ip<0) ip=is=-1; /* cas /home */
|
---|
493 | else {is=1; ip=len-1;} /* cas /toto.c */
|
---|
494 | } else if(is>0) { /* cas /home/toto.c */
|
---|
495 | is++; ip=len-1;
|
---|
496 | } else { /* cas toto.c */
|
---|
497 | is=0; ip=len-1;
|
---|
498 | }
|
---|
499 | break;
|
---|
500 | case 'r':
|
---|
501 | if(is==0) { /* cas /... */
|
---|
502 | if(ip<0) ip=is=-1; /* cas /home */
|
---|
503 | else if(ip==1) is=ip=-1; /* cas /.c */
|
---|
504 | else {is=1; ip--;} /* cas /toto.c */
|
---|
505 | } else if(is>0) { /* cas /home/... */
|
---|
506 | if(ip<0) {is++;ip=len-1;} /* cas /home/toto */
|
---|
507 | else if(ip==is+1) is=ip=-1; /* cas /home/.c */
|
---|
508 | else {is++; ip--;} /* cas /home/toto.c */
|
---|
509 | } else if(is<0) { /* cas toto.c */
|
---|
510 | if(ip<0) {is=0;ip=len-1;} /* cas toto */
|
---|
511 | else if(ip==0) is=ip=-1; /* cas .c */
|
---|
512 | else {is=0; ip--;} /* cas toto.c */
|
---|
513 | }
|
---|
514 | break;
|
---|
515 | case 't':
|
---|
516 | if(ip<0) is=ip=-1; /* cas sans '.' dans le nom */
|
---|
517 | else {is=ip+1,ip=len-1;} /* cas blablabla.c */
|
---|
518 | break;
|
---|
519 | default:
|
---|
520 | cout[0]='\0';
|
---|
521 | return cout;
|
---|
522 | break;
|
---|
523 | }
|
---|
524 |
|
---|
525 | j=0;
|
---|
526 | if(is>=0&&ip>=is) for(i=is,j=0;i<=ip;i++,j++) cout[j]=cin[i];
|
---|
527 | cout[j]='\0';
|
---|
528 | /* printf("........ is=%d ip=%d :%s:\n",is,ip,cout); */
|
---|
529 | return cout;
|
---|
530 | }
|
---|