Initial revision
[LeanCalc.git] / Sources / Calculation.m
1 //
2 //  Calculation.m
3 //  LeanCalc
4 //
5 //  Created by Marc Liyanage on 04.02.05.
6 //  Copyright 2005 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "Calculation.h"
10
11
12 @implementation Calculation
13
14 - (id)init {
15
16         self = [super init];
17         if (!self) return nil;
18         
19         STRING *falseString = makestring("false");
20         VALUE falseValue;
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);
26         
27         [Calculation setKeys:[NSArray arrayWithObject:@"expression"] triggerChangeNotificationsForDependentKey:@"result"];
28         
29         return self;
30 }
31
32
33
34 - (void)dealloc {
35         
36         [expression release];
37         [next release];
38 }
39
40
41
42 - (NSString *)shortcut {
43
44         int historyIndex = [self historyIndex];
45         if (historyIndex < 1) return nil;
46
47         unichar ch = 0x2318;
48         NSString *str = [NSString stringWithCharacters:&ch length:1];
49         return [NSString stringWithFormat:@"%@ %d", str, historyIndex];
50 }
51
52
53
54 - (int)historyIndex {
55         if (!next) return 0;
56         int nextIndex = [next historyIndex];
57         if (nextIndex > 8 || nextIndex == -1) return -1;
58         return nextIndex + 1;
59 }
60
61
62
63
64
65
66
67
68 extern jmp_buf calc_jmp_buf;
69 extern jmp_buf jmpbuf;
70 extern int calc_jmp;
71 extern char *calc_error;
72
73 - (NSString *)result {
74
75         if (![expression length]) return nil;
76
77         int error;
78         if ((error = setjmp(calc_jmp_buf)) != 0) {
79                 reinitialize();
80                 calc_jmp = 1;
81                 return nil;
82         }
83         calc_jmp = 1;
84         
85         if ((error = setjmp(jmpbuf)) != 0) {
86                 reinitialize();
87                 return nil;
88         }       
89         reinitialize();
90
91         openstring([expression UTF8String], [expression length]);
92         math_divertio();
93         getcommands(1);
94         char *result = math_getdivertedio();
95         NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
96
97         if (result) free(result);
98
99         return resultString;
100         
101 }
102
103
104 - (BOOL)expressionIsEmpty {
105         return ![expression length];
106 }
107
108
109
110 - (void)encodeWithCoder:(NSCoder *)coder
111 {
112
113 //    [super encodeWithCoder:coder];
114         [coder encodeObject:expression forKey:@"expression"];
115         [coder encodeObject:next forKey:@"next"];
116
117 }
118
119
120 - (id)initWithCoder:(NSCoder *)decoder {
121
122         self = [super init];
123         if (!self) return nil;
124         
125         [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
126         [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];
127         return self;
128 }
129
130
131
132
133
134 @end