Sha256: 18980e9591f1f482ff1b0380f4e65911d4ad9c2d7d0c1ed04b4b7e1f61fea6ec

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

#include <Gosu/Utility.hpp>
#include <Gosu/Platform.hpp>
#include "AppleUtility.hpp"
#import <Foundation/Foundation.h>
#include <stdexcept>
#include <vector>
using namespace std;

#ifdef GOSU_IS_IPHONE // (but could also be used for OS X)
wstring Gosu::utf8ToWstring(const string& s)
{
    if (s.empty()) {
        return wstring();
    }
    
    NSString *str = [NSString stringWithUTF8String:s.c_str()];
    vector<wchar_t> buffer(s.size());
    NSUInteger usedBufferCount;
    if (![str getBytes:&buffer[0]
             maxLength:buffer.size() * sizeof(wchar_t)
            usedLength:&usedBufferCount
              encoding:NSUTF32LittleEndianStringEncoding
               options:0
                 range:NSMakeRange(0, str.length)
            remainingRange:NULL]) {
        throw std::runtime_error("String " + s + " could not be converted to Unicode");
    }
    return wstring(&buffer[0], &buffer[0] + usedBufferCount / sizeof(wchar_t));
}

string Gosu::wstringToUTF8(const std::wstring& ws)
{
    if (ws.empty()) {
        return string();
    }

    @autoreleasepool {
        NSString *str = [[NSString alloc] initWithBytes:ws.data()
                                                 length:ws.size() * sizeof(wchar_t)
                                               encoding:NSUTF32LittleEndianStringEncoding];
        return str.UTF8String ?: string();
    }
}

wstring Gosu::widen(const string& s)
{
    return utf8ToWstring(s);
}

string Gosu::narrow(const std::wstring& ws)
{
    return wstringToUTF8(ws);
}
#endif

string Gosu::language()
{
    @autoreleasepool {
        // Cannot use accessor syntax here without breaking compilation with OS X 10.7/Xcode 4.6.3.
        NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
        return language.UTF8String ?: "en";
    }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gosu-0.10.9.pre1 src/UtilityApple.mm
gosu-0.10.8 src/UtilityApple.mm