Archived

This topic is now archived and is closed to further replies.

McZ

parsing text..

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites