forked from MacPaw/XADMaster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSSegmentedHandle.m
222 lines (181 loc) · 4.58 KB
/
CSSegmentedHandle.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
/*
* CSSegmentedHandle.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "CSSegmentedHandle.h"
NSString *CSNoSegmentsException=@"CSNoSegmentsException";
NSString *CSSizeOfSegmentUnknownException=@"CSSizeOfSegmentUnknownException";
@implementation CSSegmentedHandle
-(id)init
{
if(self=[super init])
{
count=0;
currindex=NSNotFound;
currhandle=nil;
segmentends=NULL;
segmentsizes=nil;
}
return self;
}
-(id)initAsCopyOf:(CSSegmentedHandle *)other
{
if(self=[super initAsCopyOf:other])
{
count=other->count;
currindex=other->currindex;
currhandle=[other->currhandle copy];
size_t size=count*sizeof(segmentends[0]);
segmentends=malloc(size);
memcpy(segmentends,other->segmentends,size);
segmentsizes=[other->segmentsizes retain];
}
return self;
}
-(void)dealloc
{
[currhandle release];
free(segmentends);
[segmentsizes release];
[super dealloc];
}
-(CSHandle *)currentHandle
{
[self _open];
return currhandle;
}
-(NSArray *)segmentSizes
{
[self _open];
if(!segmentsizes)
{
NSMutableArray *array=[NSMutableArray array];
NSInteger last=0;
for(NSInteger i=0;i<count;i++)
{
[array addObject:[NSNumber numberWithLongLong:segmentends[i]-last]];
last=segmentends[i];
}
segmentsizes=[[NSArray arrayWithArray:array] retain];
}
return segmentsizes;
}
-(off_t)fileSize
{
[self _open];
return segmentends[count-1];
}
-(off_t)offsetInFile
{
[self _open];
off_t start=0;
if(currindex>0) start=segmentends[currindex-1];
return start+[currhandle offsetInFile];
}
-(BOOL)atEndOfFile
{
[self _open];
return currindex==count-1 && [currhandle atEndOfFile];
}
-(void)seekToFileOffset:(off_t)offs
{
[self _open];
for(NSInteger i=0;i<count;i++)
{
if(offs<segmentends[i] || (i==count-1 && offs==segmentends[i]))
{
[self _setCurrentIndex:i];
off_t start=0;
if(currindex>0) start=segmentends[currindex-1];
[currhandle seekToFileOffset:offs-start];
return;
}
}
[self _raiseEOF];
}
-(void)seekToEndOfFile
{
[self _open];
[self _setCurrentIndex:count-1];
[currhandle seekToEndOfFile];
}
-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
[self _open];
int total=0;
for(;;)
{
off_t actual=[currhandle readAtMost:num-total toBuffer:((char *)buffer)+total];
total+=actual;
if(total==num || currindex==count-1) return total;
[self _setCurrentIndex:currindex+1];
[currhandle seekToFileOffset:0];
}
}
-(NSString *)name
{
return [[self currentHandle] name];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"%@ @ %qu segment %ld of %ld: %@",
[self class],[self offsetInFile],(long)currindex+1,(long)count,[currhandle description]];
}
-(NSInteger)numberOfSegments { return 0; }
-(off_t)segmentSizeAtIndex:(NSInteger)index { return 0; }
-(CSHandle *)handleAtIndex:(NSInteger)index { return nil; }
-(void)_open
{
if(currindex!=NSNotFound) return;
count=[self numberOfSegments];
if(count<1) [self _raiseNoSegments];
segmentends=malloc(count*sizeof(segmentends[0]));
off_t total=0;
for(NSInteger i=0;i<count-1;i++)
{
off_t size=[self segmentSizeAtIndex:i];
if(size==CSHandleMaxLength) [self _raiseSizeUnknownForSegment:i];
total+=size;
segmentends[i]=total;
}
off_t size=[self segmentSizeAtIndex:count-1];
if(size==CSHandleMaxLength) segmentends[count-1]=CSHandleMaxLength;
else segmentends[count-1]=total+size;
[self _setCurrentIndex:0];
}
-(void)_setCurrentIndex:(NSInteger)newindex
{
if(currindex!=newindex)
{
currindex=newindex;
[currhandle release];
currhandle=nil;
currhandle=[[self handleAtIndex:newindex] retain];
}
}
-(void)_raiseNoSegments
{
[NSException raise:CSNoSegmentsException format:@"No segments for CSSegmentedHandle."];
}
-(void)_raiseSizeUnknownForSegment:(NSInteger)i
{
[NSException raise:CSSizeOfSegmentUnknownException
format:@"Size of CSSegmentedHandle segment %ld (%@) unknown.",(long)i,[self handleAtIndex:i]];
}
@end