cleanup, reformatting
[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 extern jmp_buf calc_jmp_buf;
63 extern jmp_buf jmpbuf;
64 extern int calc_jmp;
65 extern char *calc_error;
66
67 - (NSString *)result {
68
69         if (![expression length]) return nil;
70
71         int error;
72         if ((error = setjmp(calc_jmp_buf)) != 0) {
73                 reinitialize();
74                 calc_jmp = 1;
75                 return nil;
76         }
77         calc_jmp = 1;
78         
79         if ((error = setjmp(jmpbuf)) != 0) {
80                 reinitialize();
81                 return nil;
82         }       
83         reinitialize();
84
85         openstring([expression UTF8String], [expression length]);
86         math_divertio();
87         getcommands(1);
88         char *result = math_getdivertedio();
89         NSString *resultString = [[NSString stringWithCString:result] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
90
91         if (result) free(result);
92
93         return resultString;
94         
95 }
96
97
98 - (BOOL)expressionIsEmpty {
99         return ![expression length];
100 }
101
102
103
104 - (void)encodeWithCoder:(NSCoder *)coder {
105         [coder encodeObject:expression forKey:@"expression"];
106         [coder encodeObject:next forKey:@"next"];
107 }
108
109
110 - (id)initWithCoder:(NSCoder *)decoder {
111
112         self = [super init];
113         if (!self) return nil;
114         
115         [self setValue:[decoder decodeObjectForKey:@"expression"] forKey:@"expression"];
116         [self setValue:[decoder decodeObjectForKey:@"next"] forKey:@"next"];
117         return self;
118 }
119
120
121
122
123
124 @end