/*@@ @file Misc.c @date Wed Jan 20 10:06:35 1999 @author Tom Goodale @desc Miscellaneuous routines. @enddesc @@*/ /*#define DEBUG_MISC*/ #include #include #include #include #include #include #include "gnu_regex.h" #include "config.h" #include "cctk_Misc.h" #include "cctk_FortranString.h" #include "cctk_WarnLevel.h" /*@@ @routine Util_SplitString @date Wed Jan 20 10:14:00 1999 @author Tom Goodale @desc Splits a string into two parts at the given seperator. Assigns memory for the two resulting strings, so this should be freed when no longer needed. @enddesc @calls @calledby @history @endhistory @@*/ int Util_SplitString(char **before, char **after, const char *string, const char *sep) { int retval; char *position; /* Find location of the seperator */ position = strstr(string, sep); if(position) { /*Allocate memory for return strings. */ *before = (char *)malloc((position-string+1)*sizeof(char)); *after = (char *)malloc((strlen(string)-(position-string)-strlen(sep)+1)*sizeof(char)); /* Check that the allocation succeeded. */ if(!*before || !*after) { free(*before); *before = NULL; free(*after); *after = NULL; retval = 2; } } else { *before = NULL; *after = NULL; retval = 1; } if(position && before && after) { /* Copy the data */ strncpy(*before, string, (int)(position-string)); (*before)[(int)(position-string)] = '\0'; strncpy(*after, position+strlen(sep), strlen(string)-(int)(position-string)-strlen(sep)); (*after)[strlen(string)-(position-string)-strlen(sep)] = '\0'; retval = 0; } return retval; } /*@@ @routine CCTK_Equals @date Wed Jan 20 10:25:30 1999 @author Tom Goodale @desc Does a case independent comparison of strings. Returns true if they are equal. @enddesc @calls @calledby @history @endhistory @@*/ int CCTK_Equals(const char *string1, const char *string2) { int retval; int position; char *message; retval = 1; /* Check that string1 isn't null */ if (!string1 || !string2) { if (!string1 && string2) { message = (char *)malloc((100+sizeof(string2))*sizeof(char)); sprintf(message,"First string null in CCTK_Equals (2nd is %s)",string2); CCTK_Warn(0,__LINE__,__FILE__,"Cactus",message); } else if (string1 && !string2) { message = (char *)malloc((100+sizeof(string1))*sizeof(char)); sprintf(message,"Second string null in CCTK_Equals (1st is %s)",string1); CCTK_Warn(0,__LINE__,__FILE__,"Cactus",message); } else { CCTK_Warn(0,__LINE__,__FILE__,"Cactus","Both strings null in CCTK_Equals"); } } if(strlen(string1)==strlen(string2)) { for(position = 0; position < strlen(string1);position++) { if(tolower(string1[position]) != tolower(string2[position])) { retval = 0; break; } } } else { retval = 0; } return retval; } int FMODIFIER FORTRAN_NAME(CCTK_Equals)(const char **arg1,ONE_FORTSTRING_ARG) { int retval; ONE_FORTSTRING_CREATE(arg2) retval = CCTK_Equals(*arg1,arg2); free(arg2); return(retval); } /*@@ @routine Util_NullTerminateString @author Paul Walker @desc Null terminates a fortran string. Remember to free what it returns... @enddesc @@*/ char *Util_NullTerminateString(const char *instring, unsigned int len) { char *outstring; unsigned int i; #ifdef DEBUG_MISC printf("Util_NullTerminateString: -%s-, (%u)\n",instring,len); #endif outstring = (char *)malloc((len+2)*sizeof(char)); assert(outstring); for (i=0;i