More fixes to the build files after trying to build the whole thing again for release
[zxing.git] / iphone / Classes / SMSResultParser.m
1 //
2 //  SMSResultParser.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 "SMSResultParser.h"
10 #import "SMSParsedResult.h"
11
12 #define PREFIX @"sms:"
13
14 @implementation SMSResultParser
15
16 + (ParsedResult *)parsedResultForString:(NSString *)s {
17   NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
18   if (prefixRange.location == 0) {
19     int max = [s length];
20     int restStart = prefixRange.location + prefixRange.length;
21     
22     // initial presuption: everything after the prefix is the number, and there is no body
23     NSRange numberRange = NSMakeRange(restStart, max - restStart);
24     NSRange bodyRange = NSMakeRange(NSNotFound, 0);
25
26     // is there a query string?
27     NSRange queryRange = [s rangeOfString:@"?" options:0 range:numberRange];
28     if (queryRange.location != NSNotFound) {
29       // truncate the number range at the beginning of the query string
30       numberRange.length = queryRange.location - numberRange.location;
31       
32       int paramsStart = queryRange.location + queryRange.length;
33       NSRange paramsRange = NSMakeRange(paramsStart, max - paramsStart);
34       NSRange bodyPrefixRange = [s rangeOfString:@"body=" options:0 range:paramsRange];
35       if (bodyPrefixRange.location != NSNotFound) {
36         int bodyStart = bodyPrefixRange.location + bodyPrefixRange.length;
37         bodyRange = NSMakeRange(bodyStart, max - bodyStart);
38         NSRange ampRange = [s rangeOfString:@"&" options:0 range:bodyRange];
39         if (ampRange.location != NSNotFound) {
40           // we found a '&', so we truncate the body range there
41           bodyRange.length = ampRange.location - bodyRange.location;
42         }
43       }
44     } 
45     
46     // if there's a semicolon in the number, truncate the number there
47     NSRange semicolonRange = [s rangeOfString:@";" options:0 range:numberRange];
48     if (semicolonRange.location != NSNotFound) {
49       numberRange.length = semicolonRange.location - numberRange.location;
50     }
51     
52     NSString *number = [s substringWithRange:numberRange];
53     NSString *body = bodyRange.location != NSNotFound ? [s substringWithRange:bodyRange] : nil;
54     return [[[SMSParsedResult alloc] initWithNumber:number body:body]
55           autorelease];
56   }
57   return nil;
58 }
59
60
61 @end