]> Dogcows Code - chaz/thecheat/blob - SearchData.m
6170bac8c92364ac895a260d4844c94e406ff7d1
[chaz/thecheat] / SearchData.m
1 //
2 // SearchData.m
3 // The Cheat
4 //
5 // Created by Chaz McGarvey on 12/13/04.
6 // Copyright 2004 Chaz McGarvey. All rights reserved.
7 //
8
9 #import "SearchData.h"
10
11
12 @implementation SearchData
13
14
15 - (id)init
16 {
17 if ( self = [super init] ) {
18 [self setVariableType:TCInt32];
19 [self setIntegerSign:TCSigned];
20 [self setSearchOperator:TCEqual];
21 [self setValueUsed:TCGivenValue];
22
23 _undoes = -1;
24 _redoes = 0;
25 }
26 return self;
27 }
28
29 - (void)dealloc
30 {
31 [_variableValue release];
32 TCReleaseArray( _addresses );
33 TCReleaseArray( _values );
34 [super dealloc];
35 }
36
37
38 - (TCVariableType)variableType
39 {
40 return _variableType;
41 }
42
43 - (void)setVariableType:(TCVariableType)varType
44 {
45 // can't change the variable type when there has been a search
46 if ( ![self hasSearchedOnce] ) {
47 _variableType = varType;
48 }
49 }
50
51
52 - (TCIntegerSign)integerSign
53 {
54 return _integerSign;
55 }
56
57 - (void)setIntegerSign:(TCIntegerSign)sign
58 {
59 // can't change the integer sign when there has been a search
60 if ( ![self hasSearchedOnce] ) {
61 _integerSign = sign;
62 }
63 }
64
65
66 - (TCSearchOperator)searchOperator
67 {
68 return _operator;
69 }
70
71 - (void)setSearchOperator:(TCSearchOperator)op
72 {
73 _operator = op;
74 }
75
76
77 - (TCSearchType)valueUsed
78 {
79 if ( ![self hasSearchedOnce] ) {
80 // if there is no search, then use search value no matter what
81 _value = TCGivenValue;
82 }
83 return _value;
84 }
85
86 - (void)setValueUsed:(TCSearchType)value
87 {
88 _value = value;
89 }
90
91
92 - (Variable *)searchValue
93 {
94 if ( !_variableValue ) {
95 // create a zero value if there is none
96 _variableValue = [[Variable alloc] init];
97 }
98 return _variableValue;
99 }
100
101 - (void)setSearchValue:(Variable *)value
102 {
103 [value retain];
104 [_variableValue release];
105 _variableValue = value;
106 }
107
108
109 - (unsigned)numberOfResults
110 {
111 if ( _addresses ) {
112 return TCArrayElementCount( _addresses );
113 }
114 return 0;
115 }
116
117 - (TCArray)addresses
118 {
119 return _addresses;
120 }
121
122 - (void)setAddresses:(TCArray)addresses
123 {
124 TCReleaseArray( _addresses );
125 _addresses = addresses;
126
127 if ( !_addresses ) {
128 // clear the undoes and redoes if the search is cleared
129 _undoes = -1;
130 _redoes = 0;
131 }
132
133 // clear the stored values
134 [self setValues:NULL];
135 }
136
137 - (TCArray)values
138 {
139 return _values;
140 }
141
142 - (void)setValues:(TCArray)values
143 {
144 TCReleaseArray( _values );
145 _values = values;
146 }
147
148 - (void)setValue:(Variable *)value atIndex:(unsigned)index
149 {
150 if ( _values ) {
151 if ( index < TCArrayElementCount( _values ) ) {
152 TCArraySetElementAtIndex( _values, index, [value value] );
153 }
154 }
155 }
156
157 - (BOOL)valuesLoaded
158 {
159 return ( _values != NULL );
160 }
161
162 - (Variable *)variableAtIndex:(unsigned)index
163 {
164 if ( [self hasSearchedOnce] ) {
165 Variable *var = [[Variable alloc] initWithType:[self variableType] integerSign:[self integerSign]];
166 [var setAddress:*(TCAddress *)TCArrayElementAtIndex( [self addresses], index )];
167 if ( [self valuesLoaded] ) {
168 [var setValue:TCArrayElementAtIndex( [self values], index ) size:TCArrayElementSize([self values])];
169 }
170 return [var autorelease];
171 }
172 return nil;
173 }
174
175 - (NSString *)stringForRow:(unsigned)rowIndex
176 {
177 Variable *var = [self variableAtIndex:rowIndex];
178
179 if ( var ) {
180 if ( [self valuesLoaded] ) {
181 if ( [var type] == TCString ) {
182 return [NSString stringWithFormat:@"%@ = \"%@\"", [var addressString], [var stringValue]];
183 }
184 else {
185 return [NSString stringWithFormat:@"%@ = %@", [var addressString], [var stringValue]];
186 }
187 }
188 else {
189 return [var addressString];
190 }
191 }
192 return @"";
193 }
194
195
196 - (BOOL)hasSearchedOnce
197 {
198 return ( _addresses != NULL );
199 }
200
201
202 - (int)undoesLeft
203 {
204 return _undoes;
205 }
206
207 - (int)redoesLeft
208 {
209 return _redoes;
210 }
211
212 - (void)didAddResults
213 {
214 _undoes++;
215 _redoes = 0;
216 }
217
218 - (void)didUndo
219 {
220 _undoes--;
221 _redoes++;
222 }
223
224 - (void)didRedo
225 {
226 _undoes++;
227 _redoes--;
228 }
229
230
231 - (BOOL)isTypeInteger
232 {
233 return _variableType <= TCInt8;
234 }
235
236
237 - (void)clearResults
238 {
239 [self setAddresses:NULL];
240 }
241
242
243 @end
This page took 0.038209 seconds and 3 git commands to generate.