CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Text parser

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Text parser

    Hi Guys,

    Just a quick question. I need to write a text parser which basically reads in data from the file.. The would basically define all the objects on my screen. and will have elements like mass , gravity , name and more importantly a array of vertices that build up the object et c.
    The name of the object is a string .. the vertices are a 3d vector gravity is a 3d vector and mass is a float .

    But not all the variables are required for all the objects on screen.For example some values are left to be initialized in the code while others are over ridden by values in the file.

    This means that the data is nt regular in the input file. Also I need to parse in various data types from the text file and do it for all the objects on the screen.

    This process needs to be repeated for the "n" objects on my screen.

    Currently I have writtern a parser which can loop a file and find out the particular key for the value and retrieve the value associated with the key. Say if there is a Mass : 10 my parser can retrieve 10 if i provide a string "Mass" as a input . But then repeated looping the file is cumbersome and I felt this isnt the best way to do this sort of thing . does anyone have a good way to do this??

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Text parser

    Is the format of the text file set in stone or can you change it? It'd be far easier to change your text file than write a parser. If you put it into JSON format, there are lots and lots of JSON parsers out there, same goes for XML.

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Text parser

    In that case XML is also a sane alternative. However the basic thing remains that you want to model your data that is currently in the file also in some way in your memory. In that case you only need to read the file once from disk into memory and can then look at values and manipulate them directly.

    It seems like you need something like std::map (key -> value mapping). And then your objects have the following variables:
    std::string : the object name
    float : the mass
    std::vector<std::vector<float> > : 3d array for the vertices and gravity

    Since you can already parse the datafile, you just need to read out the values and assign them to the correct variable in memory.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Mar 2009
    Posts
    20

    Re: Text parser

    Ya i was doing just about the same thin .. reading it into the memory before the execution of the program and then manipulating the values there on.. Jus wanted to make sure if there was any other better way to program than this.

    Thanks for your replies guys

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured