PGN file parser in objective c for use in xcode

Hi

as a requirement i was asked to make an PGN parser in XCODE i made this from scratch and it is my original work .
though this program is still buggy and it does not perform at professional level but it can still parse and extract no of games in one file extract a particular game and even extract moves and details of a particular game...I tried very hard to find one suitable for my need but all were in c or c++ ...usable but not in Objective c ...so i wrote one myself using XCODE...so here it is for you xcoders


how it works

1.make object of PgnPrserBrain.h and init with string which is pgn string.
2.now you can call all or any method they will return you mutable arrays.
3.you can get moves by calling  -(NSMutableArray*)getArrayOfMoves:(int)gameNumber;
4.it returns an ns mutable array containing object of moves class and every element of  that array will have .whiteMove and .blackMove.

the parser will show error for many PGN but will still successfully parse majority of them a startup for those looking help....

work is no progress and i will update the code.....


USER BEWARE THE WRITER OF THIS CODE BEARS NO RESPONSIBILITY OF ANY LOSS CAUSED DUE TO THE PARSERS USE.
DOES NOT DEAL WITH ANNOTATIONS AND COMMENTS FOR THAT CONTACT MONK

PLEASE GIVE FEED BACK...







MOVES.H

=======================================================================

//
// Moves.h
// parsingPGN
//
//User has the sole responsibility and uses this at his own risk no guarentees from the programmer who
//created this program

// Created by test on 04/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Moves : NSObject{
NSString *whiteMove;
NSString *blackMove;

}
@property (nonatomic,retain) NSString *whiteMove;
@property (nonatomic,retain) NSString *blackMove;
-(id)initWithMoves:(NSString*)whiteMv balck:(NSString*)blackMv;
@end



MOVES.M
=========================================================================
//
// Moves.m
// parsingPGN
//
//User has the sole responsibility and uses this at his own risk no guarentees from the programmer who
//created this program

// Created by test on 04/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Moves.h"


@implementation Moves
@synthesize whiteMove,blackMove;
-(id)initWithMoves:(NSString*)whiteMv balck:(NSString*)blackMv{
[self setWhiteMove:whiteMv];
[self setBlackMove:blackMv];
return self;
}


@end









PgnPrserBrain.h
=========================================================================


//
// PgnPrserBrain.h
// parsingPGN
//
//User has the sole responsibility and uses this at his own risk no guarentees from the programmer who
//created this program

// Created by Amarendra singh on 02/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface PgnPrserBrain : NSObject{
NSMutableArray *contentsOfFile;
NSMutableArray *arrayToBereturned;
NSMutableArray *startIndexOfFiles;
NSMutableArray *game;
NSMutableArray *gameDetails;
NSMutableArray *gameMoves;
}
-(id)initWithString:(NSString*)PGNString;
-(void)getArrayOfFile;
-(NSMutableArray*)getNumberOfGamesFromArrayOfFiles;
-(NSMutableArray*)getAParticularGame:(int)gameNumber;
-(NSMutableArray*)getDetailsOfTheGame:(int)gameNumber;
-(NSMutableArray*)getArrayOfMoves:(int)gameNumber;

@end

PgnPrserBrain.m
=========================================================================

//
// PgnPrserBrain.m
// parsingPGN
//User has the sole responsibility and uses this at his own risk no guarentees from the programmer who
//created this program
// Created by Amarendra singh on 02/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PgnPrserBrain.h"
#import "Moves.h"

@implementation PgnPrserBrain


-(id)initWithString:(NSString*)PGNString
{
NSCharacterSet *ch=[NSCharacterSet characterSetWithCharactersInString:@"[]"];

NSArray *temp=[PGNString componentsSeparatedByCharactersInSet:ch];
contentsOfFile=[[NSMutableArray alloc]initWithArray:temp];
[self getArrayOfFile];
[self getNumberOfGamesFromArrayOfFiles];
// NSLog(@"------%d",contentsOfFile.count);
return self;
}

// cleaning the array made of PGN file of new line white spaces and carrige returns

-(void)getArrayOfFile
{
arrayToBereturned=[[NSMutableArray alloc]init];
for (int i=0;i<contentsOfFile.count; i++)
{
if(!([[contentsOfFile objectAtIndex:i]isEqual:@"\n"]||[[contentsOfFile objectAtIndex:i]isEqual:@""]||[[contentsOfFile objectAtIndex:i]isEqual:@"\r\n"]))
[arrayToBereturned addObject:[contentsOfFile objectAtIndex:i]];
}
[contentsOfFile release];
contentsOfFile=nil;
// NSLog(@"%d",arrayToBereturned.count);

}

//Get number Of games from the Array of files it returns an array in which starting index of all games are listed count this array to get the no of games

-(NSMutableArray*)getNumberOfGamesFromArrayOfFiles
{ NSString *compareString;
NSArray *tempArray=[[arrayToBereturned objectAtIndex:0]componentsSeparatedByString:@"\""];
startIndexOfFiles=[[[NSMutableArray alloc]init]autorelease];
for (int i=0; i<arrayToBereturned.count; i++)
{
compareString=[[[arrayToBereturned objectAtIndex:i]componentsSeparatedByString:@"\""] objectAtIndex:0];
if([[tempArray objectAtIndex:0]isEqual:compareString])
[startIndexOfFiles addObject:[NSNumber numberWithInt:i ]];
}

return startIndexOfFiles;
}


//Returns the particular number game from the list of games in one pgn file

-(NSMutableArray*)getAParticularGame:(int)gameNumber
{ game=[[[NSMutableArray alloc]init]autorelease];
int startIndex=[[startIndexOfFiles objectAtIndex:(gameNumber-1)]integerValue];
int end_index;
if(gameNumber<startIndexOfFiles.count)
end_index=[[startIndexOfFiles objectAtIndex:gameNumber]integerValue]-1;
else
end_index=arrayToBereturned.count-1;

for (int i=startIndex;i<=end_index;i++) {
[game addObject:[arrayToBereturned objectAtIndex:i]];
}
return game;

}

//getting deeper it returns details of a particular game

-(NSMutableArray*)getDetailsOfTheGame:(int)gameNumber
{
gameDetails=[[[NSMutableArray alloc]init]autorelease];
NSMutableArray *temp=[self getAParticularGame:gameNumber];
for (int i=0;i<(temp.count-1); i++)
{
[gameDetails addObject:[temp objectAtIndex:i]];
}

return gameDetails;
}
//this section extracts move from movetext
-(NSMutableArray*)getArrayOfMoves:(int)gameNumber
{
gameMoves=[[[NSMutableArray alloc]init]autorelease];
NSMutableArray *temp=[self getAParticularGame:gameNumber];
NSMutableString *movesString=[[temp objectAtIndex:(temp.count-1)] stringByTrimmingCharactersInSet:[NSMutableCharacterSet whitespaceAndNewlineCharacterSet]] ;
//================changes done to accomodate all file format =================

 NSString *formattEnforcedMoveString1= [movesString stringByReplacingOccurrencesOfString:@". " withString:@"."];
    NSString *formattEnforcedMoveString=[formattEnforcedMoveString1 stringByReplacingOccurrencesOfString:@".\n" withString:@"."];


NSArray *tempDirtyMoves=[formattEnforcedMoveString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \r\n"]];

NSMutableArray *tempArrayOfMoves=[[NSMutableArray alloc]init];
for(int i=0;i<tempDirtyMoves.count;i++)
{
if(!([[tempDirtyMoves objectAtIndex:i]isEqual:@"\n"]||[[tempDirtyMoves objectAtIndex:i]isEqual:@""]||[[tempDirtyMoves objectAtIndex:i]isEqual:@"\r\n"]))
[tempArrayOfMoves addObject:[tempDirtyMoves objectAtIndex:i]];

}






NSPredicate *blackPredicate = [NSPredicate predicateWithFormat:@" not SELF contains '.'"];

NSMutableArray *tempCleanedMovesBlack=[[NSMutableArray alloc]initWithArray:tempArrayOfMoves];
[tempCleanedMovesBlack filterUsingPredicate:blackPredicate];

NSPredicate *whitePredicate = [NSPredicate predicateWithFormat:@" SELF contains '.'"];

NSMutableArray *tempCleanedMovesWhite=[[NSMutableArray alloc]initWithArray:tempArrayOfMoves];
[tempCleanedMovesWhite filterUsingPredicate:whitePredicate];

NSMutableArray *tempCleanedMoveWhiteWithoutDots=[[NSMutableArray alloc]init ];
for(int i=0;i<tempCleanedMovesWhite.count;i++)
{
[tempCleanedMoveWhiteWithoutDots addObject:[[[tempCleanedMovesWhite objectAtIndex:i] componentsSeparatedByString:@"."] objectAtIndex:1]];
}






NSLog(@"%d",tempCleanedMovesBlack.count);
NSLog(@"%@",tempArrayOfMoves);
NSLog(@"%d",tempCleanedMoveWhiteWithoutDots.count);
[tempArrayOfMoves release];
tempArrayOfMoves=nil;
Moves *mv;
for(int i=0;i<tempCleanedMoveWhiteWithoutDots.count;i++)
{
mv=[[Moves alloc ]initWithMoves:[tempCleanedMoveWhiteWithoutDots objectAtIndex:i] balck: [tempCleanedMovesBlack objectAtIndex:i] ] ;
[gameMoves addObject:mv];
}
if(tempCleanedMoveWhiteWithoutDots.count==tempCleanedMovesBlack.count)
[[gameMoves objectAtIndex:(tempCleanedMoveWhiteWithoutDots.count-1)] setBlackMove:@""];
NSLog(@"%@",[[gameMoves objectAtIndex:(tempCleanedMoveWhiteWithoutDots.count-1)] whiteMove]);
NSLog(@"%@",[[gameMoves objectAtIndex:(tempCleanedMoveWhiteWithoutDots.count-1)] blackMove]);
if(arrayToBereturned){
[arrayToBereturned release];
arrayToBereturned=nil;
}



return gameMoves;
}

@end