@@ -36,7 +36,7 @@ + (NSString *) errorTextForCode:(int)code;
36
36
+ (NSError *) errorWithCode : (int )code ;
37
37
+ (NSError *) errorWithCode : (int )code userInfo : (NSDictionary *)userInfo ;
38
38
- (void ) decodeBytes ;
39
- - (void ) allocateBytesOfLength : (long )length error : (NSError **)error ;
39
+ - (int ) allocateBytesOfLength : (long )length error : (NSError **)error ;
40
40
- (TBXMLElement*) nextAvailableElement ;
41
41
- (TBXMLAttribute*) nextAvailableAttribute ;
42
42
@end
@@ -48,35 +48,35 @@ @implementation TBXML
48
48
49
49
@synthesize rootXMLElement;
50
50
51
- + (id )tbxmlWithXMLString : (NSString *)aXMLString {
51
+ + (id )newTBXMLWithXMLString : (NSString *)aXMLString {
52
52
return [[TBXML alloc ] initWithXMLString: aXMLString];
53
53
}
54
54
55
- + (id )tbxmlWithXMLString : (NSString *)aXMLString error : (NSError *__autoreleasing *)error {
55
+ + (id )newTBXMLWithXMLString : (NSString *)aXMLString error : (NSError *__autoreleasing *)error {
56
56
return [[TBXML alloc ] initWithXMLString: aXMLString error: error];
57
57
}
58
58
59
- + (id )tbxmlWithXMLData : (NSData *)aData {
59
+ + (id )newTBXMLWithXMLData : (NSData *)aData {
60
60
return [[TBXML alloc ] initWithXMLData: aData];
61
61
}
62
62
63
- + (id )tbxmlWithXMLData : (NSData *)aData error : (NSError *__autoreleasing *)error {
63
+ + (id )newTBXMLWithXMLData : (NSData *)aData error : (NSError *__autoreleasing *)error {
64
64
return [[TBXML alloc ] initWithXMLData: aData error: error];
65
65
}
66
66
67
- + (id )tbxmlWithXMLFile : (NSString *)aXMLFile {
67
+ + (id )newTBXMLWithXMLFile : (NSString *)aXMLFile {
68
68
return [[TBXML alloc ] initWithXMLFile: aXMLFile];
69
69
}
70
70
71
- + (id )tbxmlWithXMLFile : (NSString *)aXMLFile error : (NSError *__autoreleasing *)error {
71
+ + (id )newTBXMLWithXMLFile : (NSString *)aXMLFile error : (NSError *__autoreleasing *)error {
72
72
return [[TBXML alloc ] initWithXMLFile: aXMLFile error: error];
73
73
}
74
74
75
- + (id )tbxmlWithXMLFile : (NSString *)aXMLFile fileExtension : (NSString *)aFileExtension {
75
+ + (id )newTBXMLWithXMLFile : (NSString *)aXMLFile fileExtension : (NSString *)aFileExtension {
76
76
return [[TBXML alloc ] initWithXMLFile: aXMLFile fileExtension: aFileExtension];
77
77
}
78
78
79
- + (id )tbxmlWithXMLFile : (NSString *)aXMLFile fileExtension : (NSString *)aFileExtension error : (NSError *__autoreleasing *)error {
79
+ + (id )newTBXMLWithXMLFile : (NSString *)aXMLFile fileExtension : (NSString *)aFileExtension error : (NSError *__autoreleasing *)error {
80
80
return [[TBXML alloc ] initWithXMLFile: aXMLFile fileExtension: aFileExtension error: error];
81
81
}
82
82
@@ -107,10 +107,10 @@ - (id)initWithXMLString:(NSString*)aXMLString error:(NSError *__autoreleasing *)
107
107
108
108
109
109
// allocate memory for byte array
110
- [self allocateBytesOfLength: [aXMLString lengthOfBytesUsingEncoding: NSUTF8StringEncoding] error: error];
110
+ int result = [self allocateBytesOfLength: [aXMLString lengthOfBytesUsingEncoding: NSUTF8StringEncoding] error: error];
111
111
112
112
// if an error occured, return
113
- if (error && *error != nil )
113
+ if (result != D_TBXML_SUCCESS )
114
114
return self;
115
115
116
116
// copy string to byte array
@@ -207,32 +207,40 @@ - (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtensio
207
207
return self;
208
208
}
209
209
210
- - (void ) decodeData : (NSData *)data {
210
+ - (int ) decodeData : (NSData *)data {
211
211
NSError *error = nil ;
212
- [self decodeData: data withError: &error];
212
+ return [self decodeData: data withError: &error];
213
213
}
214
214
215
- - (void ) decodeData : (NSData *)data withError : (NSError **)error {
215
+ - (int ) decodeData : (NSData *)data withError : (NSError **)error {
216
+
217
+ NSError *localError = nil ;
216
218
217
219
// allocate memory for byte array
218
- [self allocateBytesOfLength: [data length ] error: error ];
220
+ int result = [self allocateBytesOfLength: [data length ] error: &localError ];
219
221
220
- // if an error occured, return
221
- if (error && *error)
222
- return ;
223
-
224
- // copy data to byte array
225
- [data getBytes: bytes length: bytesLength];
226
-
227
- // set null terminator at end of byte array
228
- bytes[bytesLength] = 0 ;
229
-
230
- // decode xml data
231
- [ self decodeBytes ];
232
-
233
- if (!self. rootXMLElement ) {
234
- *error = [TBXML errorWithCode: D_TBXML_DECODE_FAILURE];
222
+ // ensure no errors during allocation
223
+ if (result == D_TBXML_SUCCESS) {
224
+
225
+ // copy data to byte array
226
+ [data getBytes: bytes length: bytesLength];
227
+
228
+ // set null terminator at end of byte array
229
+ bytes[bytesLength] = 0 ;
230
+
231
+ // decode xml data
232
+ [ self decodeBytes ];
233
+
234
+ if (!self. rootXMLElement ) {
235
+ localError = [TBXML errorWithCode: D_TBXML_DECODE_FAILURE];
236
+ }
235
237
}
238
+
239
+ // assign local error to pointer
240
+ if (error) *error = localError;
241
+
242
+ // return success or error code
243
+ return localError == nil ? D_TBXML_SUCCESS : [localError code ];
236
244
}
237
245
238
246
@end
@@ -255,13 +263,13 @@ + (NSString*) elementName:(TBXMLElement*)aXMLElement {
255
263
+ (NSString *) elementName : (TBXMLElement*)aXMLElement error : (NSError **)error {
256
264
// check for nil element
257
265
if (nil == aXMLElement) {
258
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
266
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
259
267
return @" " ;
260
268
}
261
269
262
270
// check for nil element name
263
271
if (nil == aXMLElement->name || strlen (aXMLElement->name ) == 0 ) {
264
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NAME_IS_NIL];
272
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NAME_IS_NIL];
265
273
return @" " ;
266
274
}
267
275
@@ -276,13 +284,13 @@ + (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute {
276
284
+ (NSString *) attributeName : (TBXMLAttribute*)aXMLAttribute error : (NSError **)error {
277
285
// check for nil attribute
278
286
if (nil == aXMLAttribute) {
279
- *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_IS_NIL];
287
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_IS_NIL];
280
288
return @" " ;
281
289
}
282
290
283
291
// check for nil attribute name
284
292
if (nil == aXMLAttribute->name ) {
285
- *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NAME_IS_NIL];
293
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NAME_IS_NIL];
286
294
return @" " ;
287
295
}
288
296
@@ -298,7 +306,7 @@ + (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute {
298
306
+ (NSString *) attributeValue : (TBXMLAttribute*)aXMLAttribute error : (NSError **)error {
299
307
// check for nil attribute
300
308
if (nil == aXMLAttribute) {
301
- *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_IS_NIL];
309
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_IS_NIL];
302
310
return @" " ;
303
311
}
304
312
@@ -313,13 +321,13 @@ + (NSString*) textForElement:(TBXMLElement*)aXMLElement {
313
321
+ (NSString *) textForElement : (TBXMLElement*)aXMLElement error : (NSError **)error {
314
322
// check for nil element
315
323
if (nil == aXMLElement) {
316
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
324
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
317
325
return @" " ;
318
326
}
319
327
320
328
// check for nil text value
321
329
if (nil == aXMLElement->text || strlen (aXMLElement->text ) == 0 ) {
322
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_TEXT_IS_NIL];
330
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_TEXT_IS_NIL];
323
331
return @" " ;
324
332
}
325
333
@@ -343,13 +351,13 @@ + (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)
343
351
+ (NSString *) valueOfAttributeNamed : (NSString *)aName forElement : (TBXMLElement*)aXMLElement error : (NSError **)error {
344
352
// check for nil element
345
353
if (nil == aXMLElement) {
346
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
354
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
347
355
return @" " ;
348
356
}
349
357
350
358
// check for nil name parameter
351
359
if (nil == aName) {
352
- *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NAME_IS_NIL];
360
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NAME_IS_NIL];
353
361
return @" " ;
354
362
}
355
363
@@ -371,7 +379,7 @@ + (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)
371
379
372
380
// check for attribute not found
373
381
if (!value) {
374
- *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NOT_FOUND];
382
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ATTRIBUTE_NOT_FOUND];
375
383
return @" " ;
376
384
}
377
385
@@ -394,13 +402,13 @@ + (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement
394
402
+ (TBXMLElement*) childElementNamed : (NSString *)aName parentElement : (TBXMLElement*)aParentXMLElement error : (NSError **)error {
395
403
// check for nil element
396
404
if (nil == aParentXMLElement) {
397
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
405
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
398
406
return nil ;
399
407
}
400
408
401
409
// check for nil name parameter
402
410
if (nil == aName) {
403
- *error = [TBXML errorWithCode: D_TBXML_PARAM_NAME_IS_NIL];
411
+ if (error) *error = [TBXML errorWithCode: D_TBXML_PARAM_NAME_IS_NIL];
404
412
return nil ;
405
413
}
406
414
@@ -413,7 +421,7 @@ + (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement
413
421
xmlElement = xmlElement->nextSibling ;
414
422
}
415
423
416
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NOT_FOUND];
424
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NOT_FOUND];
417
425
418
426
return nil ;
419
427
}
@@ -433,13 +441,13 @@ + (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElem
433
441
+ (TBXMLElement*) nextSiblingNamed : (NSString *)aName searchFromElement : (TBXMLElement*)aXMLElement error : (NSError **)error {
434
442
// check for nil element
435
443
if (nil == aXMLElement) {
436
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
444
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_IS_NIL];
437
445
return nil ;
438
446
}
439
447
440
448
// check for nil name parameter
441
449
if (nil == aName) {
442
- *error = [TBXML errorWithCode: D_TBXML_PARAM_NAME_IS_NIL];
450
+ if (error) *error = [TBXML errorWithCode: D_TBXML_PARAM_NAME_IS_NIL];
443
451
return nil ;
444
452
}
445
453
@@ -452,7 +460,7 @@ + (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElem
452
460
xmlElement = xmlElement->nextSibling ;
453
461
}
454
462
455
- *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NOT_FOUND];
463
+ if (error) *error = [TBXML errorWithCode: D_TBXML_ELEMENT_NOT_FOUND];
456
464
457
465
return nil ;
458
466
}
@@ -572,18 +580,24 @@ + (NSError *) errorWithCode:(int)code userInfo:(NSMutableDictionary *)someUserIn
572
580
userInfo: userInfo];
573
581
}
574
582
575
- - (void ) allocateBytesOfLength : (long )length error : (NSError **)error {
583
+ - (int ) allocateBytesOfLength : (long )length error : (NSError **)error {
576
584
bytesLength = length;
577
585
586
+ NSError *localError = nil ;
587
+
578
588
if (!length) {
579
- *error = [TBXML errorWithCode: D_TBXML_DATA_NIL];
589
+ localError = [TBXML errorWithCode: D_TBXML_DATA_NIL];
580
590
}
581
591
582
592
bytes = malloc (bytesLength+1 );
583
593
584
594
if (!bytes) {
585
- *error = [TBXML errorWithCode: D_TBXML_MEMORY_ALLOC_FAILURE];
595
+ localError = [TBXML errorWithCode: D_TBXML_MEMORY_ALLOC_FAILURE];
586
596
}
597
+
598
+ if (error) *error = localError;
599
+
600
+ return localError == nil ? D_TBXML_SUCCESS : [localError code ];
587
601
}
588
602
589
603
- (void ) decodeBytes {
@@ -865,7 +879,7 @@ - (void) decodeBytes {
865
879
866
880
// Deallocate used memory
867
881
- (void ) dealloc {
868
-
882
+
869
883
if (bytes) {
870
884
free (bytes);
871
885
bytes = nil ;
@@ -896,6 +910,10 @@ - (void) dealloc {
896
910
currentAttributeBuffer = 0 ;
897
911
}
898
912
}
913
+
914
+ #ifndef ARC_ENABLED
915
+ [super dealloc ];
916
+ #endif
899
917
}
900
918
901
919
- (TBXMLElement*) nextAvailableElement {
0 commit comments