Skip to main content
GameDev.net gamedev.net
🔒 Locked

parsing text..

Started by McZ Apr 21, 2004 at 3:20 AM 8 replies 550+ views
Original Post
McZ
McZ
I''m using fstream to read in text using the getline function.. but how can I parse different strings from that line? e.g. if I have a script file something like this Material "Marble" { Effect 3 .color.ambient = 0.3 0.3 0.3 .color.diffuse = 0.5 0.5 0.5 .texture[0].file = "textures\marble.tga" } how do I search for the keywords?
Freebe
Freebe
why not consider using xml. TinyXML is small and eay to use, and solves the "Just Another Text File Parser"-problem


http://www.grinninglizard.com/tinyxml/
-victory is mine!
McZ
McZ
I want to do it myself.. for educational purpose..

I want to know how I can make it work this way I can create some kind of script engine later.. but first I want to know a good/easy way to parse text for my material scripts and so, and settings files
flangazor
flangazor
Do you have a grammar specification?
Agony
Agony
One place to start looking would be stringstream. You can set it equal to a std::string, and then it will act like a std::stream. Thus, you can call things like getline() on it, to read substrings up to characters like ''.'', and read numeric values out of it just like any other stream.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
bobstevens
bobstevens
Boost's Spirit library is possibly one of the better ways, but it may be overkill if your parsing is not very complex. Take a compiler design class when you get a chance.

Edit: Did I just recommend boost? Ugh, how gamedev.net of me. Someone kill me if I start using D.

[edited by - bobstevens on April 21, 2004 10:18:30 AM]
flangazor
flangazor
quote:
Original post by bobstevens
Boost''s Spirit library is possibly one of the better ways, but it may be overkill if your parsing is not very complex. Take a compiler design class when you get a chance.

Edit: Did I just recommend boost? Ugh, how gamedev.net of me. Someone kill me if I start using D.

[edited by - bobstevens on April 21, 2004 10:18:30 AM]

Bison isn''t too bad if you can bear using the trainwreck known as C.
daerid
daerid
Boost Rawks

So does spirit.

So does D.

How very GD.net of me, too, I guess :-\


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
antareus
antareus
I was reminded of my love for boost::spirit today. So I''ll second or third that. It has a decent learning curve, but it is fun, just tinker with the examples for awhile.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Zipster
Zipster
boost::spirit is quite powerful and fun to play with, but I''d say the learning curve can grow appreciably depending on the complexity of the script/setting files you''re parsing.

I''m personally a fan of XML files, even more so since I started getting into C#/.NET development. And I just think they''re very, very cool.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.