Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes, removed 2 compiler warnings and code cleanup #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 18 additions & 42 deletions TBXML-Code/TBXML.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ - (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtensio
return self;
}

- (int) decodeData:(NSData*)data {
- (NSInteger) decodeData:(NSData*)data {
NSError *error = nil;
return [self decodeData:data withError:&error];
}

- (int) decodeData:(NSData*)data withError:(NSError **)error {
- (NSInteger) decodeData:(NSData*)data withError:(NSError **)error {

NSError *localError = nil;

Expand Down Expand Up @@ -256,8 +256,7 @@ - (int) decodeData:(NSData*)data withError:(NSError **)error {
@implementation TBXML (StaticFunctions)

+ (NSString*) elementName:(TBXMLElement*)aXMLElement {
if (nil == aXMLElement->name) return @"";
return [NSString stringWithCString:&aXMLElement->name[0] encoding:NSUTF8StringEncoding];
return [self elementName:aXMLElement error:nil];
}

+ (NSString*) elementName:(TBXMLElement*)aXMLElement error:(NSError **)error {
Expand All @@ -277,8 +276,7 @@ + (NSString*) elementName:(TBXMLElement*)aXMLElement error:(NSError **)error {
}

+ (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute {
if (nil == aXMLAttribute->name) return @"";
return [NSString stringWithCString:&aXMLAttribute->name[0] encoding:NSUTF8StringEncoding];
return [self attributeName:aXMLAttribute error:nil];
}

+ (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute error:(NSError **)error {
Expand All @@ -299,8 +297,7 @@ + (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute error:(NSError **)err


+ (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute {
if (nil == aXMLAttribute->value) return @"";
return [NSString stringWithCString:&aXMLAttribute->value[0] encoding:NSUTF8StringEncoding];
return [self attributeValue:aXMLAttribute error:nil];
}

+ (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute error:(NSError **)error {
Expand All @@ -310,12 +307,17 @@ + (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute error:(NSError **)er
return @"";
}

// check for nil attribute value
if (nil == aXMLAttribute->value) {
if (error) *error = [TBXML errorWithCode:D_TBXML_ATTRIBUTE_VALUE_IS_NIL];
return @"";
}

return [NSString stringWithCString:&aXMLAttribute->value[0] encoding:NSUTF8StringEncoding];
}

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
if (nil == aXMLElement->text) return @"";
return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
return [self textForElement:aXMLElement error:nil];
}

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement error:(NSError **)error {
Expand All @@ -335,17 +337,7 @@ + (NSString*) textForElement:(TBXMLElement*)aXMLElement error:(NSError **)error
}

+ (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)aXMLElement {
const char * name = [aName cStringUsingEncoding:NSUTF8StringEncoding];
NSString * value = nil;
TBXMLAttribute * attribute = aXMLElement->firstAttribute;
while (attribute) {
if (strlen(attribute->name) == strlen(name) && memcmp(attribute->name,name,strlen(name)) == 0) {
value = [NSString stringWithCString:&attribute->value[0] encoding:NSUTF8StringEncoding];
break;
}
attribute = attribute->next;
}
return value;
return [self valueOfAttributeNamed:aName forElement:aXMLElement error:nil];
}

+ (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)aXMLElement error:(NSError **)error {
Expand All @@ -370,7 +362,7 @@ + (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)
if (attribute->value[0])
value = [NSString stringWithCString:&attribute->value[0] encoding:NSUTF8StringEncoding];
else
value = [NSString stringWithString:@""];
value = @"";

break;
}
Expand All @@ -386,17 +378,8 @@ + (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)
return value;
}

+ (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement{

TBXMLElement * xmlElement = aParentXMLElement->firstChild;
const char * name = [aName cStringUsingEncoding:NSUTF8StringEncoding];
while (xmlElement) {
if (strlen(xmlElement->name) == strlen(name) && memcmp(xmlElement->name,name,strlen(name)) == 0) {
return xmlElement;
}
xmlElement = xmlElement->nextSibling;
}
return nil;
+ (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement {
return [self childElementNamed:aName parentElement:aParentXMLElement error:nil];
}

+ (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement error:(NSError **)error {
Expand Down Expand Up @@ -427,15 +410,7 @@ + (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement
}

+ (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElement*)aXMLElement{
TBXMLElement * xmlElement = aXMLElement->nextSibling;
const char * name = [aName cStringUsingEncoding:NSUTF8StringEncoding];
while (xmlElement) {
if (strlen(xmlElement->name) == strlen(name) && memcmp(xmlElement->name,name,strlen(name)) == 0) {
return xmlElement;
}
xmlElement = xmlElement->nextSibling;
}
return nil;
return [self nextSiblingNamed:aName searchFromElement:aXMLElement error:nil];
}

+ (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElement*)aXMLElement error:(NSError **)error {
Expand Down Expand Up @@ -550,6 +525,7 @@ + (NSString *) errorTextForCode:(int)code {
case D_TBXML_ELEMENT_NAME_IS_NIL: codeText = @"Element name is nil"; break;
case D_TBXML_ATTRIBUTE_IS_NIL: codeText = @"Attribute is nil"; break;
case D_TBXML_ATTRIBUTE_NAME_IS_NIL: codeText = @"Attribute name is nil"; break;
case D_TBXML_ATTRIBUTE_VALUE_IS_NIL: codeText = @"Attribute value is nil"; break;
case D_TBXML_ELEMENT_TEXT_IS_NIL: codeText = @"Element text is nil"; break;
case D_TBXML_PARAM_NAME_IS_NIL: codeText = @"Parameter name is nil"; break;
case D_TBXML_ATTRIBUTE_NOT_FOUND: codeText = @"Attribute not found"; break;
Expand Down
5 changes: 3 additions & 2 deletions TBXML-Headers/TBXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum TBXMLErrorCodes {
D_TBXML_ELEMENT_TEXT_IS_NIL,
D_TBXML_ATTRIBUTE_IS_NIL,
D_TBXML_ATTRIBUTE_NAME_IS_NIL,
D_TBXML_ATTRIBUTE_VALUE_IS_NIL,
D_TBXML_ATTRIBUTE_NOT_FOUND,
D_TBXML_PARAM_NAME_IS_NIL
};
Expand Down Expand Up @@ -170,8 +171,8 @@ typedef void (^TBXMLIterateAttributeBlock)(TBXMLAttribute *attribute, NSString *
- (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtension __attribute__((deprecated));


- (int) decodeData:(NSData*)data;
- (int) decodeData:(NSData*)data withError:(NSError **)error;
- (NSInteger) decodeData:(NSData*)data;
- (NSInteger) decodeData:(NSData*)data withError:(NSError **)error;

@end

Expand Down