More fixes to the build files after trying to build the whole thing again for release
[zxing.git] / iphone / Classes / DoCoMoResultParser.m
1 //
2 //  DoCoMoResultParser.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 25/06/2008.
6 //  Copyright 2008 Google Inc. All rights reserved.
7 //
8
9 #import "DoCoMoResultParser.h"
10
11
12 @implementation NSString (DoCoMoFieldParsing) 
13
14 - (NSString *)backslashUnescaped {
15   NSRange backslashRange = [self rangeOfString:@"\\"];
16   if (backslashRange.location == NSNotFound) {
17     return self;
18   }
19   
20   int max = [self length];
21   int startLocation = 0;
22   NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
23   while (backslashRange.location != NSNotFound) {
24     [result appendString:[self substringWithRange:NSMakeRange(startLocation, 
25                                                               backslashRange.location - startLocation)]];
26     [result appendFormat:@"%c", [self characterAtIndex:backslashRange.location + 1]];
27     startLocation = backslashRange.location + 2;
28     NSRange searchRange = NSMakeRange(startLocation, max - startLocation);
29     backslashRange = [self rangeOfString:@"\\" options:0 range:searchRange];
30   }
31   if (startLocation < max) {
32     [result appendString:[self substringWithRange:NSMakeRange(startLocation, max - startLocation)]];
33   }
34   return [NSString stringWithString:result];
35 }
36
37 - (NSArray *)fieldsWithPrefix:(NSString *)prefix {
38   return [self fieldsWithPrefix:prefix terminator:@";"];
39 }
40
41 - (NSArray *)fieldsWithPrefix:(NSString *)prefix terminator:(NSString *)term {
42   NSMutableArray *result = nil;
43   
44   int i = 0;
45   int max = [self length];
46   NSRange searchRange;
47   NSRange foundRange;
48   while (i < max) {
49     searchRange = NSMakeRange(i, max - i);
50     foundRange = [self rangeOfString:prefix options:0 range:searchRange];
51     if(foundRange.location == NSNotFound) {
52       break;
53     }
54     
55     int start = i = foundRange.location + foundRange.length;
56     bool done = false;
57     while (!done) {
58       searchRange = NSMakeRange(i, max - i);
59       NSRange termRange = [self rangeOfString:term options:0 range:searchRange];
60       if (termRange.location == NSNotFound) {
61         i = max;
62         done = true;
63       } else if ([self characterAtIndex:termRange.location-1] == (unichar)'\\') {
64         i++;
65       } else {
66         NSString *substring = [self substringWithRange:NSMakeRange(start, termRange.location - start)];
67         NSString *unescaped = [substring backslashUnescaped];
68         if (result == nil) {
69           result = [NSMutableArray arrayWithObject:unescaped];
70         } else {
71           [result addObject:unescaped];
72         }
73         i = termRange.location + termRange.length;
74         done = true;
75       }
76     }
77   }
78   
79   return result;
80 }
81
82 - (NSString *)fieldWithPrefix:(NSString *)prefix {
83   return [self fieldWithPrefix:prefix terminator:@";"];
84 }
85
86 - (NSString *)fieldWithPrefix:(NSString *)prefix terminator:(NSString *)term {
87   NSArray *fields = [self fieldsWithPrefix:prefix terminator:term];
88   if (fields.count == 0) {
89     return nil;
90   } else {
91     return [fields lastObject];
92   }
93 }
94
95 @end
96
97
98
99 @implementation DoCoMoResultParser
100
101 @end