Skip to content

Instantly share code, notes, and snippets.

@ThanosSiopoudis
Last active December 20, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThanosSiopoudis/7bf208b2c3fb8a3265c3 to your computer and use it in GitHub Desktop.
Save ThanosSiopoudis/7bf208b2c3fb8a3265c3 to your computer and use it in GitHub Desktop.
A winetricks parser for wineskin (courtesy of Barrel)
NSMutableArray *winetricksForPlist = [[NSMutableArray alloc] init];
// Winetricks has been downloaded, so proceed with parsing it
[downloadAlert close];
//create file handle and load the contents in the memory
NSFileHandle *file;
file = [NSFileHandle fileHandleForReadingAtPath:resultPath];
//read data into file in NSData format
NSData *filedata;
filedata = [file readDataToEndOfFile];
//convert NSData to NSString
NSString *string;
string = [[NSString alloc] initWithData:filedata encoding:NSASCIIStringEncoding];
// Look for the word "w_metadata " in the cached file in memory
NSUInteger length = [string length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound) {
range = [string rangeOfString: @"w_metadata " options:0 range:range];
if(range.location != NSNotFound)
{
NSRange outerRange = NSMakeRange(range.location + range.length, length - (range.location + range.length));
// Found an occurence. Make sure it's an entry
// 1st: Get the two characters before the entry
// to make sure they are both newlines
NSString *m2 = [string substringWithRange:NSMakeRange(range.location - 2, 1)];
NSString *m1 = [string substringWithRange:NSMakeRange(range.location - 1, 1)];
if (([m2 isEqualToString:@"\n"] || [m2 isEqualToString:@"\""]) && [m1 isEqualToString:@"\n"]) {
// 2: It's an entry. Parse it
// 2.1: Get the whole line
NSRange lineRange = [string rangeOfString:@"\n" options:0 range:outerRange];
// 2.2: Calculate the line range
lineRange = NSMakeRange(range.location, lineRange.location - range.location);
// 2.3: Split the space seperated string in an array
NSArray *winetricksComponents = [[string substringWithRange:lineRange] componentsSeparatedByString:@" "];
// Malformatted file workaround:
// There seems to be an extra space so detect it and ignore it
NSMutableDictionary *entry = [[NSMutableDictionary alloc] init];
if ([winetricksComponents count] > 0) {
if ([(NSString *)[winetricksComponents objectAtIndex:1] length] == 0) {
[entry setObject:[winetricksComponents objectAtIndex:2] forKey:@"winetrick"];
[entry setObject:[winetricksComponents objectAtIndex:3] forKey:@"category"];
}
else {
[entry setObject:[winetricksComponents objectAtIndex:1] forKey:@"winetrick"];
[entry setObject:[winetricksComponents objectAtIndex:2] forKey:@"category"];
}
}
// 2.4: Find and parse the winetrick title
NSRange titleRange = [string rangeOfString:@"title=\"" options:0 range:outerRange];
NSRange endTitleRange = [string rangeOfString:@"\"" options:0 range:NSMakeRange(titleRange.location + 7, length - (titleRange.location + 7))];
// Now read the title in the range
NSString *title = [string substringWithRange:NSMakeRange(titleRange.location + 7, (endTitleRange.location - (titleRange.location + 7)))];
[entry setObject:title forKey:@"title"];
[winetricksForPlist addObject:entry];
}
NSLog(@"%lu", (unsigned long)range.location);
// Advance the range
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment