// // Word.m // Duncan // // Created by Risa Mettaserikul on Tue Feb 18 2003. // Copyright (c) 2003 CSFSS. All rights reserved. // #import #import "Word.h" @implementation WordState + (WordState *)state { id st = [super new]; [st autorelease]; return st; } @end @implementation Word static NSLock *_globalWordLock; + (void)initialize { _globalWordLock = [NSLock new]; } - (void)globalLock { [_globalWordLock lock]; } - (void)globalUnlock { [_globalWordLock unlock]; } + (Word *) wordWithString:(NSString *)w definition:(NSString *)def { return [[[self alloc] initWithString:w definition:def] autorelease]; } - (id) init { string = definition = nil; _dict = [[NSMutableDictionary alloc] init]; return [super init]; } - (id) initWithString:(NSString *)initstr definition:(NSString *)def { self = [self init]; string = [initstr retain]; definition = [def retain]; return self; } // NEW MAIN QUERY - (void) query:(id)datarep withParentPath:(NSString *)parentpath { NSComparisonResult cp = [[datarep keyword] caseInsensitiveCompare:string]; if (cp == NSOrderedSame) { // set path [self setPath:[NSString stringWithFormat:@"%@/%@",parentpath,string]]; [[datarep wordList] addObject:self]; [datarep flush]; } } - (NSString *) string { return string; } - (NSString *) definition { /* id def; [self lock]; def = [_dict objectForKey:@"Definition"]; if (def == nil) def = @"No definition"; [self unlock];*/ if (definition == nil) return @"No definition"; return definition; } - (NSString *) path { id pt; [self lock]; pt = [_dict objectForKey:@"Path"]; if (!pt) pt = @"no path"; [self unlock]; return pt; } - (id) target { id rt; [self lock]; rt = [_dict objectForKey:@"Target"]; [self unlock]; return rt; } - (id) parent { return parentDictionary; } - (void) setParent:(Word *)newparent { parentDictionary = newparent; } - (BOOL)isReady { return YES; } - (void) setDefinition:(NSString *)aDefinition { /* [self lock]; [_dict setObject:aDefinition forKey:@"Definition"]; [self unlock]; */ id oldstr = definition; definition = [aDefinition retain]; [oldstr release]; } - (id) wordState { id ws; [self lock]; if (![_dict objectForKey:@"State"]) { [_dict setObject:[WordState state] forKey:@"State"]; } ws = [_dict objectForKey:@"State"]; [self unlock]; return ws; } - (void) setPath:(NSString *)aPath { [self lock]; [_dict setObject:aPath forKey:@"Path"]; [self unlock]; } - (void) setTarget:(NSString *)newTarget { [self lock]; if (newTarget != nil) { [_dict setObject:newTarget forKey:@"Target"]; } else { [_dict removeObjectForKey:@"Target"]; } [self unlock]; } - (void) setString:(NSString *)word { id oldstr = string; string = [word retain]; [oldstr release]; } static int t; - (void) lock { [_globalWordLock lock]; id aLock = [_dict objectForKey:@"Lock"]; if (!aLock) { // NSLog(@"Create lock for %@ %d",string,t); t++; [_dict setObject:[NSLock new] forKey:@"Lock"]; aLock = [_dict objectForKey:@"Lock"]; } [_globalWordLock unlock]; // NSLog(@"thread %@ %d lock",string,[NSThread currentThread]); [aLock lock]; } - (void) unlock { [_globalWordLock lock]; // NSLog(@"thread %@ %d unlock",string,[NSThread currentThread]); [[_dict objectForKey:@"Lock"] unlock]; [_globalWordLock unlock]; } - (NSComparisonResult)caseInsensitiveCompare:(Word *)aWord { return [string caseInsensitiveCompare:[aWord string]]; } - (int)scoreWithString:(NSString *)aString { return 0; char buff[256]; bzero(buff,256); char *cS,*cD; int i,s; cD = [aString cString]; cS = [[self string] cString]; while (*cD) { buff[*cD]++; cD++; } while (*cS) { if (buff[*cS]) buff[*cS]--; else buff[*cS]++; cS++; } for (i=0,s=0;i<256;i++) { s += buff[i]; } return abs(s); } - (unsigned int)length { return [string length]; } - (NSAttributedString *)presentation { NSLog(@"presentation %@",string); NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",[self definition]]]; [str addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0,[str length])]; return [str autorelease]; } - (void) dealloc { NSLog(@"Word +dealloc %@",string); [string release]; [[_dict objectForKey:@"Lock"] release]; [_dict release]; } @end