forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSCommandHistory.m
118 lines (102 loc) · 2.47 KB
/
FSCommandHistory.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
/* StrBuffer.m Copyright (c) 1998-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSCommandHistory.h"
@implementation FSCommandHistory
- (id)addStr:(NSString *)str
{
if ([array count] != 0)
{
head = (head+1) % [array count];
if (head == queue) queue = (queue+1) % [array count];
[array replaceObjectAtIndex:head withObject:str];
[self goToLast];
}
return self;
}
- (void)dealloc
{
[array release];
[super dealloc];
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:array forKey:@"array"];
[coder encodeInteger:head forKey:@"head"];
[coder encodeInteger:queue forKey:@"queue"];
[coder encodeInteger:cursor forKey:@"cursor"];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
if ([coder allowsKeyedCoding])
{
array = [[coder decodeObjectForKey:@"array"] retain];
head = [coder decodeIntegerForKey:@"head"];
queue = [coder decodeIntegerForKey:@"queue"];
cursor= [coder decodeIntegerForKey:@"cursor"];
}
else
{
int intHead, intQueue, intCursor;
array = [[coder decodeObject] retain];
[coder decodeValueOfObjCType:@encode(int) at:&intHead];
head = intHead;
[coder decodeValueOfObjCType:@encode(int) at:&intQueue];
queue = intQueue;
[coder decodeValueOfObjCType:@encode(int) at:&intCursor];
cursor = intCursor;
}
return self;
}
- (id)goToFirst
{
cursor = head;
return self;
}
- (id)goToLast
{
cursor = queue;
return self;
}
- (id)goToNext
{
if ([array count] != 0)
{
if (cursor == head) cursor = queue;
else cursor = (cursor+1) % [array count];
}
return self;
}
- (id)goToPrevious
{
if([array count] != 0)
{
if (cursor == queue) cursor = head;
else cursor = (cursor-1+[array count]) % [array count];
}
return self;
}
- (NSString *)getMostRecentlyInsertedStr
{
if ([array count] != 0) return [array objectAtIndex:head];
else return(@"");
}
- (NSString *)getStr
{
if ([array count] != 0) return [array objectAtIndex:cursor];
else return(@"");
}
- (id)init {return [self initWithUIntSize:0];}
- (id)initWithUIntSize:(NSUInteger)size
{
if ((self = [super init]))
{
array = [[NSMutableArray alloc] initWithCapacity:size];
head = 0; queue =0;
for (; size > 0; size--) [array addObject:@""];
return self;
}
return nil;
}
- (NSInteger)size {return [array count];}
@end