[iphone]Reorganized ZXingWidget with directories for categories of source files
[zxing.git] / iphone / ZXingWidget / Classes / resultParsers / DoCoMoResultParser.m
1 //
2 //  DoCoMoResultParser.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 25/06/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #import "DoCoMoResultParser.h"
23
24
25 @implementation NSString (DoCoMoFieldParsing) 
26
27 - (NSString *)backslashUnescaped {
28   NSRange backslashRange = [self rangeOfString:@"\\"];
29   if (backslashRange.location == NSNotFound) {
30     return self;
31   }
32   
33   int max = [self length];
34   int startLocation = 0;
35   NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
36   while (backslashRange.location != NSNotFound) {
37     [result appendString:[self substringWithRange:NSMakeRange(startLocation, 
38                                                               backslashRange.location - startLocation)]];
39     [result appendFormat:@"%c", [self characterAtIndex:backslashRange.location + 1]];
40     startLocation = backslashRange.location + 2;
41     NSRange searchRange = NSMakeRange(startLocation, max - startLocation);
42     backslashRange = [self rangeOfString:@"\\" options:0 range:searchRange];
43   }
44   if (startLocation < max) {
45     [result appendString:[self substringWithRange:NSMakeRange(startLocation, max - startLocation)]];
46   }
47   return [NSString stringWithString:result];
48 }
49
50 - (NSArray *)fieldsWithPrefix:(NSString *)prefix {
51   return [self fieldsWithPrefix:prefix terminator:@";"];
52 }
53
54 - (NSArray *)fieldsWithPrefix:(NSString *)prefix terminator:(NSString *)term {
55   NSMutableArray *result = nil;
56   
57   int i = 0;
58   int max = [self length];
59   NSRange searchRange;
60   NSRange foundRange;
61   while (i < max) {
62     searchRange = NSMakeRange(i, max - i);
63     foundRange = [self rangeOfString:prefix options:0 range:searchRange];
64     if(foundRange.location == NSNotFound) {
65       break;
66     }
67     
68     int start = i = foundRange.location + foundRange.length;
69     bool done = false;
70     while (!done) {
71       searchRange = NSMakeRange(i, max - i);
72       NSRange termRange = [self rangeOfString:term options:0 range:searchRange];
73       if (termRange.location == NSNotFound) {
74         i = max;
75         done = true;
76       } else if ([self characterAtIndex:termRange.location-1] == (unichar)'\\') {
77         i++;
78       } else {
79         NSString *substring = [self substringWithRange:NSMakeRange(start, termRange.location - start)];
80         NSString *unescaped = [substring backslashUnescaped];
81         if (result == nil) {
82           result = [NSMutableArray arrayWithObject:unescaped];
83         } else {
84           [result addObject:unescaped];
85         }
86         i = termRange.location + termRange.length;
87         done = true;
88       }
89     }
90   }
91   
92   return result;
93 }
94
95 - (NSString *)fieldWithPrefix:(NSString *)prefix {
96   return [self fieldWithPrefix:prefix terminator:@";"];
97 }
98
99 - (NSString *)fieldWithPrefix:(NSString *)prefix terminator:(NSString *)term {
100   NSArray *fields = [self fieldsWithPrefix:prefix terminator:term];
101   if (fields.count == 0) {
102     return nil;
103   } else {
104     return [fields lastObject];
105   }
106 }
107
108 @end
109
110
111
112 @implementation DoCoMoResultParser
113
114 @end