Actual source code: str.c
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.
7: */
8: #include petsc.h
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"
20: /*@C
21: PetscStrlen - Gets length of a string
23: Not Collective
25: Input Parameters:
26: . s - pointer to string
28: Output Parameter:
29: . len - length in bytes
31: Level: intermediate
33: Note:
34: This routine is analogous to strlen().
36: Null string returns a length of zero
38: Concepts: string length
39:
40: @*/
41: PetscErrorCode PetscStrlen(const char s[],size_t *len)
42: {
44: if (!s) {
45: *len = 0;
46: } else {
47: *len = strlen(s);
48: }
49: return(0);
50: }
54: /*@C
55: PetscStrallocpy - Allocates space to hold a copy of a string then copies the string
57: Not Collective
59: Input Parameters:
60: . s - pointer to string
62: Output Parameter:
63: . t - the copied string
65: Level: intermediate
67: Note:
68: Null string returns a new null string
70: Concepts: string copy
71:
72: @*/
73: PetscErrorCode PetscStrallocpy(const char s[],char *t[])
74: {
76: size_t len;
77: char *tmp = 0;
80: if (s) {
81: PetscStrlen(s,&len);
82: PetscMalloc((1+len)*sizeof(char),&tmp);
83: PetscStrcpy(tmp,s);
84: }
85: *t = tmp;
86: return(0);
87: }
91: /*@C
92: PetscStrcpy - Copies a string
94: Not Collective
96: Input Parameters:
97: . t - pointer to string
99: Output Parameter:
100: . s - the copied string
102: Level: intermediate
104: Note:
105: Null string returns a string starting with zero
107: Concepts: string copy
108:
109: .seealso: PetscStrncpy(), PetscStrcat(), PetscStrncat()
111: @*/
112: PetscErrorCode PetscStrcpy(char s[],const char t[])
113: {
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: return(0);
121: }
125: /*@C
126: PetscStrncpy - Copies a string up to a certain length
128: Not Collective
130: Input Parameters:
131: + t - pointer to string
132: - n - the length to copy
134: Output Parameter:
135: . s - the copied string
137: Level: intermediate
139: Note:
140: Null string returns a string starting with zero
142: Concepts: string copy
144: .seealso: PetscStrcpy(), PetscStrcat(), PetscStrncat()
145:
146: @*/
147: PetscErrorCode PetscStrncpy(char s[],const char t[],size_t n)
148: {
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: return(0);
156: }
160: /*@C
161: PetscStrcat - Concatenates a string onto a given string
163: Not Collective
165: Input Parameters:
166: + s - string to be added to
167: - t - pointer to string to be added to end
169: Level: intermediate
171: Concepts: string copy
173: .seealso: PetscStrcpy(), PetscStrncpy(), PetscStrncat()
174:
175: @*/
176: PetscErrorCode PetscStrcat(char s[],const char t[])
177: {
179: strcat(s,t);
180: return(0);
181: }
185: /*@C
186: PetscStrncat - Concatenates a string onto a given string, up to a given length
188: Not Collective
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
195: Level: intermediate
197: Concepts: string copy
199: .seealso: PetscStrcpy(), PetscStrncpy(), PetscStrcat()
200:
201: @*/
202: PetscErrorCode PetscStrncat(char s[],const char t[],size_t n)
203: {
205: strncat(s,t,n);
206: return(0);
207: }
211: /*@C
212: PetscStrcmp - Compares two strings,
214: Not Collective
216: Input Parameters:
217: + a - pointer to string first string
218: - b - pointer to second string
220: Output Parameter:
221: . flg - if the two strings are equal
223: Level: intermediate
225: .seealso: PetscStrgrt(), PetscStrncmp(), PetscStrcasecmp()
227: @*/
228: PetscErrorCode PetscStrcmp(const char a[],const char b[],PetscTruth *flg)
229: {
230: int c;
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: return(0);
243: }
247: /*@C
248: PetscStrgrt - If first string is greater than the second
250: Not Collective
252: Input Parameters:
253: + a - pointer to first string
254: - b - pointer to second string
256: Output Parameter:
257: . flg - if the first string is greater
259: Notes:
260: Null arguments are ok, a null string is considered smaller than
261: all others
263: Level: intermediate
265: .seealso: PetscStrcmp(), PetscStrncmp(), PetscStrcasecmp()
267: @*/
268: PetscErrorCode PetscStrgrt(const char a[],const char b[],PetscTruth *t)
269: {
270: int c;
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: return(0);
285: }
289: /*@C
290: PetscStrcasecmp - Returns true if the two strings are the same
291: except possibly for case.
293: Not Collective
295: Input Parameters:
296: + a - pointer to first string
297: - b - pointer to second string
299: Output Parameter:
300: . flg - if the two strings are the same
302: Notes:
303: Null arguments are ok
305: Level: intermediate
307: .seealso: PetscStrcmp(), PetscStrncmp(), PetscStrgrt()
309: @*/
310: PetscErrorCode PetscStrcasecmp(const char a[],const char b[],PetscTruth *t)
311: {
312: int c;
315: if (!a && !b) c = 0;
316: else if (!a || !b) c = 1;
317: #if defined(PETSC_HAVE_STRCASECMP)
318: else c = strcasecmp(a,b);
319: #elif defined(PETSC_HAVE_STRICMP)
320: else c = stricmp(a,b);
321: #else
322: else {
323: char *aa,*bb;
325: PetscStrallocpy(a,&aa);
326: PetscStrallocpy(b,&bb);
327: PetscStrtolower(aa);
328: PetscStrtolower(bb);
329: PetscStrcmp(aa,bb,t);
330: PetscStrfree(aa);
331: PetscStrfree(bb);
332: return(0);
333: }
334: #endif
335: if (!c) *t = PETSC_TRUE;
336: else *t = PETSC_FALSE;
337: return(0);
338: }
344: /*@C
345: PetscStrncmp - Compares two strings, up to a certain length
347: Not Collective
349: Input Parameters:
350: + a - pointer to first string
351: . b - pointer to second string
352: - n - length to compare up to
354: Output Parameter:
355: . t - if the two strings are equal
357: Level: intermediate
359: .seealso: PetscStrgrt(), PetscStrcmp(), PetscStrcasecmp()
361: @*/
362: PetscErrorCode PetscStrncmp(const char a[],const char b[],size_t n,PetscTruth *t)
363: {
364: int c;
367: c = strncmp(a,b,n);
368: if (!c) *t = PETSC_TRUE;
369: else *t = PETSC_FALSE;
370: return(0);
371: }
375: /*@C
376: PetscStrchr - Locates first occurance of a character in a string
378: Not Collective
380: Input Parameters:
381: + a - pointer to string
382: - b - character
384: Output Parameter:
385: . c - location of occurance, PETSC_NULL if not found
387: Level: intermediate
389: @*/
390: PetscErrorCode PetscStrchr(const char a[],char b,char *c[])
391: {
393: *c = (char *)strchr(a,b);
394: return(0);
395: }
399: /*@C
400: PetscStrrchr - Locates one location past the last occurance of a character in a string,
401: if the character is not found then returns entire string
403: Not Collective
405: Input Parameters:
406: + a - pointer to string
407: - b - character
409: Output Parameter:
410: . tmp - location of occurance, a if not found
412: Level: intermediate
414: @*/
415: PetscErrorCode PetscStrrchr(const char a[],char b,char *tmp[])
416: {
418: *tmp = (char *)strrchr(a,b);
419: if (!*tmp) *tmp = (char*)a; else *tmp = *tmp + 1;
420: return(0);
421: }
425: /*@C
426: PetscStrtolower - Converts string to lower case
428: Not Collective
430: Input Parameters:
431: . a - pointer to string
433: Level: intermediate
435: @*/
436: PetscErrorCode PetscStrtolower(char a[])
437: {
439: while (*a) {
440: if (*a >= 'A' && *a <= 'Z') *a += 'a' - 'A';
441: a++;
442: }
443: return(0);
444: }
448: /*@C
449: PetscTokenFind - Locates next "token" in a string
451: Not Collective
453: Input Parameters:
454: . a - pointer to token
456: Output Parameter:
457: . result - location of occurance, PETSC_NULL if not found
459: Notes:
461: This version is different from the system version in that
462: it allows you to pass a read-only string into the function.
464: Level: intermediate
466: .seealso: PetscTokenCreate(), PetscTokenDestroy()
467: @*/
468: PetscErrorCode PetscTokenFind(PetscToken *a,char *result[])
469: {
470: char *ptr = a->current;
473: *result = a->current;
474: if (ptr && !*ptr) *result = 0;
475: while (ptr) {
476: if (*ptr == a->token) {
477: *ptr++ = 0;
478: while (*ptr == a->token) ptr++;
479: a->current = ptr;
480: break;
481: }
482: if (!*ptr) {
483: a->current = 0;
484: break;
485: }
486: ptr++;
487: }
488: return(0);
489: }
493: /*@C
494: PetscTokenCreate - Creates a PetscToken used to find tokens in a string
496: Not Collective
498: Input Parameters:
499: + string - the string to look in
500: - token - the character to look for
502: Output Parameter:
503: . a - pointer to token
505: Notes:
507: This version is different from the system version in that
508: it allows you to pass a read-only string into the function.
510: Level: intermediate
512: .seealso: PetscTokenFind(), PetscTokenDestroy()
513: @*/
514: PetscErrorCode PetscTokenCreate(const char a[],const char b,PetscToken **t)
515: {
519: PetscNew(PetscToken,t);
520: PetscStrallocpy(a,&(*t)->array);
521: (*t)->current = (*t)->array;
522: (*t)->token = b;
523: return(0);
524: }
528: /*@C
529: PetscTokenDestroy - Destroys a PetscToken
531: Not Collective
533: Input Parameters:
534: . a - pointer to token
536: Level: intermediate
538: .seealso: PetscTokenCreate(), PetscTokenFind()
539: @*/
540: PetscErrorCode PetscTokenDestroy(PetscToken *a)
541: {
545: PetscFree(a->array);
546: PetscFree(a);
547: return(0);
548: }
552: /*@C
553: PetscStrrstr - Locates last occurance of string in another string
555: Not Collective
557: Input Parameters:
558: + a - pointer to string
559: - b - string to find
561: Output Parameter:
562: . tmp - location of occurance
564: Level: intermediate
566: @*/
567: PetscErrorCode PetscStrrstr(const char a[],const char b[],char *tmp[])
568: {
569: const char *stmp = a, *ltmp = 0;
572: while (stmp) {
573: stmp = (char *)strstr(stmp,b);
574: if (stmp) {ltmp = stmp;stmp++;}
575: }
576: *tmp = (char *)ltmp;
577: return(0);
578: }
582: /*@C
583: PetscStrstr - Locates first occurance of string in another string
585: Not Collective
587: Input Parameters:
588: + a - pointer to string
589: - b - string to find
591: Output Parameter:
592: . tmp - location of occurance
594: Level: intermediate
596: @*/
597: PetscErrorCode PetscStrstr(const char a[],const char b[],char *tmp[])
598: {
600: *tmp = (char *)strstr(a,b);
601: return(0);
602: }
606: /*@C
607: PetscGetPetscDir - Gets the directory PETSc is installed in
609: Not Collective
611: Output Parameter:
612: . dir - the directory
614: Level: developer
616: @*/
617: PetscErrorCode PetscGetPetscDir(const char *dir[])
618: {
620: *dir = PETSC_DIR;
621: return(0);
622: }
626: /*@C
627: PetscStrreplace - Replaces substrings in string with other substrings
629: Not Collective
631: Input Parameters:
632: + comm - MPI_Comm of processors that are processing the string
633: . aa - the string to look in
634: . b - the resulting copy of a with replaced strings (b can be the same as a)
635: - len - the length of b
637: Notes:
638: Replaces ${PETSC_ARCH},${PETSC_DIR},${PETSC_LIB_DIR},${DISPLAY},
639: ${HOMEDIRECTORY},${WORKINGDIRECTORY},${USERNAME} with appropriate values
640: as well as any environmental variables.
641:
642: Level: intermediate
644: @*/
645: PetscErrorCode PetscStrreplace(MPI_Comm comm,const char aa[],char b[],size_t len)
646: {
648: int i = 0;
649: size_t l,l1,l2,l3;
650: char *work,*par,*epar,env[1024],*tfree,*a = (char*)aa;
651: const char *s[] = {"${PETSC_ARCH}","${PETSC_DIR}","${PETSC_LIB_DIR}","${DISPLAY}","${HOMEDIRECTORY}","${WORKINGDIRECTORY}","${USERNAME}",0};
652: const char *r[] = {PETSC_ARCH,PETSC_DIR,PETSC_LIB_DIR,0,0,0,0,0};
653: PetscTruth flag;
656: if (!a || !b) SETERRQ(PETSC_ERR_ARG_NULL,"a and b strings must be nonnull");
657: if (aa == b) {
658: PetscStrallocpy(aa,(char **)&a);
659: }
660: PetscMalloc(len*sizeof(char*),&work);
662: /* get values for replaced variables */
663: PetscMalloc(256*sizeof(char),&r[4]);
664: PetscMalloc(PETSC_MAX_PATH_LEN*sizeof(char),&r[5]);
665: PetscMalloc(PETSC_MAX_PATH_LEN*sizeof(char),&r[6]);
666: PetscMalloc(256*sizeof(char),&r[7]);
667: PetscGetDisplay((char*)r[4],256);
668: PetscGetHomeDirectory((char*)r[5],PETSC_MAX_PATH_LEN);
669: PetscGetWorkingDirectory((char*)r[6],PETSC_MAX_PATH_LEN);
670: PetscGetUserName((char*)r[7],256);
672: /* replace the requested strings */
673: PetscStrncpy(b,a,len);
674: while (s[i]) {
675: PetscStrlen(s[i],&l);
676: PetscStrstr(b,s[i],&par);
677: while (par) {
678: *par = 0;
679: par += l;
681: PetscStrlen(b,&l1);
682: PetscStrlen(r[i],&l2);
683: PetscStrlen(par,&l3);
684: if (l1 + l2 + l3 >= len) {
685: SETERRQ(PETSC_ERR_ARG_SIZ,"b len is not long enough to hold new values");
686: }
687: PetscStrcpy(work,b);
688: PetscStrcat(work,r[i]);
689: PetscStrcat(work,par);
690: PetscStrncpy(b,work,len);
691: PetscStrstr(b,s[i],&par);
692: }
693: i++;
694: }
695: for ( i=4; i<8; i++){
696: tfree = (char*)r[i];
697: PetscFree(tfree);
698: }
700: /* look for any other ${xxx} strings to replace from environmental variables */
701: PetscStrstr(b,"${",&par);
702: while (par) {
703: *par = 0;
704: par += 2;
705: PetscStrcpy(work,b);
706: PetscStrstr(par,"}",&epar);
707: *epar = 0;
708: epar += 1;
709: PetscOptionsGetenv(comm,par,env,256,&flag);
710: if (!flag) {
711: SETERRQ1(PETSC_ERR_ARG_WRONG,"Substitution string ${%s} not found as environmental variable",par);
712: }
713: PetscStrcat(work,env);
714: PetscStrcat(work,epar);
715: PetscStrcpy(b,work);
716: PetscStrstr(b,"${",&par);
717: }
718: PetscFree(work);
719: if (aa == b) {
720: PetscFree(a);
721: }
722: return(0);
723: }
725: /*MC
726: PetscStrfree - Frees a string (if it is not null)
728: Not Collective
730: Synopsis:
731: PetscErrorCode PetscStrfree(char *s)
733: Input Parameter:
734: . s - pointer to string
736: Level: intermediate
738: Concepts: string free
739:
740: .seealso: PetscStrncpy(), PetscStrcat(), PetscStrncat(), PetscStrallocpy()
742: M*/