Skip to content

Instantly share code, notes, and snippets.

@sentient06
Created November 18, 2013 13:23
Show Gist options
  • Save sentient06/7527634 to your computer and use it in GitHub Desktop.
Save sentient06/7527634 to your computer and use it in GitHub Desktop.
New World ROM alternative checking
//
// main.m
// StringsGrep
//
// Created by Giancarlo Mariot on 18/11/2013.
// Copyright (c) 2013 Giancarlo Mariot. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSTask * task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/strings"];
//strings /Users/gian/Classic/ROMs/New World ROM/1998-07-21 - Mac OS ROM 1.1.rom
//| grep -A1 "MacOSROMFile-version"
//@"MacROM for CHRP 1.1." // All 1.1 versions
//@"MacROM for NewWorld." // All 1.x versions have no version definition
//@"MacOSROMFile-version" // Following line has the version
// /<DESCRIPTION>\n(.*)\n<\/DESCRIPTION>/
// /MacOSROMFile-version\n(.*)/
// NSArray * arguments = [NSArray arrayWithObjects: @"/Users/gian/Classic/ROMs/New World ROM/1998-07-21 - Mac OS ROM 1.1.rom", nil];
// NSArray * arguments = [NSArray arrayWithObjects: @"/Users/gian/Classic/ROMs/New World ROM/1998-12-03 - Mac OS ROM 1.2.rom", nil];
NSArray * arguments = [NSArray arrayWithObjects: @"/Users/gian/Classic/ROMs/New World ROM/1999-09-17 - Mac OS ROM 2.5.1.rom", nil];
[task setArguments: arguments];
NSPipe * pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle * file = [pipe fileHandleForReading];
[task launch];
NSData * data = [file readDataToEndOfFile];
NSString * string = [[NSString alloc]
initWithData: data
encoding: NSUTF8StringEncoding
];
NSError * error = NULL;
NSRegularExpression * regexDescription = [NSRegularExpression
regularExpressionWithPattern:@"<DESCRIPTION>\n(.*)\n</DESCRIPTION>"
options:0
error:&error
];
NSTextCheckingResult * matchDescription = [
regexDescription firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])
];
if (matchDescription) {
NSRange firstHalfRange = [matchDescription rangeAtIndex:1];
// NSLog(@"Match desc:\n%@", [string substringWithRange:firstHalfRange]);
if ([[string substringWithRange:firstHalfRange] isEqualToString:@"MacROM for NewWorld."]) {
NSRegularExpression * regexVersion = [NSRegularExpression
regularExpressionWithPattern:@"MacOSROMFile-version\n(.*)"
options:0
error:&error
];
NSTextCheckingResult * matchVersion = [
regexVersion firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])
];
if (matchVersion) {
NSRange firstHalfRangeVersion = [matchVersion rangeAtIndex:1];
NSLog(@"Matched version: %@", [string substringWithRange:firstHalfRangeVersion]);
} else {
NSLog(@"Between versions 1.2 and 2.");
}
} else
if ([[string substringWithRange:firstHalfRange] isEqualToString:@"MacROM for CHRP 1.1."]) {
NSLog(@"Version 1.1 or 1.1.2");
}
} else {
NSLog(@"File not identified.");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment