-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFIndexFile.m
321 lines (261 loc) · 9.17 KB
/
IFIndexFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// IFIndexFile.m
// Inform
//
// Created by Andrew Hunter on Sun Jun 13 2004.
// Copyright (c) 2004 Andrew Hunter. All rights reserved.
//
#import "IFIndexFile.h"
@implementation IFIndexFile
- (id) initWithContentsOfFile: (NSString*) filename {
self = [self initWithData: [NSData dataWithContentsOfFile: filename]];
return self;
}
static int intValueComparer(id a, id b, void* context) {
int aV = [a intValue];
int bV = [b intValue];
if (aV < bV) return -1;
if (aV > bV) return 1;
return 0;
}
- (id) initWithData: (NSData*) data {
self = [super init];
if (self) {
if (data == nil) {
[self release];
return nil;
}
// Data is provided as a property list file, which makes things easy for us to parse
// Req 10.2 (surely no-one is still seriously using 10.1?)
NSPropertyListFormat format;
NSString* error = nil;
id plist = [NSPropertyListSerialization propertyListFromData: data
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &error];
// Sanity check
if (plist == nil) {
NSLog(@"IFIndexFile: found no data");
[self release];
return nil;
}
if (error != nil) {
NSLog(@"IFIndexFile: error in file: %@", error);
[self release];
return nil;
}
if (![plist isKindOfClass: [NSDictionary class]]) {
NSLog(@"IFIndexFile: property list does not contain a dictionary");
[self release];
return nil;
}
// OK: we've got an index in plist. Do some processing...
index = [plist copy];
// Need the keys sorted by numeric value to make any sense of them
NSArray* orderedKeys = [index allKeys];
orderedKeys = [orderedKeys sortedArrayUsingFunction: intValueComparer
context: nil];
// Turn the index into a hierarchical dictionary
// Top level indexed by filenames
filenamesToIndexes = [[NSMutableDictionary alloc] init];
NSEnumerator* keyEnum = [orderedKeys objectEnumerator];
NSString* key;
while (key = [keyEnum nextObject]) {
NSDictionary* item = [index objectForKey: key];
if ([item isKindOfClass: [NSDictionary class]] &&
[item objectForKey: @"Filename"] != nil &&
[item objectForKey: @"Indentation"] != nil &&
[item objectForKey: @"Level"] != nil &&
[item objectForKey: @"Line"] != nil &&
[item objectForKey: @"Title"] != nil) {
NSString* filename = [item objectForKey: @"Filename"];
int indent = [[item objectForKey: @"Indentation"] intValue];
int line = [[item objectForKey: @"Line"] intValue];
NSString* title = [item objectForKey: @"Title"];
// HACK: only include the source files
if (![[filename stringByDeletingLastPathComponent] isEqualToString: @"Source"]) continue;
// Get the initial index for this file
NSMutableArray* indexForFilename = [filenamesToIndexes objectForKey: filename];
if (indexForFilename == nil) {
indexForFilename = [[NSMutableArray alloc] init];
[filenamesToIndexes setObject: [indexForFilename autorelease]
forKey: filename];
} // indexForFilename == nil
if ([title isEqualToString: @"--"]) continue; // Ignore these items
// Each index level is an array of entries at a specific indentation level
// Each item is a dictionary consisting of the 'Line' and 'Title' keys
// plus a 'Contents' key which (if present) indicates the items that come
// under this one.
//
// Items are assumed to appear in order, and the hierarchy is assumed not to cross
// files
NSMutableDictionary* newItem = [NSMutableDictionary dictionaryWithObjectsAndKeys:
title, @"Title", [NSNumber numberWithInt: line], @"Line",
filename, @"Filename", nil];
// Iterate down to the lowest item
NSMutableArray* indexToAdd = indexForFilename;
int x;
for (x=0; x<indent; x++) {
NSMutableArray* newIndex = [[indexToAdd lastObject] objectForKey: @"Contents"];
if (newIndex == nil) {
if ([indexToAdd lastObject] == nil) {
NSLog(@"IFIndexFile BUG: found an empty index");
break;
}
// Need to add a new level
newIndex = [[NSMutableArray alloc] init];
[[indexToAdd lastObject] setObject: [newIndex autorelease]
forKey: @"Contents"];
indexToAdd = newIndex;
break;
}
indexToAdd = newIndex;
} // x=0;x<indent;x++
// indexToAdd now contains the index where we need to add a new item - so do so
[indexToAdd addObject: newItem];
} // if (item is an index entry)
} // key = [keyEnum nextObject]
// Set up the rest of the data
}
return self;
}
- (void) dealloc {
if (index) [index release];
if (filenamesToIndexes) [filenamesToIndexes release];
[super dealloc];
}
// == We can be an NSOutlineView data source ==
- (id)outlineView: (NSOutlineView *)outlineView
child: (int)childIndex
ofItem: (id)item {
if (item == nil) {
// Root item
NSArray* allKeys = [filenamesToIndexes allKeys];
if (childIndex >= [allKeys count]) return nil;
return [allKeys objectAtIndex: childIndex];
} else if ([item isKindOfClass: [NSString class]]) {
// Happens with the filename indexes only...
NSArray* filenameIndex = [filenamesToIndexes objectForKey: item];
if (childIndex >= [filenameIndex count]) return nil;
return [filenameIndex objectAtIndex: childIndex];
} else {
// Is an item dictionary
NSDictionary* itemDictionary = item;
NSMutableArray* contents = [itemDictionary objectForKey: @"Contents"];
if (childIndex >= [contents count]) return nil;
return [contents objectAtIndex: childIndex];
}
}
- (BOOL)outlineView: (NSOutlineView *)outlineView
isItemExpandable: (id)item {
if (item == nil) {
// Root item
return YES;
} else if ([item isKindOfClass: [NSString class]]) {
// Happens with the filename indexes only...
NSArray* filenameIndex = [filenamesToIndexes objectForKey: item];
if ([filenameIndex count] <= 0)
return NO;
else
return YES;
} else {
// Is an item dictionary
NSDictionary* itemDictionary = item;
NSMutableArray* contents = [itemDictionary objectForKey: @"Contents"];
if (contents == nil || [contents count] <= 0)
return NO;
else
return YES;
}
}
- (int) outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item {
if (item == nil) {
// Root item
NSArray* allKeys = [filenamesToIndexes allKeys];
return [allKeys count];
} else if ([item isKindOfClass: [NSString class]]) {
// Happens with the filename indexes only...
NSArray* filenameIndex = [filenamesToIndexes objectForKey: item];
return [filenameIndex count];
} else {
// Is an item dictionary
NSDictionary* itemDictionary = item;
NSMutableArray* contents = [itemDictionary objectForKey: @"Contents"];
if (contents == nil || [contents count] <= 0)
return 0;
else
return [contents count];
}
}
- (id) outlineView:(NSOutlineView *)outlineView
objectValueForTableColumn:(NSTableColumn *)tableColumn
byItem:(id)item {
// Valid column identifiers are 'title' and 'line'
NSString* identifier = [tableColumn identifier];
if (item == nil) {
// Root item
return nil;
} else if ([item isKindOfClass: [NSString class]]) {
if ([identifier isEqualToString: @"title"]) {
return item;
} else if ([identifier isEqualToString: @"line"]) {
return @"";
} else {
return nil;
}
} else {
// Is an item dictionary
NSDictionary* itemDictionary = item;
if ([identifier isEqualToString: @"title"]) {
return [itemDictionary objectForKey: @"Title"];
} else if ([identifier isEqualToString: @"line"]) {
return [itemDictionary objectForKey: @"Line"];
} else {
return nil;
}
}
}
// == Functions for modifying what we display ==
// (None implemented yet)
// Possible ideas:
// Search for symbols with specific names
// Limit to only files in a particular set/IFProject object (ie avoid extensions file)
// == Functions ==
- (NSDictionary*) itemForItem: (id) item {
// Given an item in the outline view, returns the item info NSDictionary
if (item == nil) {
// Root item has no dictionary
return nil;
} else if ([item isKindOfClass: [NSString class]]) {
// Filename items are strings
return [NSDictionary dictionaryWithObjectsAndKeys:
item, @"Filename", item, @"Title", nil]; // Note: no line numbers
} else if ([item isKindOfClass: [NSDictionary class]]) {
// 'Normal' items are NSDictionaries
return item;
} else {
// Could be anything
return nil;
}
}
- (NSString*) filenameForItem: (id) item {
// Given an item in the outline view, work out the filename that it refers to
NSDictionary* itemInfo = [self itemForItem: item];
if (itemInfo == nil) return nil;
return [itemInfo objectForKey: @"Filename"];
}
- (int) lineForItem: (id) item {
// Given an item in the outline view, work out the line number that it refers to
NSDictionary* itemInfo = [self itemForItem: item];
if (itemInfo == nil) return -1;
if ([itemInfo objectForKey: @"Line"] == nil) return -1;
return [[itemInfo objectForKey: @"Line"] intValue];
}
- (NSString*) titleForItem: (id) item {
// Given an item in the outline view, work out the line number that it refers to
NSDictionary* itemInfo = [self itemForItem: item];
if (itemInfo == nil) return nil;
return [itemInfo objectForKey: @"Title"];
}
@end