Created
September 3, 2011 14:39
-
-
Save BenjaminPoulain/1191276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From a3ca019fd9300883d5e56beac98150cb1bcfff6e Mon Sep 17 00:00:00 2001 | |
From: Benjamin Poulain <[email protected]> | |
Date: Sat, 3 Sep 2011 16:09:02 +0200 | |
Subject: [PATCH] Matches all the statements that can be present in the | |
header. | |
--- | |
Callgrind Viewer/FileLoader.m | 70 ++++++++++++++++++++++++++++++++++++++-- | |
1 files changed, 66 insertions(+), 4 deletions(-) | |
diff --git a/Callgrind Viewer/FileLoader.m b/Callgrind Viewer/FileLoader.m | |
index 43a7d0f..557e077 100644 | |
--- a/Callgrind Viewer/FileLoader.m | |
+++ b/Callgrind Viewer/FileLoader.m | |
@@ -40,6 +40,21 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si | |
return YES; | |
} | |
+static inline bool regexMatchesString(NSString *regexpString, NSString *string) | |
+{ | |
+ NSError *error = 0; | |
+ NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:regexpString | |
+ options:0 | |
+ error:&error]; | |
+ assert(!error); | |
+ NSUInteger matches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])]; | |
+ [regex release]; | |
+ regex = nil; | |
+ | |
+ assert(matches == 0 || matches == 1); | |
+ return !!matches; | |
+} | |
+ | |
- (BOOL)processHeaderLine:(NSString *)string | |
{ | |
if (![string length]) | |
@@ -64,8 +79,53 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si | |
} | |
} | |
- // FIXME: parse the remaining headers. | |
- return YES; | |
+ // TargetID (pid, thread or part) | |
+ if (regexMatchesString(@"^(?:pid|thread|part):[ \t]*(?:0x[a-fA-F0-9]+|\\d+)$", string)) | |
+ return YES; | |
+ | |
+ // Description | |
+ if (regexMatchesString(@"^desc:[ \t]*(?:[a-zA-Z][A-Za-z0-9 ]*)[ \t]*:.*$", string)) | |
+ return YES; | |
+ | |
+ { // CostLineDef::events | |
+ NSError *error = 0; | |
+ NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"^events:[ \t]*((?:[a-zA-Z][a-zA-Z0-9]*)([ \t]+(?:[a-zA-Z][a-zA-Z0-9]*))*)$" | |
+ options:0 | |
+ error:&error]; | |
+ assert(!error); | |
+ NSTextCheckingResult *match = [regex firstMatchInString: string options: 0 range:NSMakeRange(0, [string length])]; | |
+ [regex release]; | |
+ regex = nil; | |
+ | |
+ if (match) { | |
+ // FIME: Store to values to parse the file. | |
+ return YES; | |
+ } | |
+ } | |
+ | |
+ { // CostLineDef::position | |
+ NSError *error = 0; | |
+ NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"^positions:[\t ]*(instr)?([\t ]+line)?$" | |
+ options:0 | |
+ error:&error]; | |
+ assert(!error); | |
+ NSTextCheckingResult *match = [regex firstMatchInString: string options: 0 range:NSMakeRange(0, [string length])]; | |
+ [regex release]; | |
+ regex = nil; | |
+ | |
+ if (match) { | |
+ // FIME: Store to values to parse the file. | |
+ return YES; | |
+ } | |
+ } | |
+ | |
+ // Summary (not in grammar but appear in practice) | |
+ if (regexMatchesString(@"^summary:[\t ]*(?:0x[a-fA-F0-9]+|\\d+)([\t ]+(?:0x[a-fA-F0-9]+|\\d+))*$", string)) | |
+ return YES; | |
+ | |
+ // FIXME: process body. | |
+ NSLog(@"Invalid string = \"%@\"", string); | |
+ return NO; | |
} | |
- (BOOL)processCreatorLine:(NSString *)string | |
@@ -135,7 +195,8 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si | |
if (nextNewLine >= 0) { | |
assert(nextNewLine <= size); | |
[_pendingDataBuffer appendBytes:data length:nextNewLine]; | |
- [self processLine:[_pendingDataBuffer bytes] size:[_pendingDataBuffer length]]; | |
+ if (![self processLine:[_pendingDataBuffer bytes] size:[_pendingDataBuffer length]]) | |
+ return NO; | |
workOffset = nextNewLine + 1; | |
[_pendingDataBuffer setLength:0]; | |
} else { | |
@@ -154,7 +215,8 @@ static inline ssize_t indexOfNextNewLineChar(const char* data, size_t offset, si | |
if (nextNewLine >= 0) { | |
assert(workOffset < size); | |
assert(nextNewLine < size); | |
- [self processLine: (data + workOffset) size:(nextNewLine - workOffset)]; | |
+ if (![self processLine: (data + workOffset) size:(nextNewLine - workOffset)]) | |
+ return NO; | |
workOffset = nextNewLine + 1; | |
} else { | |
assert(workOffset <= size); | |
-- | |
1.7.4.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment