bool MatchesExtension( const char * fileName, const char * ext ) { const int extLen = OVR_strlen( ext ); const int sLen = OVR_strlen( fileName ); if ( sLen < extLen + 1 ) { return false; } return ( 0 == strcmp( &fileName[ sLen - extLen ], ext ) ); }
String::String(const char* pdata1, const char* pdata2, const char* pdata3) { // Obtain length in bytes; it doesn't matter if _data is UTF8. UPInt size1 = pdata1 ? OVR_strlen(pdata1) : 0; UPInt size2 = pdata2 ? OVR_strlen(pdata2) : 0; UPInt size3 = pdata3 ? OVR_strlen(pdata3) : 0; DataDesc *pdataDesc = AllocDataCopy2(size1 + size2 + size3, 0, pdata1, size1, pdata2, size2); memcpy(pdataDesc->Data + size1 + size2, pdata3, size3); pData = pdataDesc; }
static bool ExtensionMatches( char const * fileName, char const * ext ) { if ( fileName == NULL || ext == NULL ) { return false; } size_t extLen = OVR_strlen( ext ); size_t fileNameLen = OVR_strlen( fileName ); if ( extLen > fileNameLen ) { return false; } return OVR_stricmp( fileName + fileNameLen - extLen, ext ) == 0; }
void StringBuffer::operator = (const char* pstr) { pstr = pstr ? pstr : ""; UPInt size = OVR_strlen(pstr); Resize(size); memcpy(pData, pstr, size); }
static void StripPath( char const * path, char * outName, size_t const outSize ) { if ( path[0] == '\0' ) { outName[0] = '\0'; return; } size_t n = OVR_strlen( path ); char const * fnameStart = NULL; for ( int i = n - 1; i >= 0; --i ) { if ( path[i] == PATH_SEPARATOR ) { fnameStart = &path[i]; break; } } if ( fnameStart != NULL ) { // this will copy 0 characters if the path separator was the last character OVR_strncpy( outName, outSize, fnameStart + 1, n - ( fnameStart - path ) ); } else { OVR_strcpy( outName, outSize, path ); } }
void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType, const char* fmt, va_list argList) { bool addNewline = true; switch(messageType) { case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break; case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break; case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break; case Log_Text: buffer[0] = 0; addNewline = false; break; case Log_DebugText: buffer[0] = 0; addNewline = false; break; default: buffer[0] = 0; addNewline = false; break; } size_t prefixLength = OVR_strlen(buffer); char *buffer2 = buffer + prefixLength; OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList); if (addNewline) OVR_strcat(buffer, bufferSize, "\n"); }
// Find trailing short filename in a path. const char* OVR_CDECL GetShortFilename(const char* purl) { size_t len = OVR_strlen(purl); for (size_t i=len; i>0; i--) if (purl[i]=='\\' || purl[i]=='/') return purl+i+1; return purl; }
// If pstr is NULL then the StringBuffer is cleared. void StringBuffer::operator = (const char* pstr) { pstr = pstr ? pstr : ""; size_t size = OVR_strlen(pstr); Resize(size); OVR_ASSERT((pData != NULL) || (size == 0)); memcpy(pData, pstr, size); }
// TODO: we really need a decent set of functions for path manipulation. OVR_String_PathUtil has // some bugs and doesn't have functionality for cross-platform path conversion. static void MakePathCanonical( char * path ) { int n = OVR_strlen( path ); for ( int i = 0; i < n; ++i ) { if ( path[i] == PATH_SEPARATOR_NON_CANONICAL ) { path[i] = PATH_SEPARATOR; } } }
static void AppendPath( char * path, size_t pathsize, char const * append ) { char appendCanonical[512]; OVR_strcpy( appendCanonical, sizeof( appendCanonical ), append ); MakePathCanonical( path ); int n = OVR_strlen( path ); if ( n > 0 && path[n - 1] != PATH_SEPARATOR && appendCanonical[0] != PATH_SEPARATOR ) { OVR_strcat( path, pathsize, PATH_SEPARATOR_STR ); } OVR_strcat( path, pathsize, appendCanonical ); }
void StringBuffer::AppendString(const char* putf8str, SPInt utf8StrSz) { if (!putf8str || !utf8StrSz) return; if (utf8StrSz == -1) utf8StrSz = (SPInt)OVR_strlen(putf8str); UPInt origSize = GetSize(); UPInt size = utf8StrSz + origSize; Resize(size); memcpy(pData + origSize, putf8str, utf8StrSz); }
void StringBuffer::AppendString(const char* putf8str, intptr_t utf8StrSz) { if (!putf8str || !utf8StrSz) return; if (utf8StrSz == -1) utf8StrSz = (intptr_t)OVR_strlen(putf8str); size_t origSize = GetSize(); size_t size = utf8StrSz + origSize; Resize(size); OVR_ASSERT(pData != NULL); memcpy(pData + origSize, putf8str, utf8StrSz); }
void String::AppendString(const char* putf8str, SPInt utf8StrSz) { if (!putf8str || !utf8StrSz) return; if (utf8StrSz == -1) utf8StrSz = (SPInt)OVR_strlen(putf8str); DataDesc* pdata = GetData(); UPInt oldSize = pdata->GetSize(); SetData(AllocDataCopy2(oldSize + (UPInt)utf8StrSz, 0, pdata->Data, oldSize, putf8str, (UPInt)utf8StrSz)); pdata->Release(); }
int String::CompareNoCase(const char* a, const char* b, SPInt len) { if (len) { SPInt f,l; SPInt slen = len; const char *s = b; do { f = (SPInt)OVR_tolower((int)(*(a++))); l = (SPInt)OVR_tolower((int)(*(b++))); } while (--len && f && (f == l) && *b != 0); if (f == l && (len != 0 || *b != 0)) { f = (SPInt)slen; l = (SPInt)OVR_strlen(s); return int(f - l); } return int(f - l); } else return (0-(int)OVR_strlen(b)); }
// Inserts substr at posAt void StringBuffer::Insert(const char* substr, UPInt posAt, SPInt len) { UPInt oldSize = Size; UPInt insertSize = (len < 0) ? OVR_strlen(substr) : (UPInt)len; UPInt byteIndex = LengthIsSize ? posAt : (UPInt)UTF8Util::GetByteIndex(posAt, pData, (SPInt)Size); OVR_ASSERT(byteIndex <= oldSize); Reserve(oldSize + insertSize); memmove(pData + byteIndex + insertSize, pData + byteIndex, oldSize - byteIndex + 1); memcpy (pData + byteIndex, substr, insertSize); LengthIsSize = false; Size = oldSize + insertSize; pData[Size] = 0; }
// Inserts substr at posAt void StringBuffer::Insert(const char* substr, size_t posAt, intptr_t len) { size_t oldSize = Size; size_t insertSize = (len < 0) ? OVR_strlen(substr) : (size_t)len; size_t byteIndex = LengthIsSize ? posAt : (size_t)UTF8Util::GetByteIndex(posAt, pData, (intptr_t)Size); OVR_ASSERT(byteIndex <= oldSize); Reserve(oldSize + insertSize); OVR_ASSERT(pData != NULL); // pData is unilaterally written to below. memmove(pData + byteIndex + insertSize, pData + byteIndex, oldSize - byteIndex + 1); memcpy (pData + byteIndex, substr, insertSize); LengthIsSize = false; Size = oldSize + insertSize; pData[Size] = 0; }
bool SearchPaths::ToRelativePath( char const * fullPath, char * outPath, const int outMaxLen ) const { // check if the path starts with any of the search paths const int n = Paths.GetSizeI(); for ( int i = 0; i < n; ++i ) { char const * path = Paths[i].ToCStr(); if ( strstr( fullPath, path ) == fullPath ) { size_t len = OVR_strlen( path ); OVR_sprintf( outPath, outMaxLen, "%s", fullPath + len ); return true; } } OVR_sprintf( outPath, outMaxLen, "%s", fullPath ); return false; }
String& String::Insert(const char* substr, UPInt posAt, SPInt strSize) { DataDesc* poldData = GetData(); UPInt oldSize = poldData->GetSize(); UPInt insertSize = (strSize < 0) ? OVR_strlen(substr) : (UPInt)strSize; UPInt byteIndex = (poldData->LengthIsSize()) ? posAt : (UPInt)UTF8Util::GetByteIndex(posAt, poldData->Data, oldSize); OVR_ASSERT(byteIndex <= oldSize); DataDesc* pnewData = AllocDataCopy2(oldSize + insertSize, 0, poldData->Data, byteIndex, substr, insertSize); memcpy(pnewData->Data + byteIndex + insertSize, poldData->Data + byteIndex, oldSize - byteIndex); SetData(pnewData); poldData->Release(); return *this; }
//============================== // ovrLexer::CopyResult void ovrLexer::CopyResult( char const * buffer, char * token, size_t const maxTokenSize ) { // NOTE: if any multi-byte characters are ever treated as quotes, this code must change if ( IsQuote( *buffer ) ) { size_t len = UTF8Util::GetLength( buffer ); const uint32_t lastChar = UTF8Util::GetCharAt( len - 1, buffer ); if ( IsQuote( lastChar ) ) { // The first char and last char are single-byte quotes, we can now just step past the first and not copy the last. char const * start = buffer + 1; len = OVR_strlen( start ); // We do not care about UTF length here since we know the quotes are a single bytes OVR_strncpy( token, maxTokenSize, start, len - 1 ); return; } } OVR_strcpy( token, maxTokenSize, buffer ); }
static void StripFileName( char const * path, char * outPath, size_t const outSize ) { size_t n = OVR_strlen( path ); char const * fnameStart = NULL; for ( int i = n - 1; i >= 0; --i ) { if ( path[i] == PATH_SEPARATOR ) { fnameStart = &path[i]; break; } } if ( fnameStart != NULL ) { OVR_strncpy( outPath, outSize, path, ( fnameStart - path ) + 1 ); } else { OVR_strcpy( outPath, outSize, path ); } }
bool SearchPaths::GetFullPath( char const * relativePath, char * outPath, const int outMaxLen ) const { OVR_ASSERT( outPath != NULL && outMaxLen >= 1 ); if ( FileExists( relativePath ) ) { OVR_sprintf( outPath, OVR_strlen( relativePath ) + 1, "%s", relativePath ); return true; } for ( int i = 0; i < Paths.GetSizeI(); ++i ) { OVR_sprintf( outPath, outMaxLen, "%s%s", Paths[i].ToCStr(), relativePath ); if ( FileExists( outPath ) ) { return true; // outpath is now set to the full path } } // just return the relative path if we never found the file OVR_sprintf( outPath, outMaxLen, "%s", relativePath ); return false; }
//----------------------------------------------------------------------------- // Serializes the JSON object and writes to the give file path bool JSON::Save(const char* path) { SysFile f; if (!f.Open(path, File::Open_Write | File::Open_Create | File::Open_Truncate, File::Mode_Write)) return false; char* text = PrintValue(0, true); if (text) { intptr_t len = OVR_strlen(text); OVR_ASSERT(len <= (intptr_t)(int)len); int bytes = f.Write((uint8_t*)text, (int)len); f.Close(); OVR_FREE(text); return (bytes == len); } else { return false; } }
// Returns true if head equals check plus zero or more characters. bool MatchesHead( const char * head, const char * check ) { const int l = OVR_strlen( head ); return 0 == OVR_strncmp( head, check, l ); }
void SystemActivities_AddEvent( char const * data ) { EventData * eventData = new EventData( data, OVR_strlen( data ) + 1 ); MainEventQueue->Enqueue( eventData ); LOG( "SystemActivities: queued event '%s'", data ); }
void String::StripProtocol() { const char* protocol = ScanPathProtocol(ToCStr()); if (protocol) AssignString(protocol, OVR_strlen(protocol)); }
//----------------------------------------------------------------------------- // Render an object to text. The returned string must be freed char* JSON::PrintObject(int depth, bool fmt) { char** entries = 0, **names = 0; char* out = 0; char* ptr, *ret, *str; intptr_t len = 7, i = 0, j; bool fail = false; // Count the number of entries. int numentries = GetItemCount(); // Explicitly handle empty object case if (numentries == 0) { out=(char*)OVR_ALLOC(fmt?depth+4:4); if (!out) return 0; ptr=out; *ptr++='{'; if (fmt) { *ptr++='\n'; for (i=0;i<depth-1;i++) *ptr++='\t'; } *ptr++='}'; *ptr++=0; return out; } // Allocate space for the names and the objects entries=(char**)OVR_ALLOC(numentries*sizeof(char*)); if (!entries) return 0; names=(char**)OVR_ALLOC(numentries*sizeof(char*)); if (!names) { OVR_FREE(entries); return 0; } memset(entries,0,sizeof(char*)*numentries); memset(names,0,sizeof(char*)*numentries); // Collect all the results into our arrays: depth++; if (fmt) len+=depth; JSON* child = Children.GetFirst(); while (!Children.IsNull(child)) { names[i] = str = PrintString(child->Name); entries[i++] = ret = child->PrintValue(depth, fmt); if (str && ret) { len += OVR_strlen(ret)+OVR_strlen(str)+2+(fmt?3+depth:0); } else { fail = true; break; } child = Children.GetNext(child); } // Try to allocate the output string if (!fail) out=(char*)OVR_ALLOC(len); if (!out) fail=true; // Handle failure if (fail) { for (i=0;i<numentries;i++) { if (names[i]) OVR_FREE(names[i]); if (entries[i]) OVR_FREE(entries[i]);} OVR_FREE(names); OVR_FREE(entries); return 0; } // Compose the output: *out = '{'; ptr = out+1; if (fmt) { #ifdef OVR_OS_WIN32 *ptr++ = '\r'; #endif *ptr++ = '\n'; } *ptr = 0; for (i=0; i<numentries; i++) { if (fmt) { for (j = 0; j < depth; j++) { *ptr++ = '\t'; } } OVR_strcpy(ptr, len - (ptr-out), names[i]); ptr += OVR_strlen(names[i]); *ptr++ =':'; if (fmt) { *ptr++ = '\t'; } OVR_strcpy(ptr, len - (ptr-out), entries[i]); ptr+=OVR_strlen(entries[i]); if (i != numentries - 1) { *ptr++ = ','; } if (fmt) { #ifdef OVR_OS_WIN32 *ptr++ = '\r'; #endif *ptr++ = '\n'; } *ptr = 0; OVR_FREE(names[i]); OVR_FREE(entries[i]); } OVR_FREE(names); OVR_FREE(entries); if (fmt) { for (i = 0; i < depth - 1; i++) { *ptr++ = '\t'; } } *ptr++='}'; *ptr++=0; return out; }
//----------------------------------------------------------------------------- // Render an array to text. The returned text must be freed char* JSON::PrintArray(int depth, bool fmt) { char ** entries; char * out = 0, *ptr,*ret; intptr_t len = 5; bool fail = false; // How many entries in the array? int numentries = GetItemCount(); if (!numentries) { out=(char*)OVR_ALLOC(3); if (out) OVR_strcpy(out, 3, "[]"); return out; } // Allocate an array to hold the values for each entries=(char**)OVR_ALLOC(numentries*sizeof(char*)); if (!entries) return 0; memset(entries,0,numentries*sizeof(char*)); //// Retrieve all the results: JSON* child = Children.GetFirst(); for (int i=0; i<numentries; i++) { //JSON* child = Children[i]; ret=child->PrintValue(depth+1, fmt); entries[i]=ret; if (ret) len+=OVR_strlen(ret)+2+(fmt?1:0); else { fail = true; break; } child = Children.GetNext(child); } // If we didn't fail, try to malloc the output string if (!fail) out=(char*)OVR_ALLOC(len); // If that fails, we fail. if (!out) fail = true; // Handle failure. if (fail) { for (int i=0; i<numentries; i++) { if (entries[i]) OVR_FREE(entries[i]); } OVR_FREE(entries); return 0; } // Compose the output array. *out='['; ptr=out+1; *ptr=0; for (int i=0; i<numentries; i++) { OVR_strcpy(ptr, len - (ptr-out), entries[i]); ptr+=OVR_strlen(entries[i]); if (i!=numentries-1) { *ptr++=','; if (fmt) *ptr++=' '; *ptr=0; } OVR_FREE(entries[i]); } OVR_FREE(entries); *ptr++=']'; *ptr++=0; return out; }
void String::operator = (const char* pstr) { AssignString(pstr, pstr ? OVR_strlen(pstr) : 0); }
String::String(const char* pdata) { // Obtain length in bytes; it doesn't matter if _data is UTF8. UPInt size = pdata ? OVR_strlen(pdata) : 0; pData = AllocDataCopy1(size, 0, pdata, size); };