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
|
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE 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, or (at your option) any
later version.
SOPE 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 Lesser General Public
License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "_WOStringTable.h"
#include "common.h"
@implementation _WOStringTable
static NSStringEncoding stringFilesEncoding = NSUTF8StringEncoding;
- (id)initWithPath:(NSString *)_path {
if ((self = [super init])) {
self->path = [_path copyWithZone:[self zone]];
}
return self;
}
- (id)init {
return [self initWithPath:nil];
}
- (void)dealloc {
[self->path release];
[self->lastRead release];
[self->data release];
[super dealloc];
}
/* loading */
- (NSException *)_handlePropertyListParseException:(NSException *)_exception {
[self logWithFormat:@"could not load strings file file '%@': %@",
self->path, _exception];
self->data = nil;
return nil;
}
- (void)checkState {
NSString *tmp;
NSDictionary *plist;
NSData *sdata;
if (self->data != nil)
return;
#if 0
#if Cocoa_FOUNDATION_LIBRARY /* potentially effects OGo ;-) */
/*
For WO4.5 compatibility, we need first to try to open .strings file as a
dictionary
Dictionary must be either in UTF-8 or UTF-16 encoding.
If we don't do that, then a dict in UTF-8 encoding will be opened as a
dict using defaultCString encoding
*/
plist = [NSDictionary dictionaryWithContentsOfFile:self->path];
if (plist != nil) {
self->data = [plist copy];
return;
}
#endif
#endif
/* If file was not a dictionary, then it's a standard strings file */
if ((sdata = [[NSData alloc] initWithContentsOfFile:self->path]) == nil) {
[self errorWithFormat:@"could not read strings file: %@", self->path];
self->data = nil;
return;
}
tmp = [[NSString alloc] initWithData:sdata encoding:stringFilesEncoding];
[sdata release]; sdata = nil;
if (tmp == nil) {
[self errorWithFormat:@"file is not in required encoding (%d): %@",
stringFilesEncoding, self->path];
self->data = nil;
return;
}
NS_DURING {
if ((plist = [tmp propertyListFromStringsFileFormat]) == nil) {
[self errorWithFormat:@"%s: could not load strings file '%@'",
__PRETTY_FUNCTION__,
self->path];
}
[tmp release]; tmp = nil;
self->data = [plist copy];
}
NS_HANDLER {
[tmp release]; tmp = nil;
[[self _handlePropertyListParseException:localException] raise];
}
NS_ENDHANDLER;
}
/* access */
- (NSString *)stringForKey:(NSString *)_key withDefaultValue:(NSString *)_def {
NSString *value;
[self checkState];
value = [self->data objectForKey:_key];
return value != nil ? value : _def;
}
/* fake being a dictionary */
- (NSEnumerator *)keyEnumerator {
[self checkState];
return [self->data keyEnumerator];
}
- (NSEnumerator *)objectEnumerator {
[self checkState];
return [self->data objectEnumerator];
}
- (id)objectForKey:(id)_key {
[self checkState];
return [self->data objectForKey:_key];
}
- (unsigned int)count {
[self checkState];
return [self->data count];
}
- (NSArray *)allKeys {
[self checkState];
return [self->data allKeys];
}
- (NSArray *)allValues {
[self checkState];
return [self->data allValues];
}
/* KVC */
- (id)valueForKey:(NSString *)_key {
return [self objectForKey:_key];
}
/* description */
- (NSString *)description {
NSMutableString *ms;
ms = [NSMutableString stringWithCapacity:128];
[ms appendFormat:@"<0x%p[%@]: ", self, NSStringFromClass([self class])];
if (self->path) [ms appendFormat:@" path='%@'", self->path];
if (self->data) [ms appendFormat:@" strings=#%d", (int)[self->data count]];
if (self->lastRead) [ms appendFormat:@" loaddate=%@", self->lastRead];
[ms appendString:@">"];
return ms;
}
@end /* _WOStringTable */
|