Updated core
[LibreOffice.git] / sal / osl / unx / osxlocale.cxx
blob87b34c153b699264e6112b1d29e7c3049eb81112
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sal/types.h>
22 #include <assert.h>
24 #include <premac.h>
25 #ifndef IOS
26 #include <CoreServices/CoreServices.h>
27 #endif
28 #include <CoreFoundation/CoreFoundation.h>
29 #include <postmac.h>
31 namespace /* private */
33 template <typename T>
34 class CFGuard
36 public:
37 explicit CFGuard(T& rT) : rT_(rT) {}
38 ~CFGuard() { if (rT_) CFRelease(rT_); }
39 private:
40 T& rT_;
43 typedef CFGuard<CFArrayRef> CFArrayGuard;
44 typedef CFGuard<CFStringRef> CFStringGuard;
45 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
47 /** Get the current process locale from system
49 CFStringRef getProcessLocale()
51 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
52 CFPropertyListGuard proplGuard(pref);
54 if (pref == NULL) // return fallback value 'en_US'
55 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
57 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
59 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
61 } // namespace private
63 /** Grab current locale from system.
65 extern "C" {
66 int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
68 CFStringRef sref = getProcessLocale();
69 CFStringGuard sGuard(sref);
71 assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
73 // split the string into substrings; the first two (if there are two) substrings
74 // are language and country
75 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
76 CFArrayGuard arrGuard(subs);
78 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
79 CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
81 // country also available? Assumption: if the array contains more than one
82 // value the second value is always the country!
83 if (CFArrayGetCount(subs) > 1)
85 strlcat(locale, "_", bufferLen - strlen(locale));
87 CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
88 CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
90 // Append 'UTF-8' to the locale because the Mac OS X file
91 // system interface is UTF-8 based and sal tries to determine
92 // the file system locale from the locale information
93 strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
95 return noErr;
102 * macxp_OSXConvertCFEncodingToIANACharSetName
104 * Convert a CoreFoundation text encoding to an IANA charset name.
106 extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding )
108 CFStringRef sCFEncodingName;
110 sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding );
111 CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding );
113 if ( sCFEncodingName )
114 CFRelease( sCFEncodingName );
116 return( noErr );
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */