| 1 |
/* |
| 2 |
** CWCharset.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2004 Ludovic Marcotte |
| 5 |
** |
| 6 |
** Author: Ludovic Marcotte <[email protected]> |
| 7 |
** |
| 8 |
** This library is free software; you can redistribute it and/or |
| 9 |
** modify it under the terms of the GNU Lesser General Public |
| 10 |
** License as published by the Free Software Foundation; either |
| 11 |
** version 2.1 of the License, or (at your option) any later version. |
| 12 |
** |
| 13 |
** This library is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
** Lesser General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <Pantomime/CWCharset.h> |
| 23 |
|
| 24 |
#import <Pantomime/CWConstants.h> |
| 25 |
#import <Pantomime/CWISO8859_1.h> |
| 26 |
#import <Pantomime/CWISO8859_2.h> |
| 27 |
#import <Pantomime/CWISO8859_3.h> |
| 28 |
#import <Pantomime/CWISO8859_4.h> |
| 29 |
#import <Pantomime/CWISO8859_5.h> |
| 30 |
#import <Pantomime/CWISO8859_6.h> |
| 31 |
#import <Pantomime/CWISO8859_7.h> |
| 32 |
#import <Pantomime/CWISO8859_8.h> |
| 33 |
#import <Pantomime/CWISO8859_9.h> |
| 34 |
#import <Pantomime/CWISO8859_10.h> |
| 35 |
#import <Pantomime/CWISO8859_11.h> |
| 36 |
#import <Pantomime/CWISO8859_13.h> |
| 37 |
#import <Pantomime/CWISO8859_14.h> |
| 38 |
#import <Pantomime/CWISO8859_15.h> |
| 39 |
#import <Pantomime/CWKOI8_R.h> |
| 40 |
#import <Pantomime/CWKOI8_U.h> |
| 41 |
#import <Pantomime/CWWINDOWS_1250.h> |
| 42 |
#import <Pantomime/CWWINDOWS_1251.h> |
| 43 |
#import <Pantomime/CWWINDOWS_1252.h> |
| 44 |
#import <Pantomime/CWWINDOWS_1253.h> |
| 45 |
#import <Pantomime/CWWINDOWS_1254.h> |
| 46 |
|
| 47 |
#import <Foundation/NSBundle.h> |
| 48 |
#import <Foundation/NSDictionary.h> |
| 49 |
|
| 50 |
static NSMutableDictionary *charset_name_description = nil; |
| 51 |
static NSMutableDictionary *charset_instance_cache = nil; |
| 52 |
|
| 53 |
// |
| 54 |
// |
| 55 |
// |
| 56 |
@implementation CWCharset |
| 57 |
|
| 58 |
+ (void) initialize |
| 59 |
{ |
| 60 |
if (!charset_instance_cache) |
| 61 |
{ |
| 62 |
charset_instance_cache = [[NSMutableDictionary alloc] init]; |
| 63 |
} |
| 64 |
|
| 65 |
if (!charset_name_description) |
| 66 |
{ |
| 67 |
charset_name_description = [[NSMutableDictionary alloc] init]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
// |
| 73 |
// |
| 74 |
// |
| 75 |
- (id) initWithCodeCharTable: (const struct charset_code *) c |
| 76 |
length: (int) n |
| 77 |
{ |
| 78 |
self = [super init]; |
| 79 |
|
| 80 |
_codes = c; |
| 81 |
_num_codes = n; |
| 82 |
_identity_map = 0x20; |
| 83 |
|
| 84 |
if (n > 0 && _codes[0].code == 0x20) |
| 85 |
{ |
| 86 |
int i = 1; |
| 87 |
for (_identity_map=0x20; |
| 88 |
i < _num_codes && _codes[i].code == _identity_map + 1 && _codes[i].value == _identity_map + 1; |
| 89 |
_identity_map++,i++) ; |
| 90 |
} |
| 91 |
|
| 92 |
return self; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
// |
| 97 |
// TODO: what should this return for eg. \t and \n? |
| 98 |
// |
| 99 |
- (int) codeForCharacter: (unichar) theCharacter |
| 100 |
{ |
| 101 |
int i; |
| 102 |
|
| 103 |
if (theCharacter <= _identity_map) |
| 104 |
{ |
| 105 |
return theCharacter; |
| 106 |
} |
| 107 |
|
| 108 |
for (i = 0; i < _num_codes; i++) |
| 109 |
{ |
| 110 |
if (_codes[i].value == theCharacter) |
| 111 |
{ |
| 112 |
return _codes[i].code; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
// |
| 121 |
// |
| 122 |
// |
| 123 |
- (BOOL) characterIsInCharset: (unichar) theCharacter |
| 124 |
{ |
| 125 |
if (theCharacter <= _identity_map) |
| 126 |
{ |
| 127 |
return YES; |
| 128 |
} |
| 129 |
|
| 130 |
if ([self codeForCharacter: theCharacter] != -1) |
| 131 |
{ |
| 132 |
return YES; |
| 133 |
} |
| 134 |
|
| 135 |
return NO; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
// |
| 140 |
// Returns the name of the Charset. Like: |
| 141 |
// "iso-8859-1" |
| 142 |
// |
| 143 |
- (NSString *) name |
| 144 |
{ |
| 145 |
[self subclassResponsibility: _cmd]; |
| 146 |
return nil; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// |
| 151 |
// |
| 152 |
// |
| 153 |
+ (NSDictionary *) allCharsets |
| 154 |
{ |
| 155 |
if (![charset_name_description count]) |
| 156 |
{ |
| 157 |
[charset_name_description setObject: _(@"Western European (ISO Latin 1)") forKey: @"iso-8859-1"]; |
| 158 |
[charset_name_description setObject: _(@"Western European (ISO Latin 9)") forKey: @"iso-8859-15"]; |
| 159 |
[charset_name_description setObject: _(@"Western European (Windows Latin 1)") forKey: @"windows-1252"]; |
| 160 |
|
| 161 |
[charset_name_description setObject: _(@"Japanese (ISO 2022-JP)") forKey: @"iso-2022-jp"]; |
| 162 |
[charset_name_description setObject: _(@"Japanese (EUC-JP)") forKey: @"euc-jp"]; |
| 163 |
|
| 164 |
[charset_name_description setObject: _(@"Traditional Chinese (BIG5)") forKey: @"big5"]; |
| 165 |
|
| 166 |
[charset_name_description setObject: _(@"Arabic (ISO 8859-6)") forKey: @"iso-8859-6"]; |
| 167 |
|
| 168 |
[charset_name_description setObject: _(@"Greek (ISO 8859-7)") forKey: @"iso-8859-7"]; |
| 169 |
[charset_name_description setObject: _(@"Greek (Windows)") forKey: @"windows-1253"]; |
| 170 |
|
| 171 |
[charset_name_description setObject: _(@"Hebrew (ISO 8859-8)") forKey: @"iso-8859-8"]; |
| 172 |
|
| 173 |
[charset_name_description setObject: _(@"Cyrillic (ISO 8859-5)") forKey: @"iso-8859-5"]; |
| 174 |
[charset_name_description setObject: _(@"Cyrillic (KOI8-R)") forKey: @"koi8-r"]; |
| 175 |
[charset_name_description setObject: _(@"Cyrillic (Windows)") forKey: @"windows-1251"]; |
| 176 |
|
| 177 |
[charset_name_description setObject: _(@"Thai (ISO 8859-11)") forKey: @"iso-8859-11"]; |
| 178 |
|
| 179 |
[charset_name_description setObject: _(@"Central European (ISO Latin 2)") forKey: @"iso-8859-2"]; |
| 180 |
[charset_name_description setObject: _(@"Central European (Windows Latin 2)") forKey: @"windows-1250"]; |
| 181 |
|
| 182 |
[charset_name_description setObject: _(@"Turkish (Latin 5)") forKey: @"iso-8859-9"]; |
| 183 |
[charset_name_description setObject: _(@"Turkish (Windows)") forKey: @"windows-1254"]; |
| 184 |
|
| 185 |
[charset_name_description setObject: _(@"South European (ISO Latin 3)") forKey: @"iso-8859-3"]; |
| 186 |
[charset_name_description setObject: _(@"North European (ISO Latin 4)") forKey: @"iso-8859-4"]; |
| 187 |
|
| 188 |
[charset_name_description setObject: _(@"Nordic (ISO Latin 6)") forKey: @"iso-8859-10"]; |
| 189 |
[charset_name_description setObject: _(@"Baltic Rim (ISO Latin 7)") forKey: @"iso-8859-13"]; |
| 190 |
[charset_name_description setObject: _(@"Celtic (ISO Latin 8)") forKey: @"iso-8859-14"]; |
| 191 |
|
| 192 |
[charset_name_description setObject: _(@"Simplified Chinese (GB2312)") forKey: @"gb2312"]; |
| 193 |
[charset_name_description setObject: _(@"UTF-8") forKey: @"utf-8"]; |
| 194 |
|
| 195 |
#ifdef MACOSX |
| 196 |
[charset_name_description setObject: _(@"Korean (EUC-KR/KS C 5601)") forKey: @"euc-kr"]; |
| 197 |
[charset_name_description setObject: _(@"Japanese (Win/Mac)") forKey: @"shift_jis"]; |
| 198 |
#endif |
| 199 |
} |
| 200 |
|
| 201 |
return charset_name_description; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
// |
| 206 |
// This method is used to obtain a charset from the name |
| 207 |
// of this charset. It caches this charset for future |
| 208 |
// usage when it's found. |
| 209 |
// |
| 210 |
+ (CWCharset *) charsetForName: (NSString *) theName |
| 211 |
{ |
| 212 |
CWCharset *theCharset; |
| 213 |
|
| 214 |
theCharset = [charset_instance_cache objectForKey: [theName lowercaseString]]; |
| 215 |
|
| 216 |
if (!theCharset) |
| 217 |
{ |
| 218 |
CWCharset *aCharset; |
| 219 |
|
| 220 |
if ([[theName lowercaseString] isEqualToString: @"iso-8859-2"]) |
| 221 |
{ |
| 222 |
aCharset = (CWCharset *)[[CWISO8859_2 alloc] init]; |
| 223 |
} |
| 224 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-3"]) |
| 225 |
{ |
| 226 |
aCharset = (CWCharset *)[[CWISO8859_3 alloc] init]; |
| 227 |
} |
| 228 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-4"]) |
| 229 |
{ |
| 230 |
aCharset = (CWCharset *)[[CWISO8859_4 alloc] init]; |
| 231 |
} |
| 232 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-5"]) |
| 233 |
{ |
| 234 |
aCharset = (CWCharset *)[[CWISO8859_5 alloc] init]; |
| 235 |
} |
| 236 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-6"]) |
| 237 |
{ |
| 238 |
aCharset = (CWCharset *)[[CWISO8859_6 alloc] init]; |
| 239 |
} |
| 240 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-7"]) |
| 241 |
{ |
| 242 |
aCharset = (CWCharset *)[[CWISO8859_7 alloc] init]; |
| 243 |
} |
| 244 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-8"]) |
| 245 |
{ |
| 246 |
aCharset = (CWCharset *)[[CWISO8859_8 alloc] init]; |
| 247 |
} |
| 248 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-9"]) |
| 249 |
{ |
| 250 |
aCharset = (CWCharset *)[[CWISO8859_9 alloc] init]; |
| 251 |
} |
| 252 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-10"]) |
| 253 |
{ |
| 254 |
aCharset = (CWCharset *)[[CWISO8859_10 alloc] init]; |
| 255 |
} |
| 256 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-11"]) |
| 257 |
{ |
| 258 |
aCharset = (CWCharset *)[[CWISO8859_11 alloc] init]; |
| 259 |
} |
| 260 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-13"]) |
| 261 |
{ |
| 262 |
aCharset = (CWCharset *)[[CWISO8859_13 alloc] init]; |
| 263 |
} |
| 264 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-14"]) |
| 265 |
{ |
| 266 |
aCharset = (CWCharset *)[[CWISO8859_14 alloc] init]; |
| 267 |
} |
| 268 |
else if ([[theName lowercaseString] isEqualToString: @"iso-8859-15"]) |
| 269 |
{ |
| 270 |
aCharset = (CWCharset *)[[CWISO8859_15 alloc] init]; |
| 271 |
} |
| 272 |
else if ([[theName lowercaseString] isEqualToString: @"koi8-r"]) |
| 273 |
{ |
| 274 |
aCharset = (CWCharset *)[[CWKOI8_R alloc] init]; |
| 275 |
} |
| 276 |
else if ([[theName lowercaseString] isEqualToString: @"koi8-u"]) |
| 277 |
{ |
| 278 |
aCharset = (CWCharset *)[[CWKOI8_U alloc] init]; |
| 279 |
} |
| 280 |
else if ([[theName lowercaseString] isEqualToString: @"windows-1250"]) |
| 281 |
{ |
| 282 |
aCharset = (CWCharset *)[[CWWINDOWS_1250 alloc] init]; |
| 283 |
} |
| 284 |
else if ([[theName lowercaseString] isEqualToString: @"windows-1251"]) |
| 285 |
{ |
| 286 |
aCharset = (CWCharset *)[[CWWINDOWS_1251 alloc] init]; |
| 287 |
} |
| 288 |
else if ([[theName lowercaseString] isEqualToString: @"windows-1252"]) |
| 289 |
{ |
| 290 |
aCharset = (CWCharset *)[[CWWINDOWS_1252 alloc] init]; |
| 291 |
} |
| 292 |
else if ([[theName lowercaseString] isEqualToString: @"windows-1253"]) |
| 293 |
{ |
| 294 |
aCharset = (CWCharset *)[[CWWINDOWS_1253 alloc] init]; |
| 295 |
} |
| 296 |
else if ([[theName lowercaseString] isEqualToString: @"windows-1254"]) |
| 297 |
{ |
| 298 |
aCharset = (CWCharset *)[[CWWINDOWS_1254 alloc] init]; |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
aCharset = (CWCharset *)[[CWISO8859_1 alloc] init]; |
| 303 |
} |
| 304 |
|
| 305 |
[charset_instance_cache setObject: aCharset |
| 306 |
forKey: [theName lowercaseString]]; |
| 307 |
RELEASE(aCharset); |
| 308 |
|
| 309 |
return aCharset; |
| 310 |
} |
| 311 |
|
| 312 |
return theCharset; |
| 313 |
} |
| 314 |
|
| 315 |
@end |