Created
June 19, 2011 05:07
-
-
Save aaronpearce/1033778 to your computer and use it in GitHub Desktop.
Packet Parser
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
-(NSDictionary *)parser:(NSString *)packet:(NSInteger)depth { | |
NSMutableArray *partsArr = [NSMutableArray arrayWithArray:[packet componentsSeparatedByString:@"\n\n"]]; // Split by \n\n | |
NSMutableArray *headArr = [NSMutableArray arrayWithArray:[[partsArr objectAtIndex:0] componentsSeparatedByString:@"\n"]]; // Split by \n | |
[partsArr removeObjectAtIndex:0]; | |
NSMutableArray *cmdArr = [NSMutableArray arrayWithArray:[[headArr objectAtIndex:0] componentsSeparatedByString:@" "]]; | |
[headArr removeObjectAtIndex:0]; | |
NSString *cmd = [cmdArr objectAtIndex:0]; | |
[cmdArr removeObjectAtIndex:0]; | |
NSString *param = [cmdArr componentsJoinedByString:@" "]; | |
NSDictionary *args = [[NSMutableDictionary alloc] init]; | |
for (NSInteger i=0; i < [headArr count]; i++) { | |
NSMutableArray *valArr = [NSMutableArray arrayWithArray:[[headArr objectAtIndex:i] componentsSeparatedByString:@"="]]; | |
NSString *key = [valArr objectAtIndex:0]; | |
[valArr removeObjectAtIndex:0]; | |
NSString *value = [valArr componentsJoinedByString:@"="]; | |
[args setValue:value forKey:key]; | |
} | |
NSString *body = [partsArr componentsJoinedByString:@"\n\n"]; | |
NSMutableArray *subArr = [[NSMutableArray alloc] init]; | |
if ([partsArr count] >= 1) { | |
NSInteger j = [partsArr count]-1; | |
if(j == 1) { | |
[subArr addObject:[self parser:body :depth+1]]; | |
} else { | |
for (j; j>= 0; j--) { | |
[subArr addObject:[self parser:[partsArr objectAtIndex:j] :depth+1]]; | |
} | |
} | |
} | |
NSDictionary *packetDict = [[NSMutableDictionary alloc] init]; | |
[packetDict setValue:cmd forKey:@"cmd"]; | |
[packetDict setValue:param forKey:@"param"]; | |
[packetDict setValue:args forKey:@"args"]; | |
[packetDict setValue:body forKey:@"body"]; | |
[packetDict setValue:subArr forKey:@"sub"]; | |
[packetDict setValue:packet forKey:@"raw"]; | |
[args release]; | |
[subArr release]; | |
return packetDict; | |
[cmd release]; | |
[param release]; | |
[body release]; | |
[packet release]; | |
[packetDict release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment