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;
68 extern jmp_buf calc_jmp_buf;
69 extern jmp_buf jmpbuf;
71 extern char *calc_error;
73 - (NSString *)result {
75 if (![expression length]) return nil;
78 if ((error = setjmp(calc_jmp_buf)) != 0) {
85 if ((error = setjmp(jmpbuf)) != 0) {
91 openstring([expression UTF8String], [expression length]);
94 char *result = math_getdivertedio();
95 NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
97 if (result) free(result);
104 - (BOOL)expressionIsEmpty {
105 return ![expression length];
110 - (void)encodeWithCoder:(NSCoder *)coder
113 // [super encodeWithCoder:coder];
114 [coder encodeObject:expression forKey:@"expression"];
115 [coder encodeObject:next forKey:@"next"];
120 - (id)initWithCoder:(NSCoder *)decoder {
123 if (!self) return nil;
125 [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
126 [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];