Permalink
Browse files

update preference, made all singleton thread safe

thread safety implemented using @synchronized, have to update it later. @synchronized is the slowest and easiest :P

Signed-off-by: Rifat Nabi <to.rifat@gmail.com>
1 parent 6867325 commit 7097eadfcb6de15f49573579ed979f9207fa56d7 @torifat committed Jul 7, 2012
View
@@ -8,15 +8,15 @@
#import <Foundation/Foundation.h>
@interface AutoCorrect : NSObject {
- NSMutableArray* autoCorrectEntries;
+ NSMutableArray* _autoCorrectEntries;
}
@property (retain) NSMutableArray* autoCorrectEntries;
-+ (void)allocateSharedInstance;
-+ (void)deallocateSharedInstance;
+ (AutoCorrect *)sharedInstance;
- (NSString*)find:(NSString*)term;
+- (NSMutableArray*)autoCorrectEntries;
+- (void)setAutoCorrectEntries:(NSMutableArray *)autoCorrectEntries;
@end
View
@@ -7,78 +7,110 @@
#import "AutoCorrect.h"
#import "AvroParser.h"
+
+static AutoCorrect* sharedInstance = nil;
+
@implementation AutoCorrect
-@synthesize autoCorrectEntries;
+@synthesize autoCorrectEntries = _autoCorrectEntries;
-- (id)init {
-
- self = [super init];
-
- if (self) {
- // Open the file
- NSString *fileName = [[NSBundle mainBundle] pathForResource:@"autodict" ofType:@"dct"];
- const char *fn = [fileName UTF8String];
- FILE *file = fopen(fn, "r");
-
- // Read from the file
- char replaceBuffer[512], withBuffer[512];
- autoCorrectEntries = [[NSMutableArray alloc] init];
- while(fscanf(file, "%s %[^\n]\n", replaceBuffer, withBuffer) == 2) {
- NSString* replace = [NSString stringWithFormat:@"%s", replaceBuffer];
- NSString* with = [NSString stringWithFormat:@"%s", withBuffer];
-
- if ([replace isEqualToString:with] == NO) {
- with = [[AvroParser sharedInstance] parse:with];
- }
-
- NSMutableDictionary* item = [[NSMutableDictionary alloc] initWithObjectsAndKeys:replace, @"replace", with, @"with", nil];
- [autoCorrectEntries addObject:item];
- [item release];
++ (AutoCorrect *)sharedInstance {
+ @synchronized (self) {
+ if (sharedInstance == nil) {
+ [[self alloc] init]; // assignment not done here, see allocWithZone
+ }
+ }
+ return sharedInstance;
+}
+
++ (id)allocWithZone:(NSZone *)zone {
+ @synchronized(self) {
+ if (sharedInstance == nil) {
+ sharedInstance = [super allocWithZone:zone];
+ return sharedInstance; // assignment and return on first allocation
}
- fclose(file);
}
-
- return self;
+
+ return nil; //on subsequent allocation attempts return nil
}
-- (void)dealloc {
- [autoCorrectEntries release];
- [super dealloc];
+- (id)copyWithZone:(NSZone *)zone {
+ return self;
}
-static AutoCorrect* sharedInstance = nil;
+- (id)retain {
+ return self;
+}
-+ (void)allocateSharedInstance {
- sharedInstance = [[self alloc] init];
+- (oneway void)release {
+ //do nothing
}
-+ (void)deallocateSharedInstance {
- [sharedInstance release];
+- (id)autorelease {
+ return self;
}
-+ (AutoCorrect *)sharedInstance {
- return sharedInstance;
+- (NSUInteger)retainCount {
+ return NSUIntegerMax; // This is sooo not zero
+}
+
+- (id)init {
+ @synchronized(self) {
+ self = [super init];
+ if (self) {
+ // Open the file
+ NSString *fileName = [[NSBundle mainBundle] pathForResource:@"autodict" ofType:@"dct"];
+ const char *fn = [fileName UTF8String];
+ FILE *file = fopen(fn, "r");
+
+ // Read from the file
+ char replaceBuffer[512], withBuffer[512];
+ _autoCorrectEntries = [[NSMutableArray alloc] init];
+ while(fscanf(file, "%s %[^\n]\n", replaceBuffer, withBuffer) == 2) {
+ NSString* replace = [NSString stringWithFormat:@"%s", replaceBuffer];
+ NSString* with = [NSString stringWithFormat:@"%s", withBuffer];
+
+ if ([replace isEqualToString:with] == NO) {
+ with = [[AvroParser sharedInstance] parse:with];
+ }
+
+ NSMutableDictionary* item = [[NSMutableDictionary alloc] initWithObjectsAndKeys:replace, @"replace", with, @"with", nil];
+ [_autoCorrectEntries addObject:item];
+ [item release];
+ }
+ fclose(file);
+ }
+ return self;
+ }
+}
+
+- (void)dealloc {
+ @synchronized(self) {
+ [_autoCorrectEntries release];
+ [super dealloc];
+ }
}
// Instance Methods
- (NSString*)find:(NSString*)term {
- term = [[AvroParser sharedInstance] fix:term];
- // Binary Search
- int left = 0, right = [autoCorrectEntries count] -1, mid;
- while (right >= left) {
- mid = (left + right) / 2;
- NSDictionary* item = [autoCorrectEntries objectAtIndex:mid];
- NSComparisonResult comp = [term compare:[item objectForKey:@"replace"]];
- if (comp == NSOrderedDescending) {
- left = mid + 1;
- } else if (comp == NSOrderedAscending) {
- right = mid - 1;
- } else {
- return [item objectForKey:@"with"];
+ @synchronized(self) {
+ term = [[AvroParser sharedInstance] fix:term];
+ // Binary Search
+ int left = 0, right = [_autoCorrectEntries count] -1, mid;
+ while (right >= left) {
+ mid = (left + right) / 2;
+ NSDictionary* item = [_autoCorrectEntries objectAtIndex:mid];
+ NSComparisonResult comp = [term compare:[item objectForKey:@"replace"]];
+ if (comp == NSOrderedDescending) {
+ left = mid + 1;
+ } else if (comp == NSOrderedAscending) {
+ right = mid - 1;
+ } else {
+ return [item objectForKey:@"with"];
+ }
}
+ return nil;
}
- return nil;
}
@end
@@ -24,6 +24,7 @@
356B186D15A4499B00091215 /* Credits.png in Resources */ = {isa = PBXBuildFile; fileRef = 356B186B15A4499B00091215 /* Credits.png */; };
356B186E15A4499B00091215 /* General.png in Resources */ = {isa = PBXBuildFile; fileRef = 356B186C15A4499B00091215 /* General.png */; };
3578E8FD15A26EC100C1FB8E /* Credits.rtfd in Resources */ = {isa = PBXBuildFile; fileRef = 3578E8FC15A26EC100C1FB8E /* Credits.rtfd */; };
+ 3587CCD815A7C5E50074BFF1 /* preferences.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3587CCD715A7C5E50074BFF1 /* preferences.plist */; };
35895BF01597639A00F2A3ED /* AutoCorrect.m in Sources */ = {isa = PBXBuildFile; fileRef = 35895BEF1597639A00F2A3ED /* AutoCorrect.m */; };
35895BF3159763A300F2A3ED /* autodict.dct in Resources */ = {isa = PBXBuildFile; fileRef = 35895BF2159763A300F2A3ED /* autodict.dct */; };
35895BFE1597706800F2A3ED /* MainMenu.nib in Resources */ = {isa = PBXBuildFile; fileRef = 35895BFA1597706800F2A3ED /* MainMenu.nib */; };
@@ -73,6 +74,7 @@
356B186B15A4499B00091215 /* Credits.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Credits.png; path = Icons/Credits.png; sourceTree = "<group>"; };
356B186C15A4499B00091215 /* General.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = General.png; path = Icons/General.png; sourceTree = "<group>"; };
3578E8FC15A26EC100C1FB8E /* Credits.rtfd */ = {isa = PBXFileReference; lastKnownFileType = wrapper.rtfd; path = Credits.rtfd; sourceTree = "<group>"; };
+ 3587CCD715A7C5E50074BFF1 /* preferences.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = preferences.plist; sourceTree = "<group>"; };
35895BEE1597639A00F2A3ED /* AutoCorrect.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoCorrect.h; sourceTree = "<group>"; };
35895BEF1597639A00F2A3ED /* AutoCorrect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoCorrect.m; sourceTree = "<group>"; };
35895BF2159763A300F2A3ED /* autodict.dct */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = autodict.dct; sourceTree = "<group>"; };
@@ -181,6 +183,7 @@
350CECD9159C45790009F0FE /* database.db3 */,
35895BFA1597706800F2A3ED /* MainMenu.nib */,
3512ED581597853F0085E81E /* preferences.nib */,
+ 3587CCD715A7C5E50074BFF1 /* preferences.plist */,
8D1107310486CEB800E47090 /* Info.plist */,
089C165CFE840E0CC02AAC07 /* InfoPlist.strings */,
3578E8FC15A26EC100C1FB8E /* Credits.rtfd */,
@@ -345,6 +348,7 @@
356B186915A445DA00091215 /* AutoCorrect.png in Resources */,
356B186D15A4499B00091215 /* Credits.png in Resources */,
356B186E15A4499B00091215 /* General.png in Resources */,
+ 3587CCD815A7C5E50074BFF1 /* preferences.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -53,12 +53,16 @@ - (void)findCurrentCandidates {
_currentCandidates = [[[Suggestion sharedInstance] getList:[self term]] retain];
if (_currentCandidates && [_currentCandidates count] > 0) {
- _prevSelected = -1;
- NSString* prevString = [[CacheManager sharedInstance] stringForKey:[self term]];
+ NSString* prevString = nil;
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"]) {
+ _prevSelected = -1;
+ prevString = [[CacheManager sharedInstance] stringForKey:[self term]];
+ }
int i;
for (i = 0; i < [_currentCandidates count]; ++i) {
NSString* item = [_currentCandidates objectAtIndex:i];
- if (_prevSelected && [item isEqualToString:prevString] ) {
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"] &&
+ _prevSelected && [item isEqualToString:prevString] ) {
_prevSelected = i;
}
[_currentCandidates replaceObjectAtIndex:i withObject:
@@ -74,16 +78,15 @@ - (void)findCurrentCandidates {
- (void)updateCandidatesPanel {
if (_currentCandidates && [_currentCandidates count] > 0) {
- // Need to set font here; setting it in init... doesn't work.
- // NSUserDefaults *defaultsDictionary = [NSUserDefaults standardUserDefaults];
+ NSUserDefaults *defaultsDictionary = [NSUserDefaults standardUserDefaults];
// NSString *candidateFontName = [defaultsDictionary objectForKey:@"candidateFontName"];
// float candidateFontSize = [[defaultsDictionary objectForKey:@"candidateFontSize"] floatValue];
// NSFont *candidateFont = [NSFont fontWithName:candidateFontName size:candidateFontSize];
// [[Candidates sharedInstance] setAttributes:[NSDictionary dictionaryWithObject:candidateFont forKey:NSFontAttributeName]];
- // [[Candidates sharedInstance] setPanelType:[defaultsDictionary integerForKey:@"candidatePanelType"]];
+ [[Candidates sharedInstance] setPanelType:[defaultsDictionary integerForKey:@"CandidatePanelType"]];
[[Candidates sharedInstance] updateCandidates];
[[Candidates sharedInstance] show:kIMKLocateCandidatesBelowHint];
if (_prevSelected > -1) {
@@ -100,17 +103,19 @@ - (NSArray*)candidates:(id)sender {
}
- (void)candidateSelectionChanged:(NSAttributedString*)candidateString {
- if ([self term] && [[self term] length] > 0) {
- BOOL comp = [[candidateString string] isEqualToString:[_currentCandidates objectAtIndex:0]];
- if ((comp && _prevSelected == -1) == NO) {
- NSRange range = NSMakeRange([[self prefix] length],
- [candidateString length] - ([[self prefix] length] + [[self suffix] length]));
- [[CacheManager sharedInstance] setString:[[candidateString string] substringWithRange:range] forKey:[self term]];
-
- // Reverse Suffix Caching
- NSArray* tmpArray = [[CacheManager sharedInstance] baseForKey:[candidateString string]];
- if (tmpArray && [tmpArray count] > 0) {
- [[CacheManager sharedInstance] setString:[tmpArray objectAtIndex:1] forKey:[tmpArray objectAtIndex:0]];
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDictionary"]) {
+ if ([self term] && [[self term] length] > 0) {
+ BOOL comp = [[candidateString string] isEqualToString:[_currentCandidates objectAtIndex:0]];
+ if ((comp && _prevSelected == -1) == NO) {
+ NSRange range = NSMakeRange([[self prefix] length],
+ [candidateString length] - ([[self prefix] length] + [[self suffix] length]));
+ [[CacheManager sharedInstance] setString:[[candidateString string] substringWithRange:range] forKey:[self term]];
+
+ // Reverse Suffix Caching
+ NSArray* tmpArray = [[CacheManager sharedInstance] baseForKey:[candidateString string]];
+ if (tmpArray && [tmpArray count] > 0) {
+ [[CacheManager sharedInstance] setString:[tmpArray objectAtIndex:1] forKey:[tmpArray objectAtIndex:0]];
+ }
}
}
}
@@ -194,7 +199,12 @@ - (void)insertTab:(id)sender {
}
- (void)insertNewline:(id)sender {
- [self commitText:@""];
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"CommitNewLineOnEnter"]) {
+ [self commitText:@"\n"];
+ }
+ else {
+ [self commitText:@""];
+ }
}
- (BOOL)didCommandBySelector:(SEL)aSelector client:(id)sender {
View
@@ -10,13 +10,11 @@
@interface AvroParser : NSObject {
NSString* _vowel;
NSString* _consonant;
- NSMutableString* _casesensitive;
+ NSString* _casesensitive;
NSArray* _patterns;
int _maxPatternLength;
}
-+ (void)allocateSharedInstance;
-+ (void)deallocateSharedInstance;
+ (AvroParser *)sharedInstance;
- (NSString*)parse:(NSString*)string;
Oops, something went wrong.

0 comments on commit 7097ead

Please sign in to comment.