Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / platform_darwin / NSString_defaultEncoding_darwin.m
blobca57169c45d83c2f0f8be471f80e5816969ce74f
1 /* Copyright (c) 2009 Glenn Ganz
2  
3  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:
4  
5  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6  
7  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. */
10 #import <Foundation/NSString_defaultEncoding.h>
11 #import <Foundation/NSException.h>
12 #include <pwd.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <unistd.h>
17 NSStringEncoding defaultEncoding()
19     //don't use objc calls because they call often defaultCStringEncoding 
21     static int defaultEncoding = -1;
22     
23         if (defaultEncoding == -1) {
24         static struct passwd    *pwent = NULL;
25         char                    filename[255] = {0};
26         FILE                    *fhandle;
27         if (pwent == NULL) {
28             pwent = getpwuid(getuid());
29         }
30         strcpy(filename, pwent->pw_dir);
31         strcat(filename, "/.CFUserTextEncoding");
33                 fhandle = fopen(filename, "r");
34                 if(fhandle != NULL)
35         {
36             int enc;
37             fscanf (fhandle,"%X",&enc);
38             fclose(fhandle);
39             
40                         switch(enc) {
41                                 case 0:
42                                         defaultEncoding = NSMacOSRomanStringEncoding;
43                                         break;
44                                 case 0x0500:
45                                         defaultEncoding = NSWindowsCP1252StringEncoding;
46                                         break;
47                                 case 0x0201:
48                                         defaultEncoding = NSISOLatin1StringEncoding;
49                                         break;
50                                 case 0x0202:
51                                         defaultEncoding = NSISOLatin2StringEncoding;
52 // FIXME: use until the right encoding is implemented
53 defaultEncoding = NSISOLatin1StringEncoding;
54                                         break;
55                                 case 0x0B01:
56                                         defaultEncoding = NSNEXTSTEPStringEncoding;
57                                         break;
58                                 case 0x0600:
59                                         defaultEncoding = NSASCIIStringEncoding;
60                                         break;
61                                 case 0x0100:
62                                         defaultEncoding = NSUnicodeStringEncoding;
63 // FIXME: use until the right encoding is implemented
64 defaultEncoding = NSISOLatin1StringEncoding;
65                                         break;
66                                 case 0x08000100:
67                                         defaultEncoding = NSUTF8StringEncoding;
68 // FIXME: use until the right encoding is implemented
69 defaultEncoding = NSISOLatin1StringEncoding;
70                                         break;  
71                                 case 0x0BFF:
72                                         defaultEncoding = NSNonLossyASCIIStringEncoding;
73 // FIXME: use until the right encoding is implemented
74 defaultEncoding = NSISOLatin1StringEncoding;
75                                         break;  
76                                 case 0x0A01:    
77                                         defaultEncoding = NSShiftJISStringEncoding;
78 // FIXME: use until the right encoding is implemented
79 defaultEncoding = NSISOLatin1StringEncoding;
80                                         break;
81                                 case 0x0920:
82                                         defaultEncoding = NSJapaneseEUCStringEncoding;
83 // FIXME: use until the right encoding is implemented
84 defaultEncoding = NSISOLatin1StringEncoding;
85                                         break;                                  
86                                 default:
87                                         defaultEncoding = NSMacOSRomanStringEncoding;                                   
88                    break;
89             }
90                 
91         }
92         if(defaultEncoding == -1) {
93                         defaultEncoding = NSMacOSRomanStringEncoding;
94                 }
95         }
96         
97         return defaultEncoding;