static int GM_CDECL gmStringTrimRight(gmThread * a_thread) { GM_STRING_PARAM(trim, 0, GM_WHITE_SPACE); const gmVariable * var = a_thread->GetThis(); GM_ASSERT(var->m_type == GM_STRING); gmStringObject * strObj = (gmStringObject *) GM_OBJECT(var->m_value.m_ref); const char * str = (const char *) *strObj; int strLength = strObj->GetLength(); if(strLength > 0) { char * buffer = (char *) alloca(strLength + 1); memcpy(buffer, str, strLength + 1); //Copy old string // Find beginning of trailing matches by starting at end char *lpsz = buffer + strLength; while (--lpsz >= buffer && strchr(trim, *lpsz) != NULL) {} ++lpsz; *lpsz = '\0'; a_thread->PushNewString(buffer); } else { a_thread->PushString(strObj); } return GM_OK; }
static int GM_CDECL gmfFormatTime(gmThread * a_thread) { GM_INT_PARAM(t, 0, -1); GM_STRING_PARAM(format, 1, "%A %d %B %Y, %I:%M:%S %p"); char buffer[256]; if(t == -1) { time_t lt; time(<); t = (int) lt; } struct tm * ct = localtime((time_t *) &t); strftime(buffer, 256, format, ct); a_thread->PushNewString(buffer); return GM_OK; }
static int GM_CDECL gmStringTrimLeft(gmThread * a_thread) { GM_STRING_PARAM(trim, 0, GM_WHITE_SPACE); const gmVariable * var = a_thread->GetThis(); GM_ASSERT(var->m_type == GM_STRING); gmStringObject * strObj = (gmStringObject *) GM_OBJECT(var->m_value.m_ref); const char * str = (const char *) *strObj; if(strlen(str) > 0) { while(*str && strchr(trim, *str)) ++str; a_thread->PushNewString(str); } else a_thread->PushString(strObj); return GM_OK; }
// string.RemoveInvalidChars(a_replaceChar, a_invalidSet) // eg. "File Name#1.tga".RemoveInvalidChars("_","# ") returns "File_Name_1.tga" // Note: Parameters are optional. static int GM_CDECL gmfStringReplaceCharsInSet(gmThread * a_thread) { GM_INT_PARAM(repCharInt, 0, '_'); GM_STRING_PARAM(invalidCharSet, 1, " \\/:-+"); char repChar = (char)repCharInt; //Convert full int to char const gmVariable * varA = a_thread->GetThis(); GM_ASSERT(varA->m_type == GM_STRING); gmStringObject * strObjA = (gmStringObject *) GM_OBJECT(varA->m_value.m_ref); const char* cStrA = strObjA->GetString(); int lenA = strObjA->GetLength(); //Alloc buffer on stack is fine, path strings cannot be long char * buffer = (char *) alloca(lenA + 1); memcpy(buffer, cStrA, lenA + 1); int validPos; //Check that replacement char is NOT in invalid set, otherwise endless loop... if(strchr(invalidCharSet, repChar)) { return GM_EXCEPTION; } for(;;) { validPos = strcspn(buffer, invalidCharSet); if(validPos != lenA) { buffer[validPos] = repChar; } else { break; } } a_thread->PushNewString(buffer, lenA); return GM_OK; }
static int GM_CDECL gmStringSetExtension(gmThread * a_thread) { GM_STRING_PARAM(newExt, 0, ""); const gmVariable * var = a_thread->GetThis(); GM_ASSERT(var->m_type == GM_STRING); gmStringObject * strObj = (gmStringObject *) GM_OBJECT(var->m_value.m_ref); const char * str = (const char *) *strObj; int strLength = strObj->GetLength(); int extLength = strlen(newExt); if (extLength && newExt[0] == '.') { ++newExt; extLength = strlen(newExt); } char *buffer = (char *) alloca(strLength + 1 + extLength); memcpy(buffer, str, strLength + 1); char *lpsz = buffer + strLength; while (--lpsz >= buffer && *lpsz != '.') {} if(*lpsz == '.') { *lpsz = '\0'; if (extLength) sprintf(buffer, "%s.%s", buffer, newExt); } else if (extLength) { sprintf(buffer, "%s.%s", buffer, newExt); } a_thread->PushNewString(buffer); return GM_OK; }