project_files/HedgewarsMobile/Classes/IniParser.m
author Wuzzy <almikes@aol.com>
Mon, 08 May 2017 03:42:51 +0200
changeset 12414 7cdd8e113dc0
parent 11573 e9ee87b28e17
child 12877 00215a7ec5f5
permissions -rw-r--r--
Add many, many English announcer messages for crate drops
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11572
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     1
/*
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     3
 * Copyright (c) 2015-2016 Anton Malmygin <antonc27@mail.ru>
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     4
 *
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     8
 *
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    12
 * GNU General Public License for more details.
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    13
 *
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    17
 */
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    18
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    19
#import "IniParser.h"
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    20
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    21
#define COMMENTS_START_CHAR ';'
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    22
#define  SECTION_START_CHAR '['
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    23
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    24
@interface IniParser ()
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    25
@property (nonatomic, retain) NSString *iniFilePath;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    26
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    27
@property (nonatomic, retain) NSMutableArray *mutableSections;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    28
@property (nonatomic, retain) NSMutableDictionary *currentSection;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    29
@end
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    30
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    31
@implementation IniParser
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    32
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    33
#pragma mark - Initilisation
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    34
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    35
- (instancetype)initWithIniFilePath:(NSString *)iniFilePath {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    36
    self = [super init];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    37
    if (self) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    38
        _iniFilePath = [iniFilePath copy];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    39
    }
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    40
    return self;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    41
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    42
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    43
#pragma mark - Parse sections
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    44
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    45
- (NSArray *)newParsedSections {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    46
    NSString *iniFileContents = [NSString stringWithContentsOfFile:self.iniFilePath encoding:NSUTF8StringEncoding error:nil];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    47
    
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    48
    [self prepareForParsing];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    49
    [iniFileContents enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    50
        if (![self isNeedToSkipLine:line]) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    51
            [self parseLine:line];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    52
        }
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    53
    }];
11573
e9ee87b28e17 - Campaign for iOS: Don't forget to add last parsed section in ini file
antonc27 <antonc27@mail.ru>
parents: 11572
diff changeset
    54
    [self addLastParsedSectionToSections];
11572
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    55
    
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    56
    return [self copyParsedSections];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    57
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    58
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    59
- (void)prepareForParsing {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    60
    self.mutableSections = [[NSMutableArray alloc] init];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    61
    self.currentSection = nil;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    62
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    63
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    64
- (BOOL)isNeedToSkipLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    65
    return ([line length] < 1 || [self isLineAComment:line]);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    66
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    67
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    68
- (BOOL)isLineAComment:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    69
    return ([line characterAtIndex:0] == COMMENTS_START_CHAR);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    70
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    71
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    72
- (void)parseLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    73
    if ([self isLineASectionStart:line]) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    74
        [self addPreviousSectionToSectionsIfNecessary];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    75
        [self createCurrentSection];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    76
    } else {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    77
        [self parseAssignmentForCurrentSectionInLine:line];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    78
    }
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    79
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    80
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    81
- (BOOL)isLineASectionStart:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    82
    return ([line characterAtIndex:0] == SECTION_START_CHAR);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    83
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    84
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    85
- (void)addPreviousSectionToSectionsIfNecessary {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    86
    if (self.currentSection != nil) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    87
        [self.mutableSections addObject:self.currentSection];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    88
        [self.currentSection release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    89
    }
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    90
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    91
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    92
- (void)createCurrentSection {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    93
    self.currentSection = [[NSMutableDictionary alloc] init];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    94
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    95
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    96
- (void)parseAssignmentForCurrentSectionInLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    97
    NSArray *components = [line componentsSeparatedByString:@"="];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    98
    if (components.count > 1) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    99
        NSString *key = components[0];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   100
        NSString *value = components[1];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   101
        [self.currentSection setObject:value forKey:key];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   102
    }
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   103
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   104
11573
e9ee87b28e17 - Campaign for iOS: Don't forget to add last parsed section in ini file
antonc27 <antonc27@mail.ru>
parents: 11572
diff changeset
   105
- (void)addLastParsedSectionToSections {
e9ee87b28e17 - Campaign for iOS: Don't forget to add last parsed section in ini file
antonc27 <antonc27@mail.ru>
parents: 11572
diff changeset
   106
    [self addPreviousSectionToSectionsIfNecessary];
e9ee87b28e17 - Campaign for iOS: Don't forget to add last parsed section in ini file
antonc27 <antonc27@mail.ru>
parents: 11572
diff changeset
   107
}
e9ee87b28e17 - Campaign for iOS: Don't forget to add last parsed section in ini file
antonc27 <antonc27@mail.ru>
parents: 11572
diff changeset
   108
11572
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   109
- (NSArray *)copyParsedSections {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   110
    return [self.mutableSections copy];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   111
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   112
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   113
#pragma mark - Dealloc
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   114
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   115
- (void)dealloc {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   116
    [_iniFilePath release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   117
    [_mutableSections release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   118
    [_currentSection release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   119
    [super dealloc];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   120
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   121
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   122
@end