-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitStreamFrameDetector.mm
142 lines (124 loc) · 3.95 KB
/
BitStreamFrameDetector.mm
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
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "BitStreamFrameDetector.h"
#import "UserSettings.h"
@implementation BitStreamFrameDetector
@synthesize bits, contentSize, frameContents, observer, prefix, suffix;
+ (id)create
{
return [[[BitStreamFrameDetector alloc] init] autorelease];
}
- (void)makeBits
{
if (bits == nil || [bits length] != frameSize) {
[bits release];
bits = [[NSMutableString stringWithCapacity:frameSize] retain];
}
}
- (id)init
{
if (self = [super init]) {
frameContents = nil;
observer = nil;
[self updateFromSettings];
}
return self;
}
- (void)dealloc
{
[bits release];
[prefix release];
[suffix release];
[frameContents release];
self.observer = nil;
[super dealloc];
}
- (void)reset
{
[bits setString:@""];
if (frameContents != nil) {
[frameContents release];
frameContents = nil;
}
}
- (void)updateFromSettings
{
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
prefix = [[settings stringForKey:kSettingsBitStreamFrameDetectorPrefixKey] retain];
suffix = [[settings stringForKey:kSettingsBitStreamFrameDetectorSuffixKey] retain];
contentSize = [settings integerForKey:kSettingsBitStreamFrameDetectorContentSizeKey];
frameSize = contentSize + [prefix length] + [suffix length];
[self makeBits];
}
- (void)setPrefix:(NSString*)value
{
frameSize += ([value length] - [prefix length]);
[prefix release];
prefix = [value retain];
[self makeBits];
}
- (void)setSuffix:(NSString*)value
{
frameSize += ([value length] - [suffix length]);
[suffix release];
suffix = [value retain];
[self makeBits];
}
- (void)setContentSize:(NSUInteger)value
{
frameSize += (value - contentSize);
contentSize = value;
[self makeBits];
}
- (void)nextBitValue:(NSString*)bitValue
{
[bits appendString:bitValue];
if ([bits length] == frameSize) {
if ([bits hasPrefix:prefix] && [bits hasSuffix:suffix]) {
//
// Extract and save the contents of the frame. Alert observer of new frame contents.
//
LOG(@"BitStreamFrameDetector: matched frame: %@", bits);
[frameContents release];
frameContents = [[bits substringWithRange:NSMakeRange([prefix length], contentSize)] retain];
if (observer != nil) {
[observer frameContentBitStream:frameContents];
}
//
// Clear out the bit accumulator since we used everything.
//
[bits deleteCharactersInRange:NSMakeRange(0, frameSize)];
}
else {
//
// We did not match. Locate the next match for prefix and start from there. If not found, start dropping
// bits from the end of the prefix and look for a match at the end of the string.
//
NSRange found = [bits rangeOfString:prefix options:NSLiteralSearch range:NSMakeRange(1, frameSize-1)];
if (found.location != NSNotFound) {
[bits deleteCharactersInRange:NSMakeRange(0, found.location)];
}
else {
NSMutableString* tag = [NSMutableString stringWithString:
[prefix substringWithRange:NSMakeRange(0, [prefix length] - 1)]];
while ([tag length] > 0) {
if ([bits hasSuffix:tag]) {
[bits release];
bits = [tag retain];
break;
}
[tag deleteCharactersInRange:NSMakeRange(0, 1)];
}
//
// No match above. Forget everything we've seen in the past.
//
if ([tag length] == 0) {
[bits setString:@""];
}
}
}
}
}
@end