project_files/HedgewarsMobile/Classes/IniParser.m
author antonc27 <antonc27@mail.ru>
Tue, 23 Feb 2016 00:29:46 +0100
changeset 11572 836af7d4af69
child 11573 e9ee87b28e17
permissions -rw-r--r--
- Campaign for iOS: Reinventing wheel for campaign.ini parsing
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
    }];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    54
    
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    55
    return [self copyParsedSections];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    56
}
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
- (void)prepareForParsing {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    59
    self.mutableSections = [[NSMutableArray alloc] init];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    60
    self.currentSection = nil;
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    61
}
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
- (BOOL)isNeedToSkipLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    64
    return ([line length] < 1 || [self isLineAComment:line]);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    65
}
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
- (BOOL)isLineAComment:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    68
    return ([line characterAtIndex:0] == COMMENTS_START_CHAR);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    69
}
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
- (void)parseLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    72
    if ([self isLineASectionStart:line]) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    73
        [self addPreviousSectionToSectionsIfNecessary];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    74
        [self createCurrentSection];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    75
    } else {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    76
        [self parseAssignmentForCurrentSectionInLine:line];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    77
    }
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
- (BOOL)isLineASectionStart:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    81
    return ([line characterAtIndex:0] == SECTION_START_CHAR);
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    82
}
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
- (void)addPreviousSectionToSectionsIfNecessary {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    85
    if (self.currentSection != nil) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    86
        [self.mutableSections addObject:self.currentSection];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    87
        [self.currentSection release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    88
    }
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
- (void)createCurrentSection {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    92
    self.currentSection = [[NSMutableDictionary alloc] init];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    93
}
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
- (void)parseAssignmentForCurrentSectionInLine:(NSString *)line {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    96
    NSArray *components = [line componentsSeparatedByString:@"="];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    97
    if (components.count > 1) {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    98
        NSString *key = components[0];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    99
        NSString *value = components[1];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   100
        [self.currentSection setObject:value forKey:key];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   101
    }
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
- (NSArray *)copyParsedSections {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   105
    return [self.mutableSections copy];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   106
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   107
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   108
#pragma mark - Dealloc
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   109
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   110
- (void)dealloc {
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   111
    [_iniFilePath release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   112
    [_mutableSections release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   113
    [_currentSection release];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   114
    [super dealloc];
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   115
}
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   116
836af7d4af69 - Campaign for iOS: Reinventing wheel for campaign.ini parsing
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   117
@end