Skip to content

Instantly share code, notes, and snippets.

@BenjaminPoulain
Created September 1, 2011 16:53
Show Gist options
  • Save BenjaminPoulain/1186636 to your computer and use it in GitHub Desktop.
Save BenjaminPoulain/1186636 to your computer and use it in GitHub Desktop.
From fe302fd2dbf41058eb745541d7c971716075b13b Mon Sep 17 00:00:00 2001
From: Benjamin Poulain <benjamin@webkit.org>
Date: Thu, 1 Sep 2011 18:52:03 +0200
Subject: [PATCH] Add parsing for the FormatVersion and the Creator lines.
The first two lines of a callgrind files are the optional lines for
the FormatVersion and Creator.
---
Callgrind Viewer/FileLoader.h | 6 ++++
Callgrind Viewer/FileLoader.m | 62 +++++++++++++++++++++++++++++++++++++++-
2 files changed, 66 insertions(+), 2 deletions(-)
diff --git a/Callgrind Viewer/FileLoader.h b/Callgrind Viewer/FileLoader.h
index 05080df..e82134e 100644
--- a/Callgrind Viewer/FileLoader.h
+++ b/Callgrind Viewer/FileLoader.h
@@ -24,6 +24,12 @@
__block dispatch_io_t _ioChannel;
NSMutableData *_pendingDataBuffer;
+ enum {
+ FormatVersion,
+ Creator,
+ Header,
+ Body,
+ } _readingStage;
}
- (id)initWithURL:(NSURL *)absoluteURL;
diff --git a/Callgrind Viewer/FileLoader.m b/Callgrind Viewer/FileLoader.m
index 2a6c906..3dc57e3 100644
--- a/Callgrind Viewer/FileLoader.m
+++ b/Callgrind Viewer/FileLoader.m
@@ -34,10 +34,67 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
return -1;
}
+- (BOOL)processBodyLine:(NSString *)string
+{
+ return YES;
+}
+
+- (BOOL)processHeaderLine:(NSString *)string
+{
+ return YES;
+}
+
+- (BOOL)processCreatorLine:(NSString *)string
+{
+ NSError *error = 0;
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^creator:.*$"
+ options:0
+ error:&error];
+ assert(!error);
+ NSUInteger matches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
+
+ assert(matches == 0 || matches == 1);
+ _readingStage = Header;
+ if (matches)
+ return YES;
+ else {
+ // The creator line is optional, process next stage.
+ return [self processHeaderLine:string];
+ }
+}
+
+- (BOOL)processFormatVersionLine:(NSString *)string
+{
+ NSError *error = 0;
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^version:[ \t]*(0x[a-f0-9]+|\\d+)$"
+ options:0
+ error:&error];
+ assert(!error);
+ NSUInteger matches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
+
+ assert(matches == 0 || matches == 1);
+ _readingStage = Creator;
+ if (matches)
+ return YES;
+ else {
+ // The version line is optional, process next stage.
+ return [self processCreatorLine:string];
+ }
+}
+
- (BOOL)processLine:(const char*)data size: (size_t) size
{
- // FIXME: on invalid file format: return NO;
- // FIXME: parse the line.
+ NSString *string = [[[NSString alloc] initWithBytesNoCopy:(void *)data length:size encoding:NSASCIIStringEncoding freeWhenDone:NO] autorelease];
+ switch (_readingStage) {
+ case FormatVersion:
+ return [self processFormatVersionLine:string];
+ case Creator:
+ return [self processCreatorLine:string];
+ case Header:
+ return [self processHeaderLine:string];
+ case Body:
+ return [self processBodyLine:string];
+ }
return YES;
}
@@ -90,6 +147,7 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si
self = [super init];
if (self) {
_pendingDataBuffer = [[NSMutableData alloc] initWithCapacity:0];
+ _readingStage = FormatVersion;
assert([absoluteURL isFileURL]);
NSString* filePath = [absoluteURL path];
--
1.7.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment