00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <ctype.h>
00039
00040 #include "uti.h"
00041 #include "xalloc.h"
00042
00043
00044
00045
00046
00047 #ifdef STRUPR
00048
00049 char * strupr( char * str )
00050 {
00051 char* tmp=str;
00052 do
00053 (*tmp) = toupper(*tmp);
00054 while (*(tmp++));
00055 return(str);
00056 }
00057
00058 #endif
00059
00060
00061
00062
00063
00064 #ifdef STRLWR
00065
00066 char * strlwr( char * str )
00067 {
00068 char* tmp=str;
00069 do
00070 (*tmp) = tolower(*tmp);
00071 while (*(tmp++));
00072 return(str);
00073 }
00074
00075 #endif
00076
00077
00078
00079
00080
00081
00082 #ifdef STRDUP
00083
00084 char * strdup( const char * str )
00085 {
00086 char * retval;
00087
00088 if ((retval=(char *)malloc(strlen(str)+1))!=NULL)
00089 strcpy(retval,str);
00090
00091 return(retval);
00092 }
00093
00094 #endif
00095
00096
00097
00098
00099
00100
00101
00102
00103 #ifdef STRNICMP
00104 #ifndef __CC_GNUC__
00105 #error mierda
00106 int strnicmp( const char * s1, const char * s2, size_t maxlen )
00107 {
00108 size_t i;
00109 int k;
00110
00111 for (i=0; i<maxlen; i++) {
00112 k = toupper(*s1)-toupper(*s2);
00113 if ((k)||(!(*s1))||(!(*s2)))
00114 return k;
00115 s1++;
00116 s2++;
00117 }
00118 return 0;
00119 }
00120 #endif
00121 #endif
00122
00123
00124
00125
00126
00127
00128 #ifdef STRICMP
00129 #ifndef __CC_GNUC__
00130 int stricmp( const char * s1, const char * s2 )
00131 {
00132 int k;
00133
00134 do
00135 k = toupper(*s1)-toupper(*s2);
00136 while ((!k)&&(*s1)&&(*s2));
00137
00138 return k;
00139 }
00140 #endif
00141 #endif
00142
00143
00144
00145 #ifdef STRISTR
00146 char * stristr( const char * s1, const char * s2 )
00147 {
00148 const char *ss1, *ss2;
00149
00150 while (*s1) {
00151
00152 ss1 = s1;
00153 ss2 = s2;
00154 while (*ss1) {
00155 if (toupper(*ss1)!=toupper(*ss2))
00156 break;
00157 ss2++;
00158 ss1++;
00159 }
00160 if (!(*ss2))
00161 return (char*)s1;
00162
00163 s1++;
00164 }
00165
00166 return NULL;
00167 }
00168
00169 #endif
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 const char *strsearch( const char* txt, const char* sub, int casesens )
00182
00183 {
00184 int len;
00185
00186 if (!sub) return NULL;
00187 if (!txt) return NULL;
00188
00189 len = strlen(sub);
00190
00191 if ((*sub=='$') && (sub[len-1]=='$')) {
00192 if (casesens) { if (!strncmp(sub+1,txt,len-2) && ((int)strlen(txt)==len-2)) return txt; }
00193 else if (!strnicmp(sub+1,txt,len-2) && ((int)strlen(txt)==len-2)) return txt;
00194 return NULL;
00195 }
00196
00197 if (*sub=='$') {
00198 if (casesens) { if (!strncmp(txt,sub+1,len-1)) return txt; }
00199 else if (!strnicmp(txt,sub+1,len-1)) return txt;
00200 return NULL;
00201 }
00202
00203 if (len && (sub[len-1]=='$')) {
00204 txt += strlen(txt)-(len-1);
00205 if (casesens) { if (!strncmp(txt,sub,len-1)); return txt; }
00206 else if (!strnicmp(txt,sub,len-1)) return txt;
00207 return NULL;
00208 }
00209
00210 return strstr(txt,sub);
00211 }
00212
00213
00214
00215
00216
00217 char * str_stripch( char * str, char sch )
00218 {
00219 char * ch1, * ch2;
00220
00221 ch1=ch2=str;
00222 do
00223 if ((*ch1)!=sch) *(ch2++) = (*ch1);
00224 while (*(ch1++));
00225
00226 return(str);
00227 }
00228
00229
00230
00231
00232
00233 char * str_stripchset( char * str, const char *schs )
00234 {
00235 char * ch1, * ch2;
00236
00237 ch1=ch2=str;
00238 do
00239 if (strchr(schs,*ch1)==NULL) *(ch2++) = (*ch1);
00240 while (*(ch1++));
00241
00242 return(str);
00243 }
00244
00245
00246
00247
00248
00249
00250 char * str_cutfromch( char * s, char cutch )
00251 {
00252 char * cut;
00253
00254 if ((cut=strchr(s,cutch))!=NULL)
00255 *cut = '\0';
00256
00257 return(s);
00258 }
00259
00260
00261
00262
00263
00264
00265 char * str_cutfromchset( char * s, const char* cutchset )
00266 {
00267 s[strcspn(s,cutchset)]='\0';
00268 return(s);
00269 }
00270
00271
00272
00273
00274
00275 char * str_xchr( const char * s, char ch )
00276 {
00277 char * ass;
00278
00279 ass = strchr(s,ch);
00280 if (ass==NULL)
00281 return (char*)(s+strlen(s));
00282 else
00283 return (ass);
00284 }
00285
00286
00287
00288
00289
00290
00291 char * str_xchrnext( const char * s, char ch )
00292 {
00293 char * ass;
00294
00295 ass = strchr(s,ch);
00296 if (ass==NULL)
00297 return (char*)(s+strlen(s));
00298 else
00299 return(ass+1);
00300 }
00301
00302
00303
00304
00305
00306
00307 char * str_xchrnextdiv( char * s, char ch )
00308 {
00309 char * ass;
00310
00311 ass = strchr(s,ch);
00312 if (ass==NULL)
00313 return(s+strlen(s));
00314 else {
00315 *ass = '\0';
00316 return(ass+1);
00317 }
00318 }
00319
00320
00321
00322
00323
00324
00325 char * str_assignptr( const char * s )
00326 {
00327 return str_xchr(s,FILE_KVASSIGN);
00328 }
00329
00330
00331
00332
00333
00334
00335 char * str_assignptrnext( const char * s )
00336 {
00337 return str_xchrnext(s,FILE_KVASSIGN);
00338 }
00339
00340
00341
00342
00343
00344
00345 int str_isswitch( const char * str )
00346 {
00347 return (str[0] && str[1] && (strchr(CL_SWITCH,str[0])!=NULL));
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 int str2f( const char * str, float * f )
00358 {
00359 int n, i;
00360 i=sscanf(str,"%f%n",f,&n);
00361 if (i!=1) return -1;
00362 if (str[n]!='\0') return n;
00363 return 0;
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 int str2d( const char * str, double * d )
00373 {
00374 int n, i;
00375 i=sscanf(str,"%lf%n",d,&n);
00376 if (i!=1) return -1;
00377 if (str[n]!='\0') return n;
00378 return 0;
00379 }
00380
00381
00382
00383
00384
00385
00386
00387 int str2si( const char * str, short int * si )
00388 {
00389 int n, i;
00390 i=sscanf(str,"%hd%n",si,&n);
00391 if (i!=1) return -1;
00392 if (str[n]!='\0') return n;
00393 return 0;
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403 int str2i( const char * str, int * ii )
00404 {
00405 int n, i;
00406 i=sscanf(str,"%d%n",ii,&n);
00407 if (i!=1) return -1;
00408 if (str[n]!='\0') return n;
00409 return 0;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418 int str2ld( const char * str, long double * ld )
00419 {
00420 int n, i;
00421 i=sscanf(str,"%Lf%n",ld,&n);
00422 if (i!=1) return -1;
00423 if (str[n]!='\0') return n;
00424 return 0;
00425 }
00426
00427
00428
00429
00430
00431
00432
00433 int str2li( const char * str, long int * li )
00434 {
00435 int n, i;
00436 i=sscanf(str,"%ld%n",li,&n);
00437 if (i!=1) return -1;
00438 if (str[n]!='\0') return n;
00439 return 0;
00440 }
00441
00442
00443
00444
00445
00446
00447 float xstr2f( const char * str, float defval)
00448 {
00449 float f;
00450 if (strlen(str)==0) return(defval);
00451 cdie_beep(str2f(str,&f)!=0,"valor numerico no valido (%s)",str);
00452 return f;
00453 }
00454
00455
00456
00457
00458
00459
00460 short int xstr2si( const char * str, short int defval )
00461 {
00462 short int si;
00463 if (strlen(str)==0) return(defval);
00464 cdie_beep(str2si(str,&si)!=0,"valor entero no valido (%s)",str);
00465 return si;
00466 }
00467
00468
00469
00470
00471
00472
00473 double xstr2d( const char * str, double defval)
00474 {
00475 double d;
00476 if (strlen(str)==0) return(defval);
00477 cdie_beep(str2d(str,&d)!=0,"valor numerico no valido (%s)",str);
00478 return d;
00479 }
00480
00481
00482
00483
00484
00485
00486 int xstr2i( const char * str, int defval )
00487 {
00488 int i;
00489 if (strlen(str)==0) return(defval);
00490 cdie_beep(str2i(str,&i)!=0,"valor entero no valido (%s)",str);
00491 return i;
00492 }
00493
00494
00495
00496
00497
00498
00499 long double xstr2ld( const char * str, long double defval)
00500 {
00501 long double ld;
00502 if (strlen(str)==0) return(defval);
00503 cdie_beep(str2ld(str,&ld)!=0,"valor numerico no valido (%s)",str);
00504 return ld;
00505 }
00506
00507
00508
00509
00510
00511
00512 long int xstr2li( const char * str, long int defval )
00513 {
00514 long int li;
00515 if (strlen(str)==0) return(defval);
00516 cdie_beep(str2li(str,&li)!=0,"valor entero no valido (%s)",str);
00517 return li;
00518 }
00519
00520
00521
00522
00523 int str2bool( const char *str, int def )
00524 {
00525 char *yes[]={"yes", "on", "true", "bai", "si",NULL};
00526 char *no[]= {"no", "off", "false", "ez", NULL};
00527 char *pos="+123456789sSyYtTbB";
00528 char *neg="-0nNfFeE";
00529 char **s;
00530 if (!str || (*str=='\0')) return def;
00531 s=yes; while (*s) { if (!stricmp(str,*s)) return 1; s++; }
00532 s=no; while (*s) { if (!stricmp(str,*s)) return 0; s++; }
00533 if (strchr(pos,*str)) return 1;
00534 if (strchr(neg,*str)) return 0;
00535 return def;
00536 }
00537
00538
00539
00540 const char *bool2str( int boo )
00541 {
00542 return boo?"True":"False";
00543 }
00544
00545
00546
00547
00548
00549 char *vstr_dup( char *oldvs, const char *newstr )
00550 {
00551 char* vs;
00552
00553 vstr_free(oldvs);
00554
00555 if (newstr!=NULL) {
00556 vs=(char*)xmalloc(strlen(newstr)+1);
00557 strcpy(vs,newstr);
00558 return vs;
00559 }
00560 else
00561 return NULL;
00562 }
00563
00564
00565
00566
00567 void vstr_free( char* vs )
00568 {
00569 if (vs!=NULL) xfree(vs);
00570 }
00571
00572
00573
00574
00575 char* vstr_getstr( const char* vs, const char* defval )
00576 {
00577 if (vs==NULL) return (char*)defval;
00578 else return (char*)vs;
00579 }
00580
00581
00582