#include #include #include "debug.h" #include "globals.h" BSTR SysAllocCString(LPCSTR sNew, UINT iChars) { wchar_t *sWChar = (wchar_t*) malloc(iChars*2+2); _snwprintf(sWChar, iChars, L"%S", sNew); BSTR bStr = SysAllocStringLen(sWChar, iChars); free(sWChar); #ifdef _DEBUG char sStatus[1024]; _snprintf(sStatus, sizeof(sStatus), "{%s(%i)}={%S}", sNew, iChars, bStr); DEBUGPRINT(sStatus, DEBUG_LINE); #endif return bStr; } int SysReAllocCString(BSTR &bStr, LPCSTR sNew, UINT iChars) { SysFreeString(bStr); bStr=SysAllocCString(sNew, iChars); return (bStr!=NULL); } UINT _bstrtowcs(const BSTR sBSTR, wchar_t **sWCS) { UINT iBSTRLen = SysStringLen(sBSTR); *sWCS = (wchar_t*) malloc(iBSTRLen*2+2); _snwprintf(*sWCS, iBSTRLen, L"%s", sBSTR); (*sWCS)[iBSTRLen] = (wchar_t) 0; return iBSTRLen; } UINT _bstrtocs(const BSTR sBSTR, char **sCS) { UINT iBSTRLen = SysStringLen(sBSTR); *sCS = (char*) malloc(iBSTRLen+1); _snprintf(*sCS, iBSTRLen, "%S", sBSTR); (*sCS)[iBSTRLen] = 0; return iBSTRLen; } const char *strndup(const char *_value, size_t size) { //caller responsible for freeing char *temp = 0; if (_value) { temp = (char*)malloc(size+1); memcpy(temp, _value, size); temp[size] = 0; } return temp; } char *stristr(char *haystack, const char *needle) { return (char*)stristr((const char*)haystack, needle); } const char *stristr(const char *haystack, const char *needle) { const char *pos1; char *pos2; char c, ncL, ncU; char *needleLower; size_t len; //invalid input cases if (!needle || !*needle) return haystack; if (!haystack || !*haystack) return 0; //both strings are valid //make lower case version of the needle len = strlen(needle); needleLower = (char*)malloc(len+1); pos1 = needle; pos2 = needleLower; while (c = *pos1++) { if (c >= 'A' && c <= 'Z') *pos2 = c + ('a' - 'A'); else *pos2 = c; pos2++; } *pos2 = 0; //search pos1 = haystack; pos2 = needleLower; ncL = *pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; //search while (ncL && (c = *pos1++)) { if (c == ncL || c == ncU) { //match success during test ncL = *++pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; } else if (pos2 != needleLower) { //match fail during test, back to beginning pos2 = needleLower; ncL = *pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; } } free(needleLower); if (ncL) return 0; //return 0 if not found at all return pos1 - len; }