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"];
43 - (NSString *)shortcut {
45 int historyIndex = [self historyIndex];
46 if (historyIndex < 1) return nil;
49 NSString *str = [NSString stringWithCharacters:&ch length:1];
50 return [NSString stringWithFormat:@"%@ %d", str, historyIndex];
57 int nextIndex = [next historyIndex];
58 if (nextIndex > 8 || nextIndex == -1) return -1;
63 extern jmp_buf calc_jmp_buf;
64 extern jmp_buf jmpbuf;
66 extern char *calc_error;
68 - (NSString *)result {
70 if (![expression length]) return nil;
73 if ((error = setjmp(calc_jmp_buf)) != 0) {
80 if ((error = setjmp(jmpbuf)) != 0) {
86 openstring([expression UTF8String], [expression length]);
89 char *result = math_getdivertedio();
90 NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
92 if (result) free(result);
99 - (BOOL)expressionIsEmpty {
100 return ![expression length];
105 - (void)encodeWithCoder:(NSCoder *)coder {
106 [coder encodeObject:expression forKey:@"expression"];
107 [coder encodeObject:next forKey:@"next"];
111 - (id)initWithCoder:(NSCoder *)decoder {
114 if (!self) return nil;
116 [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
117 [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];