Advertisement
Guest User

Untitled

a guest
May 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. OCStream * fileStream(FILE * file)
  2. {
  3.     char nextChar = fgetc(file);
  4.     if (nextChar == EOF)
  5.     {
  6.         fclose(file);
  7.         return nil;
  8.     }
  9.     return [OCStream streamWithValue:[NSNumber numberWithChar:nextChar]
  10.                            generator:
  11.                             ^OCStream*{
  12.                                     return fileStream(file);
  13.                             }];
  14. }
  15.  
  16. OCStream * rleDecoder(int count, OCStream * inputStream)
  17. {
  18.     if (count <= 0)
  19.     {
  20.         count = [[inputStream head] charValue];
  21.         inputStream = [inputStream tail];
  22.     }
  23.    
  24.     return [OCStream streamWithValue:[inputStream head]
  25.                            generator:^
  26.             {
  27.                 OCStream * tmpStream = inputStream;
  28.                 if (count == 1)
  29.                     tmpStream = [tmpStream tail];
  30.                    
  31.                 return rleDecoder(count - 1, tmpStream);
  32.             }];
  33. }
  34.  
  35. int main (int argc, const char * argv[]) {
  36.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  37.  
  38.     FILE * test = fopen("test.bin", "rb");
  39.    
  40.     OCStream * rle = rleDecoder(0, fileStream(test));
  41.     for(NSNumber * num in [rle enumerator])
  42.         printf("%c", [num charValue]);
  43.    
  44.     [pool drain];
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement