- Campaign for iOS: Reinventing wheel for campaign.ini parsing
authorantonc27 <antonc27@mail.ru>
Tue, 23 Feb 2016 00:29:46 +0100
changeset 11572 836af7d4af69
parent 11570 3db41c7697f0
child 11573 e9ee87b28e17
- Campaign for iOS: Reinventing wheel for campaign.ini parsing
project_files/HedgewarsMobile/Classes/IniParser.h
project_files/HedgewarsMobile/Classes/IniParser.m
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/IniParser.h	Tue Feb 23 00:29:46 2016 +0100
@@ -0,0 +1,27 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2015-2016 Anton Malmygin <antonc27@mail.ru>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface IniParser : NSObject
+
+- (instancetype)initWithIniFilePath:(NSString *)iniFilePath;
+
+- (NSArray *)newParsedSections;
+
+@end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/IniParser.m	Tue Feb 23 00:29:46 2016 +0100
@@ -0,0 +1,117 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2015-2016 Anton Malmygin <antonc27@mail.ru>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+ */
+
+#import "IniParser.h"
+
+#define COMMENTS_START_CHAR ';'
+#define  SECTION_START_CHAR '['
+
+@interface IniParser ()
+@property (nonatomic, retain) NSString *iniFilePath;
+
+@property (nonatomic, retain) NSMutableArray *mutableSections;
+@property (nonatomic, retain) NSMutableDictionary *currentSection;
+@end
+
+@implementation IniParser
+
+#pragma mark - Initilisation
+
+- (instancetype)initWithIniFilePath:(NSString *)iniFilePath {
+    self = [super init];
+    if (self) {
+        _iniFilePath = [iniFilePath copy];
+    }
+    return self;
+}
+
+#pragma mark - Parse sections
+
+- (NSArray *)newParsedSections {
+    NSString *iniFileContents = [NSString stringWithContentsOfFile:self.iniFilePath encoding:NSUTF8StringEncoding error:nil];
+    
+    [self prepareForParsing];
+    [iniFileContents enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
+        if (![self isNeedToSkipLine:line]) {
+            [self parseLine:line];
+        }
+    }];
+    
+    return [self copyParsedSections];
+}
+
+- (void)prepareForParsing {
+    self.mutableSections = [[NSMutableArray alloc] init];
+    self.currentSection = nil;
+}
+
+- (BOOL)isNeedToSkipLine:(NSString *)line {
+    return ([line length] < 1 || [self isLineAComment:line]);
+}
+
+- (BOOL)isLineAComment:(NSString *)line {
+    return ([line characterAtIndex:0] == COMMENTS_START_CHAR);
+}
+
+- (void)parseLine:(NSString *)line {
+    if ([self isLineASectionStart:line]) {
+        [self addPreviousSectionToSectionsIfNecessary];
+        [self createCurrentSection];
+    } else {
+        [self parseAssignmentForCurrentSectionInLine:line];
+    }
+}
+
+- (BOOL)isLineASectionStart:(NSString *)line {
+    return ([line characterAtIndex:0] == SECTION_START_CHAR);
+}
+
+- (void)addPreviousSectionToSectionsIfNecessary {
+    if (self.currentSection != nil) {
+        [self.mutableSections addObject:self.currentSection];
+        [self.currentSection release];
+    }
+}
+
+- (void)createCurrentSection {
+    self.currentSection = [[NSMutableDictionary alloc] init];
+}
+
+- (void)parseAssignmentForCurrentSectionInLine:(NSString *)line {
+    NSArray *components = [line componentsSeparatedByString:@"="];
+    if (components.count > 1) {
+        NSString *key = components[0];
+        NSString *value = components[1];
+        [self.currentSection setObject:value forKey:key];
+    }
+}
+
+- (NSArray *)copyParsedSections {
+    return [self.mutableSections copy];
+}
+
+#pragma mark - Dealloc
+
+- (void)dealloc {
+    [_iniFilePath release];
+    [_mutableSections release];
+    [_currentSection release];
+    [super dealloc];
+}
+
+@end
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Mon Feb 22 16:01:29 2016 -0500
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Tue Feb 23 00:29:46 2016 +0100
@@ -242,6 +242,7 @@
 		61F9040B11DF59370068B24D /* background.png in Resources */ = {isa = PBXBuildFile; fileRef = 61F9040A11DF59370068B24D /* background.png */; };
 		61F904D711DF7DA30068B24D /* WeaponCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = 61F904D611DF7DA30068B24D /* WeaponCellView.m */; };
 		922F64900F10F53100DC6EC0 /* libfpc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 928301170F10CAFC00CC5A3C /* libfpc.a */; };
+		F60ACBB71C7BC08B00385701 /* IniParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F60ACBB61C7BC08B00385701 /* IniParser.m */; };
 		F60D04771BD137B5003ACB00 /* bullet_filled.png in Resources */ = {isa = PBXBuildFile; fileRef = F60D04631BD137B5003ACB00 /* bullet_filled.png */; };
 		F60D04781BD137B5003ACB00 /* bullet_filled@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F60D04641BD137B5003ACB00 /* bullet_filled@2x.png */; };
 		F60D04791BD137B5003ACB00 /* bullet.png in Resources */ = {isa = PBXBuildFile; fileRef = F60D04651BD137B5003ACB00 /* bullet.png */; };
@@ -737,6 +738,8 @@
 		61F904D611DF7DA30068B24D /* WeaponCellView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = WeaponCellView.m; path = Classes/WeaponCellView.m; sourceTree = "<group>"; };
 		8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		928301170F10CAFC00CC5A3C /* libfpc.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libfpc.a; sourceTree = BUILT_PRODUCTS_DIR; };
+		F60ACBB51C7BC08B00385701 /* IniParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IniParser.h; path = Classes/IniParser.h; sourceTree = "<group>"; };
+		F60ACBB61C7BC08B00385701 /* IniParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IniParser.m; path = Classes/IniParser.m; sourceTree = "<group>"; };
 		F60D04631BD137B5003ACB00 /* bullet_filled.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = bullet_filled.png; path = Resources/Icons/bullet_filled.png; sourceTree = "<group>"; };
 		F60D04641BD137B5003ACB00 /* bullet_filled@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "bullet_filled@2x.png"; path = "Resources/Icons/bullet_filled@2x.png"; sourceTree = "<group>"; };
 		F60D04651BD137B5003ACB00 /* bullet.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = bullet.png; path = Resources/Icons/bullet.png; sourceTree = "<group>"; };
@@ -1271,6 +1274,8 @@
 				6165922611CA9BD500D6E256 /* HWUtils.m */,
 				6165922C11CA9BD500D6E256 /* UIImageExtra.h */,
 				6165922D11CA9BD500D6E256 /* UIImageExtra.m */,
+				F60ACBB51C7BC08B00385701 /* IniParser.h */,
+				F60ACBB61C7BC08B00385701 /* IniParser.m */,
 			);
 			name = Helpers;
 			sourceTree = "<group>";
@@ -1900,6 +1905,7 @@
 				6165921C11CA9BA200D6E256 /* SingleSchemeViewController.m in Sources */,
 				6165921D11CA9BA200D6E256 /* SingleTeamViewController.m in Sources */,
 				6165921F11CA9BA200D6E256 /* TeamConfigViewController.m in Sources */,
+				F60ACBB71C7BC08B00385701 /* IniParser.m in Sources */,
 				6165922011CA9BA200D6E256 /* TeamSettingsViewController.m in Sources */,
 				6165922111CA9BA200D6E256 /* VoicesViewController.m in Sources */,
 				6165922211CA9BA200D6E256 /* WeaponSettingsViewController.m in Sources */,