| 28 | |
| 29 | |
| 30 | const char *stristr(const char *szStringToBeSearched, const char *szSubstringToSearchFor) |
| 31 | { |
| 32 | const char *pPos = NULL; |
| 33 | char *szCopy1 = NULL; |
| 34 | char *szCopy2 = NULL; |
| 35 | |
| 36 | // verify parameters |
| 37 | if ( szStringToBeSearched == NULL || |
| 38 | szSubstringToSearchFor == NULL ) |
| 39 | { |
| 40 | return szStringToBeSearched; |
| 41 | } |
| 42 | |
| 43 | // empty substring - return input (consistent with strstr) |
| 44 | if (strlen(szSubstringToSearchFor) == 0 ) { |
| 45 | return szStringToBeSearched; |
| 46 | } |
| 47 | |
| 48 | szCopy1 = dStrlwr(strdup(szStringToBeSearched)); |
| 49 | szCopy2 = dStrlwr(strdup(szSubstringToSearchFor)); |
| 50 | |
| 51 | if ( szCopy1 == NULL || szCopy2 == NULL ) { |
| 52 | // another option is to raise an exception here |
| 53 | free((void*)szCopy1); |
| 54 | free((void*)szCopy2); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | pPos = strstr((const char*)szCopy1, (const char*)szCopy2); |
| 59 | |
| 60 | if ( pPos != NULL ) { |
| 61 | // map to the original string |
| 62 | pPos = szStringToBeSearched + (pPos - szCopy1); |
| 63 | } |
| 64 | |
| 65 | free((void*)szCopy1); |
| 66 | free((void*)szCopy2); |
| 67 | |
| 68 | return pPos; |
| 69 | } // stristr(...) |
| 70 | |