MuskLocaleUtil.m 696 B

12345678910111213141516171819202122232425
  1. #include "MuskLocaleUtil.h"
  2. #import <Foundation/Foundation.h>
  3. char* MuskGetLocaleRegion() {
  4. NSLocale *locale = [NSLocale currentLocale];
  5. if (locale == NULL) {
  6. return NULL;
  7. }
  8. NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
  9. if (countryCode == NULL) {
  10. return NULL;
  11. }
  12. /* .NET string marshalling takes ownership of the string, so copy it. */
  13. unsigned long length =
  14. [countryCode lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  15. if (length <= 0) {
  16. return NULL;
  17. }
  18. char* countryCodeCopy = (char*) malloc(length + 1);
  19. strncpy(countryCodeCopy, [countryCode UTF8String], length);
  20. countryCodeCopy[length] = '\0';
  21. return countryCodeCopy;
  22. }