Concatenating Strings in a REALbasic Plugin
November 22nd, 2007 by Joe RanieriConcatenating strings in REALbasic itself is a very simple operation:
dim str as string = "foo" + "bar"
However, the plugins SDK provides no function for concatenating REALstrings together. There’s also no function we can load through REALLoadObjectMethod or REALLoadGlobalMethod. While it would be nice if it was provided, it’s rather easy to do yourself.
//! Creates a new string by concatenating `firstString` and `secondString`. REALstring ALConcatStrings(REALstring firstString, REALstring secondString) { REALstring convertedFirstString = NULL; REALstring convertedSecondString = NULL; bool needsUnlock = false; if(REALGetStringEncoding(firstString) == REALGetStringEncoding(secondString)) { // encodings are equal, so no need to convert anything convertedFirstString = firstString; convertedSecondString = secondString; } else { // if the two strings aren't in the same encoding, we need to change them both to UTF8 convertedFirstString = REALConvertString(firstString, kREALTextEncodingUTF8); convertedSecondString = REALConvertString(secondString, kREALTextEncodingUTF8); needsUnlock = true; } // the total length (in bytes) of the resulting string size_t totalLength = convertedFirstString->Length() + convertedSecondString->Length(); // ok, now we need to create a buffer for the total string contents char *buffer = (char *)malloc(totalLength); // copy in the first and second strings' data to the right spots memcpy(buffer, convertedFirstString->CString(), convertedFirstString->Length()); memcpy(buffer + convertedFirstString->Length(), convertedSecondString->CString(), convertedSecondString->Length()); // next we create a REALstring from it REALstring result = REALBuildString(buffer, totalLength, REALGetStringEncoding(convertedFirstString)); // free our buffer (REALbasic copies it on BuildString) free(buffer); // unlock our converted strings if we have to if(needsUnlock) { REALUnlockString(convertedFirstString); REALUnlockString(convertedSecondString); } // phew, that was easy enough! return result; }
I apologize for the awful formatting of this page! It looks decent in an RSS reader at least…
A minor update to CalendarKit is now available, bringing the current release to version 1.1. This update fixes all reported bugs and introduces Tasks, or “to-dos.”