Skip to content

Instantly share code, notes, and snippets.

@davepeck
Created April 27, 2009 04:35
Show Gist options
  • Select an option

  • Save davepeck/102331 to your computer and use it in GitHub Desktop.

Select an option

Save davepeck/102331 to your computer and use it in GitHub Desktop.
Two simple Objective-C classes that make it crazy easy to read data from sequential binary files.
//
// BinaryDataScanner.m
//
// Copyright 2009 Dave Peck <davepeck [at] davepeck [dot] org>. All rights reserved.
// http://davepeck.org/
//
// This class makes it quite a bit easier to read sequential binary files in Objective-C.
//
// This code is released under the BSD license. If you use it in your product, please
// let me know and, if possible, please put me in your credits.
//
#import <UIKit/UIKit.h>
@interface BinaryDataScanner : NSObject {
BOOL littleEndian;
NSStringEncoding encoding;
NSData *data;
const uint8_t *current;
NSUInteger scanRemain;
}
+(id)binaryDataScannerWithData:(NSData*)data littleEndian:(BOOL)littleEndian defaultEncoding:(NSStringEncoding)defaultEncoding;
-(NSUInteger) remainingBytes;
-(const uint8_t *) currentPointer;
-(void) skipBytes:(NSUInteger)count;
-(uint8_t) readByte;
-(uint16_t) readWord;
-(uint32_t) readDoubleWord;
-(NSString*) readNullTerminatedString;
-(NSString*) readNullTerminatedStringWithEncoding:(NSStringEncoding)overrideEncoding;
-(NSString*) readStringUntilDelimiter:(uint8_t)delim;
-(NSString*) readStringUntilDelimiter:(uint8_t)delim encoding:(NSStringEncoding)overrideEncoding;
-(NSString*) readStringOfLength:(NSUInteger)count handleNullTerminatorAfter:(BOOL)handleNull;
-(NSString*) readStringOfLength:(NSUInteger)count handleNullTerminatorAfter:(BOOL)handleNull encoding:(NSStringEncoding)overrideEncoding;
-(NSArray*) readArrayOfNullTerminatedStrings:(NSUInteger)count;
-(NSArray*) readArrayOfNullTerminatedStrings:(NSUInteger)count encoding:(NSStringEncoding)overrideEncoding;
@end
@2011FallUConnCSE2102
Copy link

Hi, I"m a newb to Objective C, and am writing for Mac. It seems that UIKit is for iOS...

@davepeck
Copy link
Author

Yeah that UIKit import probably doesn't need to be there.

@codrutaritivoiu19
Copy link

Hi, can you please tell me on what version of the bsd license is the code released?
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment