-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOHMData.m
140 lines (119 loc) · 3.16 KB
/
OHMData.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
//
// OHMData.m
// OHMTagLib
//
// Created by Tobias Hieta on 2011-01-10.
// Copyright 2011 OHM Interactive. All rights reserved.
//
#import "OHMData.h"
#import "GTMLogger.h"
#define EXTRA_DEBUG 1
#if EXTRA_DEBUG
#define extraLogDebug(...) GTMLoggerDebug (__VA_ARGS__)
#define extraLogError(...) GTMLoggerError (__VA_ARGS__)
#else
#define extraLogDebug(...) do {} while(0)
#define extraLogError(...) do {} while(0)
#endif
@implementation OHMData
@synthesize shouldExpand;
-(NSUInteger)freeSpace
{
return dataBufferSize - dataBufferLength;
}
-(NSUInteger)dataSize
{
return dataBufferSize;
}
-(NSUInteger)length
{
return dataBufferLength;
}
-(id)initWithSize:(NSUInteger)size
{
if ((self = [super init])) {
dataBuffer = malloc (size);
memset(dataBuffer, 0, size);
dataBufferSize = size;
dataBufferLength = 0;
self.shouldExpand = YES;
}
return self;
}
-(id)init
{
if ((self = [self initWithSize:4096])) {
}
return self;
}
-(BOOL)addData:(NSData *)data
{
if ([data length] > self.freeSpace) {
if (self.shouldExpand) {
/* realloc */
extraLogDebug(@"need to realloc because freeSpace is %d", self.freeSpace);
NSUInteger newSize = MAX (dataBufferSize * 2, [data length] + dataBufferSize);
dataBuffer = realloc (dataBuffer, newSize);
if (!dataBuffer) {
extraLogError(@"failed to realloc the buffer to size: %d", newSize);
}
memset(dataBuffer + dataBufferSize, 0, newSize-dataBufferSize);
dataBufferSize = newSize;
extraLogDebug(@"Reallocing buffer to size %d", newSize);
} else {
extraLogError(@"wanted to add bytes, but shouldExpand was not set correctly.");
return NO;
}
}
[data getBytes:dataBuffer+dataBufferLength length:[data length]];
dataBufferLength += [data length];
return YES;
}
-(NSData*)getData
{
return [NSData dataWithBytes:dataBuffer length:dataBufferLength];
}
-(NSData*)popData
{
NSData *ret = [self getData];
/* reset data */
dataBufferLength = 0;
return ret;
}
-(NSData*)getDataWithRange:(NSRange)range
{
NSUInteger rangeEnd = range.length + range.location;
if (rangeEnd > dataBufferLength) {
extraLogError(@"range is out of bounds");
return nil;
/* TODO: raise exception? */
}
return [NSData dataWithBytes:dataBuffer+range.location length:range.length];
}
-(NSData*)popDataWithRange:(NSRange)range
{
NSData *data = [self getDataWithRange:range];
[self discardDataWithRange:range];
return data;
}
-(void)discardDataWithRange:(NSRange)range; {
NSUInteger rangeEnd = range.location + range.length;
NSAssert(rangeEnd <= dataBufferLength, @"Need to have a range that is within the size of the buffer");
if (rangeEnd != dataBufferLength) {
memmove (dataBuffer + range.location,
dataBuffer + rangeEnd,
dataBufferLength - rangeEnd);
}
dataBufferLength -= range.length;
memset(dataBuffer + dataBufferLength, 0, range.length);
}
-(void)removeAllData
{
dataBufferLength = 0;
}
-(void)dealloc
{
free (dataBuffer);
[super dealloc];
}
@end