|
| 1 | +/*==LICENSE==* |
| 2 | +
|
| 3 | +CyanWorlds.com Engine - MMOG client, server and tools |
| 4 | +Copyright (C) 2011 Cyan Worlds, Inc. |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +
|
| 19 | +Additional permissions under GNU GPL version 3 section 7 |
| 20 | +
|
| 21 | +If you modify this Program, or any covered work, by linking or |
| 22 | +combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
| 23 | +NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
| 24 | +JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
| 25 | +(or a modified version of those libraries), |
| 26 | +containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
| 27 | +PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
| 28 | +JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
| 29 | +licensors of this Program grant you additional |
| 30 | +permission to convey the resulting work. Corresponding Source for a |
| 31 | +non-source form of such a combination shall include the source code for |
| 32 | +the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
| 33 | +work. |
| 34 | +
|
| 35 | +You can contact Cyan Worlds, Inc. by email [email protected] |
| 36 | + or by snail mail at: |
| 37 | + Cyan Worlds, Inc. |
| 38 | + 14617 N Newport Hwy |
| 39 | + Mead, WA 99021 |
| 40 | +
|
| 41 | +*==LICENSE==*/ |
| 42 | + |
| 43 | +#ifndef _hsDarwin_inc_ |
| 44 | +#define _hsDarwin_inc_ |
| 45 | + |
| 46 | +#include <string_theory/string> |
| 47 | +#include <string_theory/format> |
| 48 | + |
| 49 | +#ifdef HS_BUILD_FOR_APPLE |
| 50 | +#include <CoreFoundation/CoreFoundation.h> |
| 51 | + |
| 52 | +[[nodiscard]] |
| 53 | +#if __has_feature(attribute_cf_returns_retained) |
| 54 | +__attribute__((cf_returns_retained)) |
| 55 | +#endif |
| 56 | +inline CFStringRef CFStringCreateWithSTString(const ST::string& str) |
| 57 | +{ |
| 58 | + return CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)str.data(), str.size(), kCFStringEncodingUTF8, false); |
| 59 | +} |
| 60 | + |
| 61 | +inline ST::string STStringFromCFString(CFStringRef str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION) |
| 62 | +{ |
| 63 | + CFRange range = CFRangeMake(0, CFStringGetLength(str)); |
| 64 | + CFIndex strBufSz = 0; |
| 65 | + CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &strBufSz); |
| 66 | + ST::char_buffer buffer; |
| 67 | + buffer.allocate(strBufSz); |
| 68 | + CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, (UInt8*)buffer.data(), strBufSz, nullptr); |
| 69 | + |
| 70 | + return ST::string(buffer, validation); |
| 71 | +} |
| 72 | + |
| 73 | +inline void format_type(const ST::format_spec &format, ST::format_writer &output, CFStringRef str) |
| 74 | +{ |
| 75 | + ST::char_buffer utf8 = STStringFromCFString(str).to_utf8(); |
| 76 | + ST::format_string(format, output, utf8.data(), utf8.size()); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +#ifdef __OBJC__ |
| 81 | +@class NSString; |
| 82 | + |
| 83 | +[[nodiscard]] |
| 84 | +#if __has_feature(attribute_ns_returns_retained) |
| 85 | +__attribute__((ns_returns_retained)) |
| 86 | +#endif |
| 87 | +inline NSString* NSStringCreateWithSTString(const ST::string& str) |
| 88 | +{ |
| 89 | +#if __has_feature(objc_arc) |
| 90 | + return (NSString*)CFBridgingRelease(CFStringCreateWithSTString(str)); |
| 91 | +#else |
| 92 | + return (NSString*)CFStringCreateWithSTString(str); |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +inline ST::string STStringFromNSString(NSString* str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION) |
| 97 | +{ |
| 98 | +#if __has_feature(objc_arc) |
| 99 | + return STStringFromCFString((__bridge CFStringRef)str, validation); |
| 100 | +#else |
| 101 | + return STStringFromCFString((CFStringRef)str, validation); |
| 102 | +#endif |
| 103 | +} |
| 104 | + |
| 105 | +inline void format_type(const ST::format_spec &format, ST::format_writer &output, NSString* str) |
| 106 | +{ |
| 107 | + ST::char_buffer utf8 = STStringFromNSString(str).to_utf8(); |
| 108 | + ST::format_string(format, output, utf8.data(), utf8.size()); |
| 109 | +} |
| 110 | +#endif // __OBJC__ |
| 111 | + |
| 112 | +#endif // HS_BUILD_FOR_APPLE |
| 113 | + |
| 114 | +#endif // _hsDarwin_inc_ |
0 commit comments