|
-
May 8th, 2009, 07:19 AM
#1
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??
-
May 8th, 2009, 07:35 AM
#2
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.
-
May 8th, 2009, 09:53 AM
#3
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.
-
May 8th, 2009, 12:55 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|