Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXAddressBookAUResultParser.h"
  17. #import "ZXAddressBookParsedResult.h"
  18. #import "ZXResult.h"
  19. @implementation ZXAddressBookAUResultParser
  20. - (ZXParsedResult *)parse:(ZXResult *)result {
  21. NSString *rawText = [ZXResultParser massagedText:result];
  22. if ([rawText rangeOfString:@"MEMORY"].location == NSNotFound ||
  23. [rawText rangeOfString:@"\r\n"].location == NSNotFound) {
  24. return nil;
  25. }
  26. NSString *name = [[self class] matchSinglePrefixedField:@"NAME1:" rawText:rawText endChar:'\r' trim:YES];
  27. NSString *pronunciation = [[self class] matchSinglePrefixedField:@"NAME2:" rawText:rawText endChar:'\r' trim:YES];
  28. NSArray *phoneNumbers = [self matchMultipleValuePrefix:@"TEL" max:3 rawText:rawText trim:YES];
  29. NSArray *emails = [self matchMultipleValuePrefix:@"MAIL" max:3 rawText:rawText trim:YES];
  30. NSString *note = [[self class] matchSinglePrefixedField:@"MEMORY:" rawText:rawText endChar:'\r' trim:NO];
  31. NSString *address = [[self class] matchSinglePrefixedField:@"ADD:" rawText:rawText endChar:'\r' trim:YES];
  32. NSArray *addresses = address == nil ? nil : @[address];
  33. return [ZXAddressBookParsedResult addressBookParsedResultWithNames:[self maybeWrap:name]
  34. nicknames:nil
  35. pronunciation:pronunciation
  36. phoneNumbers:phoneNumbers
  37. phoneTypes:nil
  38. emails:emails
  39. emailTypes:nil
  40. instantMessenger:nil
  41. note:note
  42. addresses:addresses
  43. addressTypes:nil
  44. org:nil
  45. birthday:nil
  46. title:nil
  47. urls:nil
  48. geo:nil];
  49. }
  50. - (NSArray *)matchMultipleValuePrefix:(NSString *)prefix max:(int)max rawText:(NSString *)rawText trim:(BOOL)trim {
  51. NSMutableArray *values = nil;
  52. for (int i = 1; i <= max; i++) {
  53. NSString *value = [[self class] matchSinglePrefixedField:[NSString stringWithFormat:@"%@%d:", prefix, i]
  54. rawText:rawText
  55. endChar:'\r'
  56. trim:trim];
  57. if (value == nil) {
  58. break;
  59. }
  60. if (values == nil) {
  61. values = [[NSMutableArray alloc] initWithCapacity:max];
  62. }
  63. [values addObject:value];
  64. }
  65. if (values == nil) {
  66. return nil;
  67. }
  68. return values;
  69. }
  70. @end