Generating data structures by parsing plain text files

I wrote a file parser for a game I'm writing to make it easy for myself to change various aspects of the game (things like the character/stage/collision data). For example, I might have a character class like this:

class Character { public: int x, y; // Character's location Character* teammate; }

I set up my parser to read in from a file the data structure with syntax similar to C++

Character Sidekick { X = 12 Y = 0 } Character AwesomeDude { X = 10 Y = 50 Teammate = Sidekick }

This will create two data structures and put them in a map<std::string, Character*>, where the key string is whatever name I gave it (in this case Sidekick and AwesomeDude). When my parser sees a pointer to a class, like the teammate pointer, it's smart enough to look up in the map to fetch the pointer to that data structure. The problem is that I can't declare Sidekick's teammate to be AwesomeDude because it hasn't been placed into the Character map yet.

I'm trying to find the best way to solve this so that I can have my data structures reference objects that haven't yet been added to the map. The two easiest solutions that I can think of are (a) add the ability to forward declare data structures or (b) have the parser read through the file twice, once to populate the map with pointers to empty data structures and a second time to go through and fill them in.

The problem with (a) is that I also can decide which constructor to call on a class, and if I forward declare something I'd have to have the constructor be apart from the rest of the data, which could be confusing. The problem with (b) is that I might want to declare Sidekick and AwesomeDude in their own files. I'd have to make my parser be able to take a list of files to read rather than just one at a time (this isn't so bad I guess, although sometimes I might want to get a list of files to read from a file). (b) also has the drawback of not being able to use data structures declared later in the constructor itself, but I don't think that's a huge deal.

Which way sounds like a better approach? Is there a third option I haven't thought of? It seems like there ought to be some clever solution to this with pointer references or binding or something... :-/ I suppose this is somewhat subjective based on what features I want to give myself, but any input is welcome.

--------------Solutions-------------

When you encounter the reference the first time, simply store it as a reference. Then, you can put the character, or the reference, or whatever on a list of "references that need to be resolved later".

When the file is done, run through those that have references and resolve them.

Well, you asked for a third option. You don't have to use XML, but if you follow the following structure, it would be very simple to use a SAX parser to build your data structure.

At any rate, instead of referencing a teammate, each character references a team (Blue team in this case). This will decouple the circular reference issue. Just make sure you list the teams before the characters.

<team>Blue</team>

<character>
<name>Sidekick</name>
<X>12</X>
<Y>0</Y>
<teamref>Blue</teamref>
</character>

<character>
<name>Sidekick</name>
<X>10</X>
<Y>50</Y>
<teamref>Blue</teamref>
</character>

Personally, I'd go with b). Splitting your code into Parser and Validator classes, both operating on the same data structure. The Parser will read and parse a file, filling the data structure and storing any object references as their textual names, leaving the real pointer null in your structure for now.

When you are finished loading the files, use the Validator class to validate and resolve any references, filling in the "real" pointers. You will want to consider how to structure your data to make these lookups nice and fast.

Will said exactly what I was about to write. Just keep a list or something with the unsolved references.

And don't forget to throw an error if there are unsolved references once you finish reading the file =P

Instead of storing Character object in your map, store a proxy for Character. The proxy will than contain a pointer to the actual Character object when the object is loaded. The type of Character::teammate will be changed to this proxy type. When you read in a reference that is not already in your map, you create a proxy and use the proxy. When you load an character which you already have an empty proxy in the map, populate it with your newly loaded character. You may also want to add a counter to keep track of how many empty proxy you have in the map so you know when all referenced characters have been loaded.

Another layer of indirection....it always make programming easier and slower.

One option would be to reverse the obligation. The Map is responsible for filling in the reference

template<T> class SymbolMap // I never could rememeber C++ template syntax
{
...

/// fill in target with thing name
/// if no name yet, add it to the list of thing that will be name
void Set(T& target, std::string name);

/// define name as target
/// go back and fill in anything that needs to be name
void Define(T target, std::string name);

/// make sure everything is resolved
~SymbolMap()
}

that won't interact well with value/moving semantics but I suspect that not much will.

Category:c# Time:2009-04-28 Views:1

Related post

  • Parsing of data structure in a plain text file 2010-02-09

    How would you parse in Java a structure, similar to this \\Header (name)\\\ 1JohnRide 2MarySwanson 1 password1 2 password2 \\\1 block of data name\\\ 1.ABCD 2.FEGH 3.ZEY \\\2-nd block of data name\\\ 1. 123232aDDF dkfjd ksksd 2. dfdfsf dkfjd .... etc

  • Creating a complex data structure by parsing an output file 2012-02-21

    I'm looking for some advice on how to create a data structure by parsing a file. This is the list i have in my file. '01bpar( 2)= 0.23103878E-01 half_life= 0.3000133E+02 relax_time= 0.4328278E+02', '01bpar( 3)= 0.00000000E+00', '02epar( 1)= 0.4999896

  • python parse plain text file with ^M carriage returns 2011-05-19

    Possible Duplicate: Make the readline method of Python recognize both end-of-line variations? Hello, I have a plain text file that uses ^M as carriage returns. In my OSX text editors it appears well formatted with spaces, but in vi it shows as one li

  • How to parse plain text file with occasional XML tags using Java and SAX? 2012-02-07

    I have a rather large log file from a server which contains plain text. The server logs every thing it does and occasionally it prints xml tags which I am interested in parsing. To give you an example: -----------log file------------- bla bla bla ran

  • Parsing plain text to some structured object 2010-04-26

    I am working on parsing plain text and converting it to key-value pairs. For example, plain text: some_uninteresting_thing key1 valueA, some_uninteresting_thing valueB key2 valueD key3 some_uninteresting_thing valueE key4 valueG(valueH, valueI) key5

  • A command-line method to generate a Word document from a plain text file 2011-02-01

    We are required to use Word documents for part of our project documentation. However, I would like to put information into the Word document automatically each build. For building, we have a set of build scripts written in Python. I'd like to know if

  • Parse timestamps in plain text file and count them per 5 minute intervals 2011-06-29

    My Input is a plain text file containing 6,000 timestamps, looks like this 2011-06-21 13:17:05,905 2011-06-21 13:17:11,371 2011-06-21 13:17:16,380 2011-06-21 13:17:20,074 2011-06-21 13:17:20,174 2011-06-21 13:17:24,749 2011-06-21 13:17:27,210 2011-06

  • How do i parse a text file in c# 2008-11-19

    How do i parse a text file in c#? --------------Solutions------------- Check this interesting approach, Linq To Text Files, very nice, you only need a IEnumerable<string> method, that yields every file.ReadLine(), and you do the query. Here is

  • Scraping a plain text file with no HTML? 2011-12-08

    I have the following data in a plain text file: 1. Value Location : Value Owner: Value Architect: Value 2. Value Location : Value Owner: Value Architect: Value ... upto 200+ ... The numbering and the word Value changes for each segment. Now I need to

  • optimizing Code for inserting 27000*2 keys from plain text file to DB 2012-04-30

    I need to insert data from a plain text file, explode each line to 2 parts and then insert to the database. I'm doing in this way, But can this programme be optimized for speed ? the file has around 27000 lines of entry DB structure [unique key (ext,

  • Is there a clever way to parse plain-text lists into HTML? 2009-06-17

    Question: Is there a clever way to parse plain-text lists into HTML? Or, must we resort to esoteric recursive methods, or sheer brute force? I've been wondering this for a while now. In my own ruminations I have come back again and again to the brute

  • Parsing a text file with Python? 2009-10-21

    I have to do an assignment where i have a .txt file that contains something like this p There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain... h1 this is another example of what this text file loo

  • How to read the xml file and write its content into a plain text file? 2010-03-15

    How to read the below xml file and write its content into a plain text file? <!-- <config> <compare> <source>d:\demo\</source> <destination>e:\demo\</destination> <parameters> <parameter>FileName<

  • Intent for editing plain text file with the installed file editor (if any) 2010-12-27

    I want my app to open a plain text file with any of the file editors installed on my terminal, but I keep getting this exception: ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDIT dat=file:///sdcard/folder

  • how can i use a Perl script to create the plain text file 2011-08-01

    How can use a Perl script to create the plain text file. Perl should generate (random 16 bit hex) input data. ASAP help needed. --------------Solutions------------- If you are new to Perl take a look at http://learn.perl.org Check out Open, hex, and

  • Routines for parsing a text file in C++ 2011-11-01

    I would like to parse a text file using C++. I know the syntax of the file and from the computer science point of view I dont think that I have any problems. However, I dont know exactly how to implement the parser in C++. I think there are a number

  • Embedding hidden encoded bits in plain text file 2012-01-14

    I'm designing a system to process plain text files, one of its features will be to move processed files to an archive server once they've been completely processed through the system. What I want to do is tag a text file once its been completely proc

  • How to remove duplicate words from a plain text file using linux command 2009-06-04

    I have a plain text file with words, which are separated by comma, for example: word1, word2, word3, word2, word4, word5, word 3, word6, word7, word3 i want to delete the duplicates and to become: word1, word2, word3, word4, word5, word6, word7 Any I

  • How can I get plain text file in Flash? 2009-10-22

    Which function can I use to get the content of a plain text file? I can just read "value=32" as a variable or file in xml format now, but the file from vendor contains only the value "32". --------------Solutions------------- Oops: Only now did I not

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.638 (s). 13 q(s)