summaryrefslogtreecommitdiff
path: root/zen/osx_string.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:23:48 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:23:48 +0200
commitee1c8c5c25d25dfa42120125a8a45dc9831ee412 (patch)
tree67aa287157db954e0cadeee05b4aad331eb2ecf2 /zen/osx_string.h
parent5.13 (diff)
downloadFreeFileSync-ee1c8c5c25d25dfa42120125a8a45dc9831ee412.tar.gz
FreeFileSync-ee1c8c5c25d25dfa42120125a8a45dc9831ee412.tar.bz2
FreeFileSync-ee1c8c5c25d25dfa42120125a8a45dc9831ee412.zip
5.14
Diffstat (limited to 'zen/osx_string.h')
-rw-r--r--zen/osx_string.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/zen/osx_string.h b/zen/osx_string.h
new file mode 100644
index 00000000..a5c0849e
--- /dev/null
+++ b/zen/osx_string.h
@@ -0,0 +1,82 @@
+// **************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
+// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
+// **************************************************************************
+
+#ifndef OSX_STRING_1873641732143214324
+#define OSX_STRING_1873641732143214324
+
+#include <zen/zstring.h>
+#include <CoreFoundation/CoreFoundation.h> //CFString
+
+namespace osx
+{
+Zstring cfStringToZstring(const CFStringRef& cfStr);
+
+CFStringRef createCFString (const char* utf8Str); //returns nullptr on error
+CFMutableStringRef createMutableCFString(const char* utf8Str); //pass ownership! => ZEN_ON_SCOPE_EXIT(::CFRelease(utf8Str));
+
+
+
+
+
+
+
+
+
+
+
+
+
+//################# implementation #####################
+inline
+Zstring cfStringToZstring(const CFStringRef& cfStr)
+{
+ if (cfStr)
+ {
+ //perf: try to get away cheap:
+ if (const char* utf8Str = ::CFStringGetCStringPtr(cfStr, kCFStringEncodingUTF8))
+ return utf8Str;
+
+ CFIndex length = ::CFStringGetLength(cfStr);
+ if (length > 0)
+ {
+ CFIndex bufferSize = ::CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
+ Zstring buffer;
+ buffer.resize(bufferSize);
+
+ if (::CFStringGetCString(cfStr, &*buffer.begin(), bufferSize, kCFStringEncodingUTF8))
+ {
+ buffer.resize(zen::strLength(&*buffer.begin())); //caveat: memory consumption of returned string!
+ return buffer;
+ }
+ }
+ }
+ return Zstring();
+}
+
+
+inline
+CFStringRef createCFString(const char* utf8Str)
+{
+ //don't bother with CFStringCreateWithBytes: it's slightly slower, despite passing length info
+ return ::CFStringCreateWithCString(nullptr, //CFAllocatorRef alloc,
+ utf8Str, //const char *cStr,
+ kCFStringEncodingUTF8); //CFStringEncoding encoding
+}
+
+
+inline
+CFMutableStringRef createMutableCFString(const char* utf8Str)
+{
+ if (CFMutableStringRef strRef = ::CFStringCreateMutable(NULL, 0))
+ {
+ ::CFStringAppendCString(strRef, utf8Str, kCFStringEncodingUTF8);
+ return strRef;
+ }
+ return nullptr;
+}
+}
+
+#endif //OSX_STRING_1873641732143214324
bgstack15