Skip to content

Instantly share code, notes, and snippets.

@photofroggy
Last active August 29, 2015 14:00
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 photofroggy/25961f96906458ca4706 to your computer and use it in GitHub Desktop.
Save photofroggy/25961f96906458ca4706 to your computer and use it in GitHub Desktop.
Lol wtf
//
// PFYPacket.m
// dAmnGDCAsyncTest
//
// Created by Henry on 05/05/2014.
// Copyright (c) 2014 dAmnLab. All rights reserved.
//
#import "PFYPacket.h"
@implementation PFYPacket
@synthesize cmd;
@synthesize param;
@synthesize arguments;
@synthesize body;
@synthesize sub;
@synthesize raw;
+ (id) createFromString:(NSString *)pkt {
return [[PFYPacket alloc] initWithString:pkt];
}
+ (id) createFromNSData:(NSData *)pkt {
return [PFYPacket createFromString:[[NSString alloc] initWithData:pkt encoding:NSUTF8StringEncoding]];
}
- (id) init {
return [self initWithString:@""];
}
- (id) initWithString:(NSString *) pkt {
self = [super init];
if(self) {
self.cmd = @"";
self.param = @"";
self.arguments = [NSMutableDictionary new];
self.body = @"";
self.sub = [NSMutableArray new];
[self parse:pkt];
}
return self;
}
- (id) parse: (NSString *) pkt {
// Remove the trailing \0
self.raw = [pkt componentsSeparatedByString:@"\0"][0];
// Separate header from body
NSArray *buf = [self.raw componentsSeparatedByString:@"\n\n"];
NSString *head = buf[0];
buf = [buf subarrayWithRange:NSMakeRange(1, [buf count] - 1)];
self.body = [buf componentsJoinedByString:@"\n\n"];
// Split the header
NSArray *hbuf = [head componentsSeparatedByString:@"\n"];
NSArray *clbuf = [hbuf[0] componentsSeparatedByString:@" "];
hbuf = [hbuf subarrayWithRange:NSMakeRange(1, [hbuf count] - 1)];
NSUInteger cllen = [clbuf count];
// Command
if(cllen > 0)
self.cmd = clbuf[0];
// Param
if(cllen > 1)
self.param = [[clbuf subarrayWithRange:NSMakeRange(1, cllen - 1)] componentsJoinedByString:@" "];
// Arguments
NSRange sepr;
for(NSString *argline in hbuf) {
sepr = [argline rangeOfString:@"="];
if(sepr.location == NSNotFound)
continue;
[self.arguments
setValue:[argline substringFromIndex:sepr.location + sepr.length]
forKey:[argline substringToIndex:sepr.location]
];
}
// Sub packets.
for(NSString *sstr in buf) {
[self.sub addObject:[PFYPacket createFromString:sstr]];
}
return self;
}
- (id) argument:(NSString *)key {
return [self.arguments objectForKey:key];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment