Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Yanarella committed Jul 4, 2012
1 parent 96a432e commit 4a57c90
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 3 deletions.
44 changes: 44 additions & 0 deletions CCMultipartReader.h
@@ -0,0 +1,44 @@
//
// CCMultipartReader.h
// CCMultipartReader
//
// Copyright 2011 CodeCatalyst, LLC. All rights reserved.
//

#import <Foundation/Foundation.h>

#if defined __cplusplus
class MultipartReader;
#else
typedef struct MultipartReader MultipartReader;
#endif

@protocol CCMultipartReaderDelegate;

@interface CCMultipartReader : NSObject {
MultipartReader *reader;
}

@property (nonatomic, assign) id<CCMultipartReaderDelegate> delegate;

@property (nonatomic, copy) NSString *boundary;
@property (nonatomic, readonly, copy) NSString *errorMessage;
@property (nonatomic, readonly, assign) BOOL hasError;
@property (nonatomic, readonly, assign) BOOL stopped;
@property (nonatomic, readonly, assign) BOOL succeeded;

- (void)reset;
- (size_t)read:(NSData*)buffer;

@end

@protocol CCMultipartReaderDelegate <NSObject>

@optional
- (void)readerDidBeginPart:(CCMultipartReader *)reader headers:(NSDictionary *)headers;
- (void)reader:(CCMultipartReader *)reader didReadPartData:(NSData *)data;
- (void)readerDidEndPart:(CCMultipartReader *)reader;
- (void)readerDidFinishReading:(CCMultipartReader *)reader;

@end

138 changes: 138 additions & 0 deletions CCMultipartReader.mm
@@ -0,0 +1,138 @@
//
// CCMultipartReader.mm
// CCMultipartReader
//
// Copyright 2011 CodeCatalyst, LLC. All rights reserved.
//

#import "CCMultipartReader.h"

#include "MultipartReader.h"

@implementation CCMultipartReader

@synthesize delegate = delegate_;

@synthesize boundary = boundary_;

#pragma mark - Callback functions

void onPartBegin(const MultipartHeaders &headers, void *userData)
{
CCMultipartReader *reader = (CCMultipartReader *)userData;

NSMutableDictionary *headerDictionary = [[NSMutableDictionary alloc] init];
MultipartHeaders::const_iterator it;
for (it = headers.begin(); it != headers.end(); it++) {
NSString *key = [NSString stringWithCString:it->first.c_str() encoding:NSUTF8StringEncoding];
NSString *value = [NSString stringWithCString:it->second.c_str() encoding:NSUTF8StringEncoding];

[headerDictionary setValue:value forKey:key];
}

if (reader.delegate && [reader.delegate respondsToSelector:@selector(readerDidBeginPart:headers:)])
[reader.delegate readerDidBeginPart:reader headers:headerDictionary];

[headerDictionary release];
}

void onPartData(const char *buffer, size_t size, void *userData)
{
CCMultipartReader *reader = (CCMultipartReader *)userData;

NSData *data = [[NSData alloc] initWithBytesNoCopy:(void *)buffer length:size freeWhenDone:NO];

if (reader.delegate && [reader.delegate respondsToSelector:@selector(reader:didReadPartData:)])
[reader.delegate reader:reader didReadPartData:data];

[data release];
}

void onPartEnd(void *userData)
{
CCMultipartReader *reader = (CCMultipartReader *)userData;

if (reader.delegate && [reader.delegate respondsToSelector:@selector(readerDidEndPart:)])
[reader.delegate readerDidEndPart:reader];
}

void onEnd(void *userData)
{
CCMultipartReader *reader = (CCMultipartReader *)userData;

if (reader.delegate && [reader.delegate respondsToSelector:@selector(readerDidFinishReading:)])
[reader.delegate readerDidFinishReading:reader];
}

#pragma mark - Initialization

- (id)init {
self = [super init];
if (self) {
reader = new MultipartReader();
reader->onPartBegin = onPartBegin;
reader->onPartData = onPartData;
reader->onPartEnd = onPartEnd;
reader->onEnd = onEnd;
reader->userData = self;
}
return self;
}

#pragma mark - Properties

- (void)setBoundary:(NSString *)value
{
if (boundary_ != value)
{
[boundary_ release];
boundary_ = [value copy];

reader->setBoundary([value UTF8String]);
}
}

- (NSString*)errorMessage
{
return [NSString stringWithCString:reader->getErrorMessage() encoding:NSUTF8StringEncoding];
}

- (BOOL)hasError
{
return reader->hasError();
}

- (BOOL)stopped
{
return reader->stopped();
}

- (BOOL)succeeded
{
return reader->succeeded();
}

#pragma mark - Methods

- (void)reset
{
reader->reset();
}

- (size_t)read:(NSData*)data
{
return reader->feed((const char*)[data bytes], [data length]);
}

#pragma mark - Memory Management

- (void)dealloc
{
[boundary_ release];

delete reader;

[super dealloc];
}

@end
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2011 CodeCatalyst, LLC

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 14 additions & 3 deletions README.md
@@ -1,4 +1,15 @@
CCMultipartReader
=================
# CCMultipartReader

An Objective C wrapper for multipart-parser, Hongli Lai's C++ implementation of a simple and efficient multipart MIME message parser.
An Objective C wrapper for (multipart-parser)[https://github.com/FooBarWidget/multipart-parser], Hongli Lai's C++ implementation of a simple and efficient multipart MIME message parser.

*NOTE*: This project only includes the wrapper class. Be sure to also grab (multipart-parser)[https://github.com/FooBarWidget/multipart-parser] from its official repository.

# License

Copyright (c) 2011 [CodeCatalyst, LLC](http://www.codecatalyst.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 4a57c90

Please sign in to comment.