Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OCStream * fileStream(FILE * file)
- {
- char nextChar = fgetc(file);
- if (nextChar == EOF)
- {
- fclose(file);
- return nil;
- }
- return [OCStream streamWithValue:[NSNumber numberWithChar:nextChar]
- generator:
- ^OCStream*{
- return fileStream(file);
- }];
- }
- OCStream * rleDecoder(int count, OCStream * inputStream)
- {
- if (count <= 0)
- {
- count = [[inputStream head] charValue];
- inputStream = [inputStream tail];
- }
- return [OCStream streamWithValue:[inputStream head]
- generator:^
- {
- OCStream * tmpStream = inputStream;
- if (count == 1)
- tmpStream = [tmpStream tail];
- return rleDecoder(count - 1, tmpStream);
- }];
- }
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- FILE * test = fopen("test.bin", "rb");
- OCStream * rle = rleDecoder(0, fileStream(test));
- for(NSNumber * num in [rle enumerator])
- printf("%c", [num charValue]);
- [pool drain];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement