Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i320 patch by Glenn Ganz, NSString_defaultEncoding_* functionality ad…
…ded, not complete patch yet git-svn-id: http://cocotron.googlecode.com/svn/trunk@612 43251c19-cd1f-0410-9ec0-95c558c233a8
- Loading branch information
cocotron
committed
Aug 24, 2009
1 parent
4b4997c
commit cb9f3c5
Showing
5 changed files
with
290 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* Copyright (c) 2009 Glenn Ganz | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
|
||
|
||
#import <Foundation/NSString.h> | ||
|
||
NSStringEncoding defaultEnconding(); | ||
|
85 changes: 85 additions & 0 deletions
85
Foundation/platform_darwin/NSString_defaultEncoding_darwin.m
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,85 @@ | ||
/* Copyright (c) 2009 Glenn Ganz | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
|
||
|
||
#import <Foundation/NSString_defaultEncoding.h> | ||
#import <Foundation/NSException.h> | ||
#import <pwd.h> | ||
#import <string.h> | ||
#import <stdio.h> | ||
|
||
NSStringEncoding defaultEnconding() | ||
{ | ||
//don't use objc calls because they call often defaultCStringEncoding | ||
|
||
static int defaultEncoding = -1; | ||
|
||
if (defaultEncoding == -1) { | ||
|
||
static struct passwd *pwent = NULL; | ||
char *filename; | ||
FILE *fhandle; | ||
|
||
if (pwent == NULL) { | ||
pwent = getpwuid(getuid()); | ||
} | ||
|
||
filename = strcat(pwent->pw_dir, "/.CFUserTextEncoding"); | ||
fhandle = fopen(filename, "r"); | ||
if(fhandle != NULL) | ||
{ | ||
int enc; | ||
fscanf (fhandle,"%X",&enc); | ||
fclose(fhandle); | ||
|
||
switch(enc) { | ||
case 0: | ||
defaultEncoding = NSMacOSRomanStringEncoding; | ||
break; | ||
case 0x0500: | ||
defaultEncoding = NSWindowsCP1252StringEncoding; | ||
break; | ||
case 0x0201: | ||
defaultEncoding = NSISOLatin1StringEncoding; | ||
break; | ||
case 0x0202: | ||
defaultEncoding = NSISOLatin2StringEncoding; | ||
break; | ||
case 0x0B01: | ||
defaultEncoding = NSNEXTSTEPStringEncoding; | ||
break; | ||
case 0x0600: | ||
defaultEncoding = NSASCIIStringEncoding; | ||
break; | ||
case 0x0100: | ||
defaultEncoding = NSUnicodeStringEncoding; | ||
break; | ||
case 0x08000100: | ||
defaultEncoding = NSUTF8StringEncoding; | ||
break; | ||
case 0x0BFF: | ||
defaultEncoding = NSNonLossyASCIIStringEncoding; | ||
break; | ||
case 0x0A01: | ||
defaultEncoding = NSShiftJISStringEncoding; | ||
break; | ||
case 0x0920: | ||
defaultEncoding = NSJapaneseEUCStringEncoding; | ||
break; | ||
default: | ||
defaultEncoding = NSMacOSRomanStringEncoding; | ||
} | ||
|
||
} | ||
if(defaultEncoding == -1) { | ||
defaultEncoding = NSMacOSRomanStringEncoding; | ||
} | ||
} | ||
|
||
return defaultEncoding; | ||
} |
89 changes: 89 additions & 0 deletions
89
Foundation/platform_posix/NSString_defaultEncoding_posix.m
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,89 @@ | ||
/* Copyright (c) 2009 Glenn Ganz | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
|
||
|
||
#import <Foundation/NSString_defaultEncoding.h> | ||
#import <Foundation/NSException.h> | ||
#import <string.h> | ||
#import <stdlib.h> | ||
#import <ctype.h> | ||
|
||
NSStringEncoding defaultEnconding() | ||
{ | ||
//don't use objc calls because they call often defaultCStringEncoding | ||
|
||
static int defaultEncoding = -1; | ||
|
||
if(defaultEncoding == -1) { | ||
|
||
char charset[20]; | ||
char *lang = getenv("LANG"); | ||
if (lang && *lang) { | ||
const char* firstpart = strtok (lang, "."); | ||
if (firstpart != NULL) { | ||
char* secondpart = strtok (NULL, "\0"); | ||
if (secondpart != NULL) { | ||
int i; | ||
|
||
//make all upper | ||
for(i = 0; i < strlen(secondpart);i++) | ||
{ | ||
secondpart[i] = toupper(secondpart[i]); | ||
} | ||
if(strcmp(secondpart, "UTF-8") == 0 || strcmp(secondpart, "UTF8") == 0) | ||
{ | ||
defaultEncoding = NSUTF8StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "ISO8859-1") == 0 || strcmp(secondpart, "ISO88591") == 0) | ||
{ | ||
defaultEncoding = NSISOLatin1StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "ISO8859-2") == 0 || strcmp(secondpart, "ISO88592") == 0) | ||
{ | ||
defaultEncoding = NSISOLatin2StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "EUCJP") == 0) | ||
{ | ||
defaultEncoding = NSJapaneseEUCStringEncoding; | ||
} | ||
else if(strcmp(secondpart, "CP1250") == 0) | ||
{ | ||
defaultEncoding = NSWindowsCP1250StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "CP1251") == 0) | ||
{ | ||
defaultEncoding = NSWindowsCP1251StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "CP1252") == 0) | ||
{ | ||
defaultEncoding = NSWindowsCP1252StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "CP1253") == 0) | ||
{ | ||
defaultEncoding = NSWindowsCP1253StringEncoding; | ||
} | ||
else if(strcmp(secondpart, "CP1254") == 0) | ||
{ | ||
defaultEncoding = NSWindowsCP1254StringEncoding; | ||
} | ||
|
||
//TODO: add more encodings | ||
} | ||
} | ||
} | ||
|
||
if(defaultEncoding == -1) | ||
{ | ||
//set the default to ASCII | ||
defaultEncoding = NSASCIIStringEncoding; | ||
} | ||
} | ||
|
||
return defaultEncoding; | ||
|
||
} |
Oops, something went wrong.