File: CSBlockStreamHandle.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (118 lines) | stat: -rw-r--r-- 2,019 bytes parent folder | download
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
#import "CSBlockStreamHandle.h"

static inline int imin(int a,int b) { return a<b?a:b; }

@implementation CSBlockStreamHandle

-(id)initWithName:(NSString *)descname length:(off_t)length
{
	if((self=[super initWithName:descname length:length]))
	{
		_currblock=NULL;
		_blockstartpos=0;
		_blocklength=0;
	}
	return self;
}

-(id)initWithHandle:(CSHandle *)handle length:(off_t)length bufferSize:(int)buffersize;
{
	if((self=[super initWithHandle:handle length:length bufferSize:buffersize]))
	{
		_currblock=NULL;
		_blockstartpos=0;
		_blocklength=0;
	}
	return self;
}

-(id)initAsCopyOf:(CSBlockStreamHandle *)other
{
	[self _raiseNotSupported:_cmd];
	return nil;
}




-(void)seekToFileOffset:(off_t)offs
{
	if(![self _prepareStreamSeekTo:offs]) return;

	if(offs<_blockstartpos) [super seekToFileOffset:0];

	while(_blockstartpos+_blocklength<offs)
	{
		[self _readNextBlock];
		if(endofstream)
		{
			if(offs==_blockstartpos) break;
			else [self _raiseEOF];
		}
	}

	streampos=offs;
}

-(void)resetStream
{
	_blockstartpos=0;
	_blocklength=0;
	_endofblocks=NO;
	[self resetBlockStream];
}

-(int)streamAtMost:(int)num toBuffer:(void *)buffer
{
	int n=0;

	if(streampos>=_blockstartpos&&streampos<_blockstartpos+_blocklength)
	{
		if(!_currblock) return 0;

		int offs=streampos-_blockstartpos;
		int count=_blocklength-offs;
		if(count>num) count=num;
		memcpy(buffer,_currblock+offs,count);
		n+=count;
	}

	while(n<num)
	{
		[self _readNextBlock];
		if(endofstream) break;

		int count=imin(_blocklength,num-n);
		memcpy(buffer+n,_currblock,count);
		n+=count;
	}

	return n;
}

-(void)_readNextBlock
{
	_blockstartpos+=_blocklength;
	if(_endofblocks) { [self endStream]; return; }
	_blocklength=[self produceBlockAtOffset:_blockstartpos];

	if(_blocklength<=0||!_currblock) [self endStream];
}

-(void)resetBlockStream { }

-(int)produceBlockAtOffset:(off_t)pos { return 0; }



-(void)setBlockPointer:(uint8_t *)blockpointer
{
	_currblock=blockpointer;
}

-(void)endBlockStream
{
	_endofblocks=YES;
}

@end