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