// // Calculation.m // LeanCalc // // Created by Marc Liyanage on 04.02.05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "Calculation.h" @implementation Calculation - (id)init { self = [super init]; if (!self) return nil; STRING *falseString = makestring("false"); VALUE falseValue; falseValue.v_type = V_STR; falseValue.v_subtype = V_NOSUBTYPE; falseValue.v_str = falseString; setconfig(CONFIG_TILDE, &falseValue); setconfig(CONFIG_TAB, &falseValue); [Calculation setKeys:[NSArray arrayWithObject:@"expression"] triggerChangeNotificationsForDependentKey:@"result"]; return self; } - (void)dealloc { [expression release]; [next release]; [super dealloc]; } - (NSString *)shortcut { int historyIndex = [self historyIndex]; if (historyIndex < 1) return nil; unichar ch = 0x2318; NSString *str = [NSString stringWithCharacters:&ch length:1]; return [NSString stringWithFormat:@"%@ %d", str, historyIndex]; } - (int)historyIndex { if (!next) return 0; int nextIndex = [next historyIndex]; if (nextIndex > 8 || nextIndex == -1) return -1; return nextIndex + 1; } extern jmp_buf calc_jmp_buf; extern jmp_buf jmpbuf; extern int calc_jmp; extern char *calc_error; - (NSString *)result { if (![expression length]) return nil; int error; if ((error = setjmp(calc_jmp_buf)) != 0) { reinitialize(); calc_jmp = 1; return nil; } calc_jmp = 1; if ((error = setjmp(jmpbuf)) != 0) { reinitialize(); return nil; } reinitialize(); openstring([expression UTF8String], [expression length]); math_divertio(); getcommands(1); char *result = math_getdivertedio(); NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (result) free(result); return resultString; } - (BOOL)expressionIsEmpty { return ![expression length]; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:expression forKey:@"expression"]; [coder encodeObject:next forKey:@"next"]; } - (id)initWithCoder:(NSCoder *)decoder { self = [super init]; if (!self) return nil; [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"]; [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"]; return self; } @end