1 #define PETSC_DLL 2 /* 3 We define the string operations here. The reason we just do not use 4 the standard string routines in the PETSc code is that on some machines 5 they are broken or have the wrong prototypes. 6 7 */ 8 #include "petsc.h" /*I "petsc.h" I*/ 9 #include "petscsys.h" 10 #if defined(PETSC_HAVE_STRING_H) 11 #include <string.h> 12 #endif 13 #if defined(PETSC_HAVE_STRINGS_H) 14 #include <strings.h> 15 #endif 16 #include "petscfix.h" 17 18 #undef __FUNCT__ 19 #define __FUNCT__ "PetscStrlen" 20 /*@C 21 PetscStrlen - Gets length of a string 22 23 Not Collective 24 25 Input Parameters: 26 . s - pointer to string 27 28 Output Parameter: 29 . len - length in bytes 30 31 Level: intermediate 32 33 Note: 34 This routine is analogous to strlen(). 35 36 Null string returns a length of zero 37 38 Concepts: string length 39 40 @*/ 41 PetscErrorCode PETSC_DLLEXPORT PetscStrlen(const char s[],size_t *len) 42 { 43 PetscFunctionBegin; 44 if (!s) { 45 *len = 0; 46 } else { 47 *len = strlen(s); 48 } 49 PetscFunctionReturn(0); 50 } 51 52 #undef __FUNCT__ 53 #define __FUNCT__ "PetscStrallocpy" 54 /*@C 55 PetscStrallocpy - Allocates space to hold a copy of a string then copies the string 56 57 Not Collective 58 59 Input Parameters: 60 . s - pointer to string 61 62 Output Parameter: 63 . t - the copied string 64 65 Level: intermediate 66 67 Note: 68 Null string returns a new null string 69 70 Concepts: string copy 71 72 @*/ 73 PetscErrorCode PETSC_DLLEXPORT PetscStrallocpy(const char s[],char *t[]) 74 { 75 PetscErrorCode ierr; 76 size_t len; 77 78 PetscFunctionBegin; 79 if (s) { 80 ierr = PetscStrlen(s,&len);CHKERRQ(ierr); 81 ierr = PetscMalloc((1+len)*sizeof(char),t);CHKERRQ(ierr); 82 ierr = PetscStrcpy(*t,s);CHKERRQ(ierr); 83 } else { 84 *t = 0; 85 } 86 PetscFunctionReturn(0); 87 } 88 89 #undef __FUNCT__ 90 #define __FUNCT__ "PetscStrcpy" 91 /*@C 92 PetscStrcpy - Copies a string 93 94 Not Collective 95 96 Input Parameters: 97 . t - pointer to string 98 99 Output Parameter: 100 . s - the copied string 101 102 Level: intermediate 103 104 Note: 105 Null string returns a string starting with zero 106 107 Concepts: string copy 108 109 .seealso: PetscStrncpy(), PetscStrcat(), PetscStrncat() 110 111 @*/ 112 PetscErrorCode PETSC_DLLEXPORT PetscStrcpy(char s[],const char t[]) 113 { 114 PetscFunctionBegin; 115 if (t && !s) { 116 SETERRQ(PETSC_ERR_ARG_NULL,"Trying to copy string into null pointer"); 117 } 118 if (t) {strcpy(s,t);} 119 else if (s) {s[0] = 0;} 120 PetscFunctionReturn(0); 121 } 122 123 #undef __FUNCT__ 124 #define __FUNCT__ "PetscStrncpy" 125 /*@C 126 PetscStrncpy - Copies a string up to a certain length 127 128 Not Collective 129 130 Input Parameters: 131 + t - pointer to string 132 - n - the length to copy 133 134 Output Parameter: 135 . s - the copied string 136 137 Level: intermediate 138 139 Note: 140 Null string returns a string starting with zero 141 142 Concepts: string copy 143 144 .seealso: PetscStrcpy(), PetscStrcat(), PetscStrncat() 145 146 @*/ 147 PetscErrorCode PETSC_DLLEXPORT PetscStrncpy(char s[],const char t[],size_t n) 148 { 149 PetscFunctionBegin; 150 if (t && !s) { 151 SETERRQ(PETSC_ERR_ARG_NULL,"Trying to copy string into null pointer"); 152 } 153 if (t) {strncpy(s,t,n);} 154 else if (s) {s[0] = 0;} 155 PetscFunctionReturn(0); 156 } 157 158 #undef __FUNCT__ 159 #define __FUNCT__ "PetscStrcat" 160 /*@C 161 PetscStrcat - Concatenates a string onto a given string 162 163 Not Collective 164 165 Input Parameters: 166 + s - pointer to string to be added to end 167 - t - string to be added to 168 169 Level: intermediate 170 171 Concepts: string copy 172 173 .seealso: PetscStrcpy(), PetscStrncpy(), PetscStrncat() 174 175 @*/ 176 PetscErrorCode PETSC_DLLEXPORT PetscStrcat(char s[],const char t[]) 177 { 178 PetscFunctionBegin; 179 strcat(s,t); 180 PetscFunctionReturn(0); 181 } 182 183 #undef __FUNCT__ 184 #define __FUNCT__ "PetscStrncat" 185 /*@C 186 PetscStrncat - Concatenates a string onto a given string, up to a given length 187 188 Not Collective 189 190 Input Parameters: 191 + s - pointer to string to be added to end 192 . t - string to be added to 193 . n - maximum length to copy 194 195 Level: intermediate 196 197 Concepts: string copy 198 199 .seealso: PetscStrcpy(), PetscStrncpy(), PetscStrcat() 200 201 @*/ 202 PetscErrorCode PETSC_DLLEXPORT PetscStrncat(char s[],const char t[],size_t n) 203 { 204 PetscFunctionBegin; 205 strncat(s,t,n); 206 PetscFunctionReturn(0); 207 } 208 209 #undef __FUNCT__ 210 #define __FUNCT__ "PetscStrcmp" 211 /*@C 212 PetscStrcmp - Compares two strings, 213 214 Not Collective 215 216 Input Parameters: 217 + a - pointer to string first string 218 - b - pointer to second string 219 220 Output Parameter: 221 . flg - if the two strings are equal 222 223 Level: intermediate 224 225 .seealso: PetscStrgrt(), PetscStrncmp(), PetscStrcasecmp() 226 227 @*/ 228 PetscErrorCode PETSC_DLLEXPORT PetscStrcmp(const char a[],const char b[],PetscTruth *flg) 229 { 230 int c; 231 232 PetscFunctionBegin; 233 if (!a && !b) { 234 *flg = PETSC_TRUE; 235 } else if (!a || !b) { 236 *flg = PETSC_FALSE; 237 } else { 238 c = strcmp(a,b); 239 if (c) *flg = PETSC_FALSE; 240 else *flg = PETSC_TRUE; 241 } 242 PetscFunctionReturn(0); 243 } 244 245 #undef __FUNCT__ 246 #define __FUNCT__ "PetscStrgrt" 247 /*@C 248 PetscStrgrt - If first string is greater than the second 249 250 Not Collective 251 252 Input Parameters: 253 + a - pointer to first string 254 - b - pointer to second string 255 256 Output Parameter: 257 . flg - if the first string is greater 258 259 Notes: 260 Null arguments are ok, a null string is considered smaller than 261 all others 262 263 Level: intermediate 264 265 .seealso: PetscStrcmp(), PetscStrncmp(), PetscStrcasecmp() 266 267 @*/ 268 PetscErrorCode PETSC_DLLEXPORT PetscStrgrt(const char a[],const char b[],PetscTruth *t) 269 { 270 int c; 271 272 PetscFunctionBegin; 273 if (!a && !b) { 274 *t = PETSC_FALSE; 275 } else if (a && !b) { 276 *t = PETSC_TRUE; 277 } else if (!a && b) { 278 *t = PETSC_FALSE; 279 } else { 280 c = strcmp(a,b); 281 if (c > 0) *t = PETSC_TRUE; 282 else *t = PETSC_FALSE; 283 } 284 PetscFunctionReturn(0); 285 } 286 287 #undef __FUNCT__ 288 #define __FUNCT__ "PetscStrcasecmp" 289 /*@C 290 PetscStrcasecmp - Returns true if the two strings are the same 291 except possibly for case. 292 293 Not Collective 294 295 Input Parameters: 296 + a - pointer to first string 297 - b - pointer to second string 298 299 Output Parameter: 300 . flg - if the two strings are the same 301 302 Notes: 303 Null arguments are ok 304 305 Level: intermediate 306 307 .seealso: PetscStrcmp(), PetscStrncmp(), PetscStrgrt() 308 309 @*/ 310 PetscErrorCode PETSC_DLLEXPORT PetscStrcasecmp(const char a[],const char b[],PetscTruth *t) 311 { 312 int c; 313 314 PetscFunctionBegin; 315 if (!a && !b) c = 0; 316 else if (!a || !b) c = 1; 317 #if defined(PETSC_HAVE_STRICMP) 318 else c = stricmp(a,b); 319 #else 320 else c = strcasecmp(a,b); 321 #endif 322 if (!c) *t = PETSC_TRUE; 323 else *t = PETSC_FALSE; 324 PetscFunctionReturn(0); 325 } 326 327 #undef __FUNCT__ 328 #define __FUNCT__ "PetscStrncmp" 329 /*@C 330 PetscStrncmp - Compares two strings, up to a certain length 331 332 Not Collective 333 334 Input Parameters: 335 + a - pointer to first string 336 . b - pointer to second string 337 - n - length to compare up to 338 339 Output Parameter: 340 . t - if the two strings are equal 341 342 Level: intermediate 343 344 .seealso: PetscStrgrt(), PetscStrcmp(), PetscStrcasecmp() 345 346 @*/ 347 PetscErrorCode PETSC_DLLEXPORT PetscStrncmp(const char a[],const char b[],size_t n,PetscTruth *t) 348 { 349 int c; 350 351 PetscFunctionBegin; 352 c = strncmp(a,b,n); 353 if (!c) *t = PETSC_TRUE; 354 else *t = PETSC_FALSE; 355 PetscFunctionReturn(0); 356 } 357 358 #undef __FUNCT__ 359 #define __FUNCT__ "PetscStrchr" 360 /*@C 361 PetscStrchr - Locates first occurance of a character in a string 362 363 Not Collective 364 365 Input Parameters: 366 + a - pointer to string 367 - b - character 368 369 Output Parameter: 370 . c - location of occurance, PETSC_NULL if not found 371 372 Level: intermediate 373 374 @*/ 375 PetscErrorCode PETSC_DLLEXPORT PetscStrchr(const char a[],char b,char *c[]) 376 { 377 PetscFunctionBegin; 378 *c = (char *)strchr(a,b); 379 PetscFunctionReturn(0); 380 } 381 382 #undef __FUNCT__ 383 #define __FUNCT__ "PetscStrrchr" 384 /*@C 385 PetscStrrchr - Locates one location past the last occurance of a character in a string, 386 if the character is not found then returns entire string 387 388 Not Collective 389 390 Input Parameters: 391 + a - pointer to string 392 - b - character 393 394 Output Parameter: 395 . tmp - location of occurance, a if not found 396 397 Level: intermediate 398 399 @*/ 400 PetscErrorCode PETSC_DLLEXPORT PetscStrrchr(const char a[],char b,char *tmp[]) 401 { 402 PetscFunctionBegin; 403 *tmp = (char *)strrchr(a,b); 404 if (!*tmp) *tmp = (char*)a; else *tmp = *tmp + 1; 405 PetscFunctionReturn(0); 406 } 407 408 #undef __FUNCT__ 409 #define __FUNCT__ "PetscStrtolower" 410 /*@C 411 PetscStrtolower - Converts string to lower case 412 413 Not Collective 414 415 Input Parameters: 416 . a - pointer to string 417 418 Level: intermediate 419 420 @*/ 421 PetscErrorCode PETSC_DLLEXPORT PetscStrtolower(char a[]) 422 { 423 PetscFunctionBegin; 424 while (*a) { 425 if (*a >= 'A' && *a <= 'Z') *a += 'a' - 'A'; 426 a++; 427 } 428 PetscFunctionReturn(0); 429 } 430 431 #undef __FUNCT__ 432 #define __FUNCT__ "PetscTokenFind" 433 /*@C 434 PetscTokenFind - Locates next "token" in a string 435 436 Not Collective 437 438 Input Parameters: 439 . a - pointer to token 440 441 Output Parameter: 442 . result - location of occurance, PETSC_NULL if not found 443 444 Notes: 445 446 This version is different from the system version in that 447 it allows you to pass a read-only string into the function. 448 449 Level: intermediate 450 451 .seealso: PetscTokenCreate(), PetscTokenDestroy() 452 @*/ 453 PetscErrorCode PETSC_DLLEXPORT PetscTokenFind(PetscToken *a,char *result[]) 454 { 455 char *ptr = a->current; 456 457 PetscFunctionBegin; 458 *result = a->current; 459 if (ptr && !*ptr) *result = 0; 460 while (ptr) { 461 if (*ptr == a->token) { 462 *ptr++ = 0; 463 while (*ptr == a->token) ptr++; 464 a->current = ptr; 465 break; 466 } 467 if (!*ptr) { 468 a->current = 0; 469 break; 470 } 471 ptr++; 472 } 473 PetscFunctionReturn(0); 474 } 475 476 #undef __FUNCT__ 477 #define __FUNCT__ "PetscTokenCreate" 478 /*@C 479 PetscTokenCreate - Creates a PetscToken used to find tokens in a string 480 481 Not Collective 482 483 Input Parameters: 484 + string - the string to look in 485 - token - the character to look for 486 487 Output Parameter: 488 . a - pointer to token 489 490 Notes: 491 492 This version is different from the system version in that 493 it allows you to pass a read-only string into the function. 494 495 Level: intermediate 496 497 .seealso: PetscTokenFind(), PetscTokenDestroy() 498 @*/ 499 PetscErrorCode PETSC_DLLEXPORT PetscTokenCreate(const char a[],const char b,PetscToken **t) 500 { 501 PetscErrorCode ierr; 502 size_t len; 503 504 PetscFunctionBegin; 505 ierr = PetscNew(PetscToken,t);CHKERRQ(ierr); 506 ierr = PetscStrlen(a,&len);CHKERRQ(ierr); 507 ierr = PetscStrallocpy(a,&(*t)->array);CHKERRQ(ierr); 508 (*t)->current = (*t)->array; 509 (*t)->token = b; 510 PetscFunctionReturn(0); 511 } 512 513 #undef __FUNCT__ 514 #define __FUNCT__ "PetscTokenDestroy" 515 /*@C 516 PetscTokenDestroy - Destroys a PetscToken 517 518 Not Collective 519 520 Input Parameters: 521 . a - pointer to token 522 523 Level: intermediate 524 525 .seealso: PetscTokenCreate(), PetscTokenFind() 526 @*/ 527 PetscErrorCode PETSC_DLLEXPORT PetscTokenDestroy(PetscToken *a) 528 { 529 PetscErrorCode ierr; 530 531 PetscFunctionBegin; 532 ierr = PetscFree(a->array);CHKERRQ(ierr); 533 ierr = PetscFree(a);CHKERRQ(ierr); 534 PetscFunctionReturn(0); 535 } 536 537 #undef __FUNCT__ 538 #define __FUNCT__ "PetscStrrstr" 539 /*@C 540 PetscStrrstr - Locates last occurance of string in another string 541 542 Not Collective 543 544 Input Parameters: 545 + a - pointer to string 546 - b - string to find 547 548 Output Parameter: 549 . tmp - location of occurance 550 551 Level: intermediate 552 553 @*/ 554 PetscErrorCode PETSC_DLLEXPORT PetscStrrstr(const char a[],const char b[],char *tmp[]) 555 { 556 const char *stmp = a, *ltmp = 0; 557 558 PetscFunctionBegin; 559 while (stmp) { 560 stmp = (char *)strstr(stmp,b); 561 if (stmp) {ltmp = stmp;stmp++;} 562 } 563 *tmp = (char *)ltmp; 564 PetscFunctionReturn(0); 565 } 566 567 #undef __FUNCT__ 568 #define __FUNCT__ "PetscStrstr" 569 /*@C 570 PetscStrstr - Locates first occurance of string in another string 571 572 Not Collective 573 574 Input Parameters: 575 + a - pointer to string 576 - b - string to find 577 578 Output Parameter: 579 . tmp - location of occurance 580 581 Level: intermediate 582 583 @*/ 584 PetscErrorCode PETSC_DLLEXPORT PetscStrstr(const char a[],const char b[],char *tmp[]) 585 { 586 PetscFunctionBegin; 587 *tmp = (char *)strstr(a,b); 588 PetscFunctionReturn(0); 589 } 590 591 #undef __FUNCT__ 592 #define __FUNCT__ "PetscGetPetscDir" 593 /*@C 594 PetscGetPetscDir - Gets the directory PETSc is installed in 595 596 Not Collective 597 598 Output Parameter: 599 . dir - the directory 600 601 Level: developer 602 603 @*/ 604 PetscErrorCode PETSC_DLLEXPORT PetscGetPetscDir(const char *dir[]) 605 { 606 PetscFunctionBegin; 607 *dir = PETSC_DIR; 608 PetscFunctionReturn(0); 609 } 610 611 #undef __FUNCT__ 612 #define __FUNCT__ "PetscStrreplace" 613 /*@C 614 PetscStrreplace - Replaces substrings in string with other substrings 615 616 Not Collective 617 618 Input Parameters: 619 + comm - MPI_Comm of processors that are processing the string 620 . a - the string to look in 621 . b - the resulting copy of a with replaced strings 622 - len - the length of b 623 624 Notes: 625 Replaces ${PETSC_ARCH},${PETSC_DIR},${PETSC_LIB_DIR},${DISPLAY}, 626 ${HOMEDIRECTORY},${WORKINGDIRECTORY},${USERNAME} with appropriate values 627 as well as any environmental variables. 628 629 Level: intermediate 630 631 @*/ 632 PetscErrorCode PETSC_DLLEXPORT PetscStrreplace(MPI_Comm comm,const char a[],char b[],size_t len) 633 { 634 PetscErrorCode ierr; 635 int i = 0; 636 size_t l,l1,l2,l3; 637 char *work,*par,*epar,env[1024],*tfree; 638 const char *s[] = {"${PETSC_ARCH}","${PETSC_DIR}","${PETSC_LIB_DIR}","${DISPLAY}","${HOMEDIRECTORY}","${WORKINGDIRECTORY}","${USERNAME}",0}; 639 const char *r[] = {PETSC_ARCH,PETSC_DIR,PETSC_LIB_DIR,0,0,0,0,0}; 640 PetscTruth flag; 641 642 PetscFunctionBegin; 643 if (!a || !b) SETERRQ(PETSC_ERR_ARG_NULL,"a and b strings must be nonnull"); 644 ierr = PetscMalloc(len*sizeof(char*),&work);CHKERRQ(ierr); 645 646 /* get values for replaced variables */ 647 ierr = PetscMalloc(256*sizeof(char),&r[4]);CHKERRQ(ierr); 648 ierr = PetscMalloc(PETSC_MAX_PATH_LEN*sizeof(char),&r[5]);CHKERRQ(ierr); 649 ierr = PetscMalloc(PETSC_MAX_PATH_LEN*sizeof(char),&r[6]);CHKERRQ(ierr); 650 ierr = PetscMalloc(256*sizeof(char),&r[7]);CHKERRQ(ierr); 651 ierr = PetscGetDisplay((char*)r[4],256);CHKERRQ(ierr); 652 ierr = PetscGetHomeDirectory((char*)r[5],PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 653 ierr = PetscGetWorkingDirectory((char*)r[6],PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 654 ierr = PetscGetUserName((char*)r[7],256);CHKERRQ(ierr); 655 656 /* replace the requested strings */ 657 ierr = PetscStrncpy(b,a,len);CHKERRQ(ierr); 658 while (s[i]) { 659 ierr = PetscStrlen(s[i],&l);CHKERRQ(ierr); 660 ierr = PetscStrstr(b,s[i],&par);CHKERRQ(ierr); 661 while (par) { 662 *par = 0; 663 par += l; 664 665 ierr = PetscStrlen(b,&l1);CHKERRQ(ierr); 666 ierr = PetscStrlen(r[i],&l2);CHKERRQ(ierr); 667 ierr = PetscStrlen(par,&l3);CHKERRQ(ierr); 668 if (l1 + l2 + l3 >= len) { 669 SETERRQ(PETSC_ERR_ARG_SIZ,"b len is not long enough to hold new values"); 670 } 671 ierr = PetscStrcpy(work,b);CHKERRQ(ierr); 672 ierr = PetscStrcat(work,r[i]);CHKERRQ(ierr); 673 ierr = PetscStrcat(work,par);CHKERRQ(ierr); 674 ierr = PetscStrncpy(b,work,len);CHKERRQ(ierr); 675 ierr = PetscStrstr(b,s[i],&par);CHKERRQ(ierr); 676 } 677 i++; 678 } 679 for ( i=4; i<8; i++){ 680 tfree = (char*)r[i]; 681 ierr = PetscFree(tfree);CHKERRQ(ierr); 682 } 683 684 /* look for any other ${xxx} strings to replace from environmental variables */ 685 ierr = PetscStrstr(b,"${",&par);CHKERRQ(ierr); 686 while (par) { 687 *par = 0; 688 par += 2; 689 ierr = PetscStrcpy(work,b);CHKERRQ(ierr); 690 ierr = PetscStrstr(par,"}",&epar);CHKERRQ(ierr); 691 *epar = 0; 692 epar += 1; 693 ierr = PetscOptionsGetenv(comm,par,env,256,&flag);CHKERRQ(ierr); 694 if (!flag) { 695 SETERRQ1(PETSC_ERR_ARG_WRONG,"Substitution string ${%s} not found as environmental variable",par); 696 } 697 ierr = PetscStrcat(work,env);CHKERRQ(ierr); 698 ierr = PetscStrcat(work,epar);CHKERRQ(ierr); 699 ierr = PetscStrcpy(b,work);CHKERRQ(ierr); 700 ierr = PetscStrstr(b,"${",&par);CHKERRQ(ierr); 701 } 702 ierr = PetscFree(work);CHKERRQ(ierr); 703 PetscFunctionReturn(0); 704 } 705 706 /*MC 707 PetscStrfree - Frees a string (if it is not null) 708 709 Not Collective 710 711 Synopsis: 712 PetscErrorCode PetscStrfree(char *s) 713 714 Input Parameter: 715 . s - pointer to string 716 717 Level: intermediate 718 719 Concepts: string free 720 721 .seealso: PetscStrncpy(), PetscStrcat(), PetscStrncat(), PetscStrallocpy() 722 723 M*/ 724