Oolite
Loading...
Searching...
No Matches
OOEncodingConverter.m
Go to the documentation of this file.
1/*
2
3OOEncodingConverter.m
4
5Copyright (C) 2008-2013 Jens Ayton and contributors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#ifndef OOENCODINGCONVERTER_EXCLUDE
28
30#import "OOCache.h"
32#import "OOLogging.h"
33
34/* Using compatibility mapping - converting strings to Unicode form KC - would
35 reduce potential complications in localizing Oolite. However, the method to
36 perform the transformation is not available in GNUstep. I'm currently not
37 using it under OS X either, for cross-platform consistency.
38 -- Ahruman 2008-01-27
39*/
40#if OOLITE_MAC_OS_X
41#define USE_COMPATIBILITY_MAPPING 0
42#else
43#define USE_COMPATIBILITY_MAPPING 0
44#endif
45
46#define PROFILE_ENCODING_CONVERTER 0
47
48static const NSUInteger kCachePruneThreshold = 200;
49
50#if PROFILE_ENCODING_CONVERTER
51static OOEncodingConverter* sProfiledConverter = nil;
52static NSTimer* sProfileTimer = nil;
53
54static unsigned sCacheHits = 0;
55static unsigned sCacheMisses = 0;
56#endif
57
58@interface OOEncodingConverter (Private)
59
60- (NSData*)performConversionForString:(NSString*)string;
61
62@end
63
64@implementation OOEncodingConverter
65
66- (id)initWithEncoding:(NSStringEncoding)encoding substitutions:(NSDictionary*)substitutions
67{
68 self = [super init];
69 if (self != nil) {
70 _cache = [[OOCache alloc] init];
71 [_cache setPruneThreshold:kCachePruneThreshold];
72 [_cache setName:@"Text encoding"];
75
76#if PROFILE_ENCODING_CONVERTER
77 if (sProfiledConverter == nil) {
78 sProfiledConverter = self;
79 sProfileTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(profileFire:) userInfo:nil repeats:YES];
80 }
81#endif
82 }
83
84 return self;
85}
86
87- (id)initWithFontPList:(NSDictionary*)fontPList
88{
89 return [self initWithEncoding:EncodingFromString([fontPList oo_stringForKey:@"encoding"]) substitutions:[fontPList oo_dictionaryForKey:@"substitutions"]];
90}
91
92- (void)dealloc
93{
94 [_cache release];
95 [_substitutions release];
96
97#if PROFILE_ENCODING_CONVERTER
98 sProfiledConverter = nil;
99 [sProfileTimer invalidate];
100 sProfileTimer = nil;
101 sCacheHits = 0;
102 sCacheMisses = 0;
103#endif
104
105 [super dealloc];
106}
107
109{
110 return [NSString stringWithFormat:@"encoding: %u", _encoding];
111}
112
113- (NSData*)convertString:(NSString*)string
114{
115 NSData* data = nil;
116
117#if USE_COMPATIBILITY_MAPPING
118 // Convert to Unicode Normalization Form KC (that is, minimize the use of combining modifiers while avoiding precomposed ligatures)
119 string = [string precomposedStringWithCompatibilityMapping];
120#endif
121
122 if (string == nil)
123 return [NSData data];
124
125 data = [_cache objectForKey:string];
126 if (data == nil) {
127 data = [self performConversionForString:string];
128 if (data != nil)
129 [_cache setObject:data forKey:string];
130
131#if PROFILE_ENCODING_CONVERTER
132 ++sCacheMisses;
133 } else {
134 ++sCacheHits;
135#endif
136 }
137
138 return data;
139}
140
141- (NSStringEncoding)encoding
142{
143 return _encoding;
144}
145
146@end
147
148@implementation OOEncodingConverter (Private)
149
150- (NSData*)performConversionForString:(NSString*)string
151{
152 NSString* subst = nil;
153 NSMutableString* mutable = nil;
154
155 mutable = [[string mutableCopy] autorelease];
156 if (mutable == nil)
157 return nil;
158
159 foreachkey(subst, _substitutions)
160 {
161 [mutable replaceOccurrencesOfString:subst
162 withString:[_substitutions objectForKey:subst]
163 options:0
164 range:NSMakeRange(0, [mutable length])];
165 }
166
167 return [mutable dataUsingEncoding:_encoding allowLossyConversion:YES];
168}
169
170#if PROFILE_ENCODING_CONVERTER
171/*
172 Profiling observations:
173 * The clock generates one new string per second.
174 * The trade screens each use over 100 strings, so cache sizes below 150
175 are undesireable.
176 * Cache hit ratio is extremely near 100% at most times.
177*/
178- (void)profileFire:(id)junk
179{
180 float ratio = (float)sCacheHits / (float)(sCacheHits + sCacheMisses);
181 OOLog(@"strings.encoding.profile", @"Cache hits: %u, misses: %u, ratio: %.2g", sCacheHits, sCacheMisses, ratio);
182 sCacheHits = sCacheMisses = 0;
183}
184#endif
185
186@end
187
188#endif // OOENCODINGCONVERTER_EXCLUDE
189
190/*
191 There are a variety of overlapping naming schemes for text encoding.
192 We ignore them and use a fixed list:
193 "windows-latin-1" NSWindowsCP1252StringEncoding
194 "windows-latin-2" NSWindowsCP1250StringEncoding
195 "windows-cyrillic" NSWindowsCP1251StringEncoding
196 "windows-greek" NSWindowsCP1253StringEncoding
197 "windows-turkish" NSWindowsCP1254StringEncoding
198*/
199
200#define kWindowsLatin1Str @"windows-latin-1"
201#define kWindowsLatin2Str @"windows-latin-2"
202#define kWindowsCyrillicStr @"windows-cyrillic"
203#define kWindowsGreekStr @"windows-greek"
204#define kWindowsTurkishStr @"windows-turkish"
205
206NSString* StringFromEncoding(NSStringEncoding encoding)
207{
208 switch (encoding) {
209 case NSWindowsCP1252StringEncoding:
210 return kWindowsLatin1Str;
211
212 case NSWindowsCP1250StringEncoding:
213 return kWindowsLatin2Str;
214
215 case NSWindowsCP1251StringEncoding:
216 return kWindowsCyrillicStr;
217
218 case NSWindowsCP1253StringEncoding:
219 return kWindowsGreekStr;
220
221 case NSWindowsCP1254StringEncoding:
222 return kWindowsTurkishStr;
223
224 default:
225 return nil;
226 }
227}
228
229NSStringEncoding EncodingFromString(NSString* name)
230{
231 if ([name isEqualToString:kWindowsLatin1Str])
232 return NSWindowsCP1252StringEncoding;
233 if ([name isEqualToString:kWindowsLatin2Str])
234 return NSWindowsCP1250StringEncoding;
235 if ([name isEqualToString:kWindowsCyrillicStr])
236 return NSWindowsCP1251StringEncoding;
237 if ([name isEqualToString:kWindowsGreekStr])
238 return NSWindowsCP1253StringEncoding;
239 if ([name isEqualToString:kWindowsTurkishStr])
240 return NSWindowsCP1254StringEncoding;
241 return (NSStringEncoding)NSNotFound;
242}
#define kWindowsCyrillicStr
static const NSUInteger kCachePruneThreshold
#define kWindowsLatin1Str
NSStringEncoding EncodingFromString(NSString *name)
#define kWindowsLatin2Str
#define kWindowsGreekStr
NSString * StringFromEncoding(NSStringEncoding encoding)
#define kWindowsTurkishStr
#define foreachkey(VAR, DICT)
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
id objectForKey:(id key)
Definition OOCache.m:242
void setPruneThreshold:(unsigned threshold)
Definition OOCache.m:279
void setName:(NSString *name)
Definition OOCache.m:350
void setObject:forKey:(id value,[forKey] id key)
Definition OOCache.m:256
NSDictionary * _substitutions
NSData * performConversionForString:(NSString *string)
id initWithEncoding:substitutions:(NSStringEncoding encoding,[substitutions] NSDictionary *substitutions)
NSStringEncoding encoding()
NSStringEncoding _encoding