OH GAWD! Text parsing in cocoa?!?!?

Member
Posts: 22
Joined: 2007.05
Post: #1
Still plugging away at my little hand history replay tool and now we come to the hard part.... gulp. It seems that parsing text files is one of the shortcomings of cocoa. I have seen everything from combinations of NSPredicate with NSScanner and NSString to third party classes that implement regular expressions. OUCH! Not stuff a new coder looks forward to playing with but I will take my lumps like everyone else. To my question:

Given text files that need to be parsed for game metadata like so:

Code:
Full Tilt Poker Game #2780441762: $1 + $0.25 Sit & Go (Turbo) (21205130), Table 1 - 15/30 - No Limit Hold'em - 22:44:32 ET - 2007/06/26
The button is in seat #9
*** HOLE CARDS ***
Dealt to hypnotx [Qc Kh]
BALLN 62: let get it started
RedDawg420 has 15 seconds left to act
...
BALLN 62 calls 70
*** FLOP *** [5c 8s Js]
BALLN 62 checks
...
hypnotx folds
BALLN 62 folds
Uncalled bet of 325 returned to msludwick
msludwick mucks
msludwick wins the pot (415)
*** SUMMARY ***
Total pot 415 | Rake 0
Board: [5c 8s Js]
Seat 1: ChipJS (small blind) folded before the Flop
...
Seat 8: icedtea68 folded on the Flop
Seat 9: hypnotx (button) folded on the Flop

I had envisioned creating a Hand class that contains the metadata for the hand which would include game number, players, seats, streets, etc. But I have to admit that I have arrived at a part of my project that seems more than a little daunting. Given that I would like to stay away from using third party software (if at all possible). What is the best approach to parsing a text file of this sort? If someone has some experience in this area please let me know.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #2
Quote:But I have to admit that I have arrived at a part of my project that seems more than a little daunting.

Don't let it intimidate you -- it'll always break your way if you focus on it.

You don't have to use Cocoa to parse text files if you don't want to. You can simply use standard C lib string functions and manual character compares if you wish. That said, it took me a while to discover that NSScanner is actually cleaner and easier (and more robust) than standard C lib, so I pretty much stick to NSScanner anymore. I admit that it is slightly confusing to pick up at first, but I think you'd eventually agree that it's worth learning how to use.Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
ANTLR v3 has a Cocoa runtime. I haven't tried it.

Either OGREKit or AGRegex give you regular expressions, or you can just use Oniguruma or PCRE straight.

Personally, I wouldn't touch NSScanner with a 10' pole, but maybe that's just me.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #4

It's not magic, it's Ruby.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #5
#include <stdio.h>
#include <string.h>
works for me...
* Joseph Duchesne hides because he doesn't understand ObjC very well
Quote this message in a reply
Member
Posts: 22
Joined: 2007.05
Post: #6
Thanks. CSRegex looks like a viable option and it does not seem quite as complex as using NSPredicate etc. Certainly gets around using the third party stuff.
Quote this message in a reply
⌘-R in Chief
Posts: 1,258
Joined: 2002.05
Post: #7
You don't use NSPredicate to parse text. You can't. That's simply not what it's for.

As for regular expressions, I prefer Oniguruma because you can switch the syntax style. OgreKit is rather huge so I created a lightweight wrapper around Oniguruma for a project so I could use it more efficiently, and it worked out well.

http://www.sethwillits.com/temp/OGRegexLite.zip
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Parsing from a string to something faster? Madrayken 3 3,178 Aug 10, 2009 03:32 PM
Last Post: smasher
  File parsing... the best way... BinarySpike 8 3,646 Nov 30, 2005 09:51 PM
Last Post: BinarySpike
  Getting text from a window text field mars 7 4,851 Mar 26, 2005 06:06 PM
Last Post: OneSadCookie