Skip to content

Instantly share code, notes, and snippets.

@khakionion
Created January 19, 2016 04:11
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 khakionion/5bf10b51aa1159dd73d2 to your computer and use it in GitHub Desktop.
Save khakionion/5bf10b51aa1159dd73d2 to your computer and use it in GitHub Desktop.
Hacky parser to build a CSV from Kallithea diff output
#import "AppDelegate.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@interface AppDelegate () <NSXMLParserDelegate>
@property (weak) IBOutlet NSWindow *window;
@property (atomic) BOOL insideFile;
@property (strong) NSString *currentFile;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL *divURL = [[NSBundle mainBundle] URLForResource:@"Example" withExtension:@"xml"];
NSXMLParser *divParser = [[NSXMLParser alloc] initWithContentsOfURL:divURL];
[divParser setDelegate:self];
[divParser parse];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {
if([elementName isEqualToString:@"a"]) {
self.insideFile = YES;
}
else if ([[elementName substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"i"]) {
NSString *action=nil;
if([attributeDict[@"class"] isEqualToString:@"icon-diff-M"]) {
action = @"modified";
}
else if([attributeDict[@"class"] isEqualToString:@"icon-diff-A"]) {
action = @"added";
}
else { //if([attributeDict[@"class"] isEqualToString:@"icon-diff-D"])
action = @"deleted";
}
self.currentFile = [NSString stringWithFormat:@"%@,",action];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName {
if([elementName isEqualToString:@"a"]) {
self.insideFile = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(self.insideFile) {
self.currentFile = [NSString stringWithFormat:@"%@%@,",self.currentFile,string];
NSLog(@"%@",self.currentFile);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment