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