1

I am building a game in Visual Studio 2008 and in order to build levels, i will have two types of files (more than that actually but these two are the only ones important to my question) i need to parse. One will dictate the contents of a level and will look something like this:

LevelName = "Sample Level"

Object1Type = "CustomObject"

Object1File = "WideFloor_DF"

Object1Position = 600,600

Object2Type = "Circle"

Object2Position = 550, 500

Object2Mass = 5

etc.

The other will dictate the properties for custom objects, and look something like this:

Name = "Wide Floor"

Mass = 1

GeometryMap = "WideFloor_GM"

IsStatic = true

etc.

I'm not very familiar with regexes (read: i don't like them because the look too much like line noise to easily understand) and i was wondering if there is an easier method than using tons of regexes?

edit: i knew i forgot something! i really hate xml files, so i would really prefer not to use them (waaaaaaaay too verbose for me). edit 2: i like the format i've come up with and would prefer not to change.

RCIX
  • 38,647
  • 50
  • 150
  • 207
  • Interestingly enough, the sample Robert Harvey presented below requires less characters (less "verbose") than your format because it doesn't have to repeat "Object1" over and over. – Talljoe Jun 11 '09 at 05:51
  • 1
    Well, we've written a whole C# compiler without using any regular expressions. I think you can probably parse a language that is much simpler than C# without using regular expressions. Parsing a language is a well-studied, common problem, so there's lots of literature on it. Start by writing a tokenizer, a device which turns a stream of characters into a stream of tokens. Then build a parser that turns streams of tokens into a stream of whatever data structures these represent. – Eric Lippert Jun 11 '09 at 06:30
  • Thanks for the idea, and i might just do that at some point, but for now i'll stick with XML. – RCIX Jun 11 '09 at 06:41

7 Answers7

7

What about using XML files? You can read and write them easily using XMLReader and XMLWriter objects, and you won't have to deal with Regex at all.

<Level name="Sample Level">
   <Object type="CustomObject" file="WideFloor_DF" positionX="600" positionY="600" />
   <Object type="Circle" file="WideFloor_DF" positionX="500" positionY="500" mass="5" />
</Level>     
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Thanks for trying, and that way does look appealing, but i guess i have xmlphobia. Sorry! – RCIX Jun 11 '09 at 05:41
  • @RCIX, XML is a far more robust answer to this problem than your fragile regex solution. Get over your xmlphobia. – KingNestor Jun 11 '09 at 05:50
  • if you still don't like XML: http://www.yaml.org/ http://yaml-net-parser.sourceforge.net/ – russau Jun 11 '09 at 06:19
1

If you are going to have your files in human readable format anyway, why not use XML?

Tetraneutron
  • 32,841
  • 3
  • 25
  • 21
1

If you have control over the file format you might try something with an existing parser, like XML or INI files, both of which have parsers in .NET.

If you're sold on the above format, you might use a tool like Lex to generate your parser for you.

Talljoe
  • 14,593
  • 4
  • 43
  • 39
1

One alternate approach might be to define a class that has properties/collections to hold all of this configuration data, and make it [Serializable]. You can then populate out an example configuration, serialize it out to a file, and then you have a complete XML config file that you can edit by hand. When you're ready to use the configuration, just deserialize the XML file back into the config object in your app.

Andy White
  • 86,444
  • 48
  • 176
  • 211
1

If you absolutely insist on using that file format, you can build an object that reads the file line by line, pulling things from the left side of the '=' and putting it into a Dictionary as the Key and everything to the right of the '=' as the Value.

However, I must echo the above answers that say to use an XML document along with an XMLDocument object. Have you looked into LINQ to XML?

jasonh
  • 29,297
  • 11
  • 59
  • 61
  • My problem with XML is that it is kinda hard to build by hand. *sigh* but there may not be any other way... – RCIX Jun 11 '09 at 05:45
  • Use an XML Editor, like XML Notepad: http://www.microsoft.com/DownLoads/details.aspx?familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en – Robert Harvey Jun 11 '09 at 05:49
0

well, if i understood your question you want a way to structure the contents of the files so that parsing will be so much easier for you, XML is a good choice for that, and XSD also for describing the content of your xml files, LINQ to XML will make it very good to read it from C#.

abdelkarim
  • 586
  • 6
  • 11
0

If you use XML you can deserialise the files directly into an object so that you don't need to parse it at all.

Mark
  • 2,392
  • 4
  • 21
  • 42