5 // Created by Marc Liyanage on 04.02.05.
6 // Copyright 2005 __MyCompanyName__. All rights reserved.
9 #import "Calculation.h"
12 @implementation Calculation
17 if (!self) return nil;
19 STRING *falseString = makestring("false");
21 falseValue.v_type = V_STR;
22 falseValue.v_subtype = V_NOSUBTYPE;
23 falseValue.v_str = falseString;
24 setconfig(CONFIG_TILDE, &falseValue);
25 setconfig(CONFIG_TAB, &falseValue);
27 [Calculation setKeys:[NSArray arrayWithObject:@"expression"] triggerChangeNotificationsForDependentKey:@"result"];
42 - (NSString *)shortcut {
44 int historyIndex = [self historyIndex];
45 if (historyIndex < 1) return nil;
48 NSString *str = [NSString stringWithCharacters:&ch length:1];
49 return [NSString stringWithFormat:@"%@ %d", str, historyIndex];
56 int nextIndex = [next historyIndex];
57 if (nextIndex > 8 || nextIndex == -1) return -1;
62 extern jmp_buf calc_jmp_buf;
63 extern jmp_buf jmpbuf;
65 extern char *calc_error;
67 - (NSString *)result {
69 if (![expression length]) return nil;
72 if ((error = setjmp(calc_jmp_buf)) != 0) {
79 if ((error = setjmp(jmpbuf)) != 0) {
85 openstring([expression UTF8String], [expression length]);
88 char *result = math_getdivertedio();
89 NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
91 if (result) free(result);
98 - (BOOL)expressionIsEmpty {
99 return ![expression length];
104 - (void)encodeWithCoder:(NSCoder *)coder {
105 [coder encodeObject:expression forKey:@"expression"];
106 [coder encodeObject:next forKey:@"next"];
110 - (id)initWithCoder:(NSCoder *)decoder {
113 if (!self) return nil;
115 [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
116 [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];