-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hsDarwin with helpers for string conversion
The NSString category in the macOS plClient folder is useful, but there are several other spots where we're wanting to convert between CFString/NSString and ST::string, so it make sense to have helpers available throughout the engine. These helper functions are written so that they can be used from C++ code without needing to bring in Objective-C.
- Loading branch information
Showing
17 changed files
with
310 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ You can contact Cyan Worlds, Inc. by email [email protected] | |
|
||
#include "HeadSpin.h" | ||
#include "hsWindows.h" | ||
#include "hsDarwin.h" | ||
|
||
#include <string_theory/formatter> | ||
#include <string_theory/string> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/*==LICENSE==* | ||
CyanWorlds.com Engine - MMOG client, server and tools | ||
Copyright (C) 2011 Cyan Worlds, Inc. | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
Additional permissions under GNU GPL version 3 section 7 | ||
If you modify this Program, or any covered work, by linking or | ||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, | ||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent | ||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK | ||
(or a modified version of those libraries), | ||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, | ||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG | ||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the | ||
licensors of this Program grant you additional | ||
permission to convey the resulting work. Corresponding Source for a | ||
non-source form of such a combination shall include the source code for | ||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered | ||
work. | ||
You can contact Cyan Worlds, Inc. by email [email protected] | ||
or by snail mail at: | ||
Cyan Worlds, Inc. | ||
14617 N Newport Hwy | ||
Mead, WA 99021 | ||
*==LICENSE==*/ | ||
|
||
#ifndef _hsDarwin_inc_ | ||
#define _hsDarwin_inc_ | ||
|
||
#include <string_theory/string> | ||
#include <string_theory/format> | ||
|
||
#ifdef HS_BUILD_FOR_APPLE | ||
#include <CoreFoundation/CoreFoundation.h> | ||
|
||
[[nodiscard]] | ||
#if __has_feature(attribute_cf_returns_retained) | ||
__attribute__((cf_returns_retained)) | ||
#endif | ||
inline CFStringRef CFStringCreateWithSTString(const ST::string& str) | ||
{ | ||
return CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)str.data(), str.size(), kCFStringEncodingUTF8, false); | ||
} | ||
|
||
inline ST::string STStringFromCFString(CFStringRef str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION) | ||
{ | ||
CFRange range = CFRangeMake(0, CFStringGetLength(str)); | ||
CFIndex strBufSz = 0; | ||
CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &strBufSz); | ||
ST::char_buffer buffer; | ||
buffer.allocate(strBufSz); | ||
CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, (UInt8*)buffer.data(), strBufSz, nullptr); | ||
|
||
return ST::string(buffer, validation); | ||
} | ||
|
||
inline void format_type(const ST::format_spec &format, ST::format_writer &output, CFStringRef str) | ||
{ | ||
ST::char_buffer utf8 = STStringFromCFString(str).to_utf8(); | ||
ST::format_string(format, output, utf8.data(), utf8.size()); | ||
} | ||
|
||
|
||
#ifdef __OBJC__ | ||
@class NSString; | ||
|
||
[[nodiscard]] | ||
#if __has_feature(attribute_ns_returns_retained) | ||
__attribute__((ns_returns_retained)) | ||
#endif | ||
inline NSString* NSStringCreateWithSTString(const ST::string& str) | ||
{ | ||
#if __has_feature(objc_arc) | ||
return (NSString*)CFBridgingRelease(CFStringCreateWithSTString(str)); | ||
#else | ||
return (NSString*)CFStringCreateWithSTString(str); | ||
#endif | ||
} | ||
|
||
inline ST::string STStringFromNSString(NSString* str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION) | ||
{ | ||
#if __has_feature(objc_arc) | ||
return STStringFromCFString((__bridge CFStringRef)str, validation); | ||
#else | ||
return STStringFromCFString((CFStringRef)str, validation); | ||
#endif | ||
} | ||
|
||
inline void format_type(const ST::format_spec &format, ST::format_writer &output, NSString* str) | ||
{ | ||
ST::char_buffer utf8 = STStringFromNSString(str).to_utf8(); | ||
ST::format_string(format, output, utf8.data(), utf8.size()); | ||
} | ||
#endif // __OBJC__ | ||
|
||
#endif // HS_BUILD_FOR_APPLE | ||
|
||
#endif // _hsDarwin_inc_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ You can contact Cyan Worlds, Inc. by email [email protected] | |
#include "plFileSystem.h" | ||
#include "hsStream.h" | ||
#include "hsWindows.h" | ||
#include "hsDarwin.h" | ||
|
||
#include <cstring> | ||
#include <iterator> | ||
|
@@ -191,13 +192,7 @@ static inline bool IGetAppleVersion(ST::string& system) | |
CFRelease(name); | ||
CFRelease(dict); | ||
|
||
CFIndex infoLen = CFStringGetLength(info); | ||
CFIndex infoBufSz = 0; | ||
CFStringGetBytes(info, CFRangeMake(0, infoLen), kCFStringEncodingUTF8, 0, false, nullptr, 0, &infoBufSz); | ||
ST::char_buffer systemBuf; | ||
systemBuf.allocate(infoBufSz); | ||
CFStringGetBytes(info, CFRangeMake(0, infoLen), kCFStringEncodingUTF8, 0, false, (UInt8*)systemBuf.data(), infoLen, nullptr); | ||
system = ST::string(systemBuf); | ||
system = STStringFromCFString(info); | ||
|
||
CFRelease(info); | ||
|
||
|
Oops, something went wrong.