File: CWCharset.m

package info (click to toggle)
pantomime 1.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: objc: 22,039; makefile: 11; sh: 4
file content (315 lines) | stat: -rw-r--r-- 9,662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
**  CWCharset.m
**
**  Copyright (c) 2001-2004 Ludovic Marcotte
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Pantomime/CWCharset.h>

#import <Pantomime/CWConstants.h>
#import <Pantomime/CWISO8859_1.h>
#import <Pantomime/CWISO8859_2.h>
#import <Pantomime/CWISO8859_3.h>
#import <Pantomime/CWISO8859_4.h>
#import <Pantomime/CWISO8859_5.h>
#import <Pantomime/CWISO8859_6.h>
#import <Pantomime/CWISO8859_7.h>
#import <Pantomime/CWISO8859_8.h>
#import <Pantomime/CWISO8859_9.h>
#import <Pantomime/CWISO8859_10.h>
#import <Pantomime/CWISO8859_11.h>
#import <Pantomime/CWISO8859_13.h>
#import <Pantomime/CWISO8859_14.h>
#import <Pantomime/CWISO8859_15.h>
#import <Pantomime/CWKOI8_R.h>
#import <Pantomime/CWKOI8_U.h>
#import <Pantomime/CWWINDOWS_1250.h>
#import <Pantomime/CWWINDOWS_1251.h>
#import <Pantomime/CWWINDOWS_1252.h>
#import <Pantomime/CWWINDOWS_1253.h>
#import <Pantomime/CWWINDOWS_1254.h>

#import <Foundation/NSBundle.h>
#import <Foundation/NSDictionary.h>

static NSMutableDictionary *charset_name_description = nil;
static NSMutableDictionary *charset_instance_cache = nil;

//
//
//
@implementation CWCharset

+ (void) initialize
{
  if (!charset_instance_cache)
    {
      charset_instance_cache = [[NSMutableDictionary alloc] init];
    }

  if (!charset_name_description)
    {
      charset_name_description = [[NSMutableDictionary alloc] init];
    }
} 


//
//
//
- (id) initWithCodeCharTable: (const struct charset_code *) c
		      length: (int) n
{
  self = [super init];
  
  _codes = c;
  _num_codes = n; 
  _identity_map = 0x20;
  
  if (n > 0 && _codes[0].code == 0x20)
    {
      int i = 1;
      for (_identity_map=0x20;
	   i < _num_codes && _codes[i].code == _identity_map + 1 && _codes[i].value == _identity_map + 1;
	   _identity_map++,i++) ;
    }

  return self;
}


//
// TODO: what should this return for eg. \t and \n?
//
- (int) codeForCharacter: (unichar) theCharacter
{
  int i;

  if (theCharacter <= _identity_map)
    {
      return theCharacter;
    }
  
  for (i = 0; i < _num_codes; i++)
    {
      if (_codes[i].value == theCharacter)
	{
	  return _codes[i].code;
	}
    }
  
  return -1;
}


//
//
//
- (BOOL) characterIsInCharset: (unichar) theCharacter
{
  if (theCharacter <= _identity_map)
    {
      return YES;
    }

  if ([self codeForCharacter: theCharacter] != -1)
    {
      return YES;
    }
  
  return NO;
}


//
// Returns the name of the Charset. Like:
// "iso-8859-1"
// 
- (NSString *) name
{
  [self subclassResponsibility: _cmd];
  return nil;
}


//
//
//
+ (NSDictionary *) allCharsets
{
  if (![charset_name_description count])
    {
      [charset_name_description setObject: _(@"Western European (ISO Latin 1)")     forKey: @"iso-8859-1"];
      [charset_name_description setObject: _(@"Western European (ISO Latin 9)")     forKey: @"iso-8859-15"];
      [charset_name_description setObject: _(@"Western European (Windows Latin 1)") forKey: @"windows-1252"];

      [charset_name_description setObject: _(@"Japanese (ISO 2022-JP)")             forKey: @"iso-2022-jp"];
      [charset_name_description setObject: _(@"Japanese (EUC-JP)")                  forKey: @"euc-jp"];

      [charset_name_description setObject: _(@"Traditional Chinese (BIG5)")         forKey: @"big5"];
  
      [charset_name_description setObject: _(@"Arabic (ISO 8859-6)")                forKey: @"iso-8859-6"];
  
      [charset_name_description setObject: _(@"Greek (ISO 8859-7)")                 forKey: @"iso-8859-7"];
      [charset_name_description setObject: _(@"Greek (Windows)")                    forKey: @"windows-1253"];

      [charset_name_description setObject: _(@"Hebrew (ISO 8859-8)")                forKey: @"iso-8859-8"];
  
      [charset_name_description setObject: _(@"Cyrillic (ISO 8859-5)")              forKey: @"iso-8859-5"];
      [charset_name_description setObject: _(@"Cyrillic (KOI8-R)")                  forKey: @"koi8-r"];
      [charset_name_description setObject: _(@"Cyrillic (Windows)")                 forKey: @"windows-1251"];

      [charset_name_description setObject: _(@"Thai (ISO 8859-11)")                 forKey: @"iso-8859-11"];

      [charset_name_description setObject: _(@"Central European (ISO Latin 2)")     forKey: @"iso-8859-2"];
      [charset_name_description setObject: _(@"Central European (Windows Latin 2)") forKey: @"windows-1250"];
  
      [charset_name_description setObject: _(@"Turkish (Latin 5)")                  forKey: @"iso-8859-9"];
      [charset_name_description setObject: _(@"Turkish (Windows)")                  forKey: @"windows-1254"];
  
      [charset_name_description setObject: _(@"South European (ISO Latin 3)")       forKey: @"iso-8859-3"];
      [charset_name_description setObject: _(@"North European (ISO Latin 4)")       forKey: @"iso-8859-4"];
 
      [charset_name_description setObject: _(@"Nordic (ISO Latin 6)")               forKey: @"iso-8859-10"];
      [charset_name_description setObject: _(@"Baltic Rim (ISO Latin 7)")           forKey: @"iso-8859-13"];
      [charset_name_description setObject: _(@"Celtic (ISO Latin 8)")               forKey: @"iso-8859-14"];

      [charset_name_description setObject: _(@"Simplified Chinese (GB2312)")        forKey: @"gb2312"];
      [charset_name_description setObject: _(@"UTF-8")                              forKey: @"utf-8"];

#ifdef MACOSX
      [charset_name_description setObject: _(@"Korean (EUC-KR/KS C 5601)")          forKey: @"euc-kr"];
      [charset_name_description setObject: _(@"Japanese (Win/Mac)")                 forKey: @"shift_jis"];
#endif
    }

  return charset_name_description;
}


//
// This method is used to obtain a charset from the name
// of this charset. It caches this charset for future
// usage when it's found.
//
+ (CWCharset *) charsetForName: (NSString *) theName
{
  CWCharset *theCharset;

  theCharset = [charset_instance_cache objectForKey: [theName lowercaseString]];

  if (!theCharset)
    {
      CWCharset *aCharset;
      
      if ([[theName lowercaseString] isEqualToString: @"iso-8859-2"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_2 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-3"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_3 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-4"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_4 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-5"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_5 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-6"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_6 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-7"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_7 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-8"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_8 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-9"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_9 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-10"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_10 alloc] init];
 	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-11"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_11 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-13"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_13 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-14"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_14 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"iso-8859-15"])
	{
	  aCharset = (CWCharset *)[[CWISO8859_15 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"koi8-r"])
	{
	  aCharset = (CWCharset *)[[CWKOI8_R alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"koi8-u"])
	{
	  aCharset = (CWCharset *)[[CWKOI8_U alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"windows-1250"])
	{
	  aCharset = (CWCharset *)[[CWWINDOWS_1250 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"windows-1251"])
	{
	  aCharset = (CWCharset *)[[CWWINDOWS_1251 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"windows-1252"])
	{
	  aCharset = (CWCharset *)[[CWWINDOWS_1252 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"windows-1253"])
	{
	  aCharset = (CWCharset *)[[CWWINDOWS_1253 alloc] init];
	}
      else if ([[theName lowercaseString] isEqualToString: @"windows-1254"])
	{
	  aCharset = (CWCharset *)[[CWWINDOWS_1254 alloc] init];
	}
      else
	{
	  aCharset = (CWCharset *)[[CWISO8859_1 alloc] init];
	}
      
      [charset_instance_cache setObject: aCharset
			      forKey: [theName lowercaseString]];
      RELEASE(aCharset);

      return aCharset;
    }

  return theCharset;
}

@end