• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.


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

Sir Derk

Text Parsing in C++

7 posts in this topic

I'm just wondering... how would u parse through text in c++? i'm pretty much a beginner here so i'm not to familiar with a lot of stuff. Umm...ok..say, for example, u have the text "hello world!" and u want to take "hello" and "world!" and put it in an array called..uhh...array... so in other words u want array[0] = "hello" and array[1] = "world!" and well generally, the seperator would be the space " ". so theoretically, the process would be for each character in the text, check if the character (being processed) is " " (a space). if it is, then store everything from that point to the last point processed to the variable array[array counter] and increament the array counter by 1. next character and well in vb coding it would look somewhat like this
dim i as long
dim lastpos as long
dim array(1) as string  'using 1 cuz i know how many items there are in the text :)
dim arraycounter as long

lastpos = 1
for i = 1 to len(text)
 if mid(text, i, 1) = " " then
  array(arraycounter) = mid(text, lastpos, i - lastpos - 1)
  arraycounter = arraycounter + 1 '*sigh* damn vb has no ++ op =(
  lastpos = i + 1
 end if
next i
well i just typed that out, out of the blue....but see..now i need to convert that into c. any suggestions? Edited by - Sir Derk on August 10, 2001 1:58:12 AM

Share this post

Link to post
Share on other sites
Use strtok();

// Example from MSDN

#include <string.h>

#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
printf( "%s\n\nTokens:\n", string );
// Establish string and get the first token:

token = strtok( string, seps );
while( token != NULL )
// While there are tokens in "string"

printf( " %s\n", token );
// Get next token:

token = strtok( NULL, seps );

Edited by - Sandman on August 10, 2001 6:42:48 AM

Share this post

Link to post
Share on other sites
wow thanx =) ..but...but...but...
what about searching for text? like if u want to parse through an html and sort out the tables and stuff...how would u do that?

so for things like

^ oo..that looked terrible without the source tags ^

so...i would have to first search for the <table> tag...then the <tr>, <td>, and take the "hello" value and such.

i've already coded this out in vb..but i just dunno where it is anymore..haha i've lost the coding =)

but this one, you would only want the stuff in the tables. anything else around the table you would ignore.

initially, i was thinking, find the <table>...</table> tags and store that into a variable and use the method posted above to parse through it, using the <td>, <tr>, </td>, </tr>'s as the seperator. BUT, how would i first search for the <table>...</table> tags ignoring everything else around it?? ahh! =)

AND this method would be the same as if i would be parsing through text like

"'Hello world';txtHello:4,'wowwie';txtWow:20,'bah';txtbah,'lala';txtlala:8"

and i wouldn't be able to use seperators cuz i need em in a specific order so

"Hello World", txtHello, and 4 is stored into a umm, type structure or a class

"wowwie", txtWow, and 20 in another

"bah", txtbah in another (notice it's missing a number value?

and lastly, "lala", txtlala, and 8 in the same

so u store the first var into a text variable, the second in a name field, and the last as a value field.

type[0].text = "Hello World"
type[0].name = "txtHello"
type[0].value = 4

type[1].text = "wowwie"
type[1].name = "txtWow"
type[1].value = 20
etc...etc... (while the "bah" one does not have a value for the value field so it would become default; 0)

wow this is getting complicated...hehe... ^-^...

ok..so all in all...
i'm not really asking how to code all that...i'm just wondering what C++ commands i should use.

strtok(); seperates the text using seperators and i can't use that method for that.

hmmmm...oo i know what's a good example!! Scripting Engines!!!

oy i can't remember the one i made, it's been so long.... i don't remember how i did it ... but i remember i used to have 2 big long string variable, one for a kind of variable system (associates ids into text) and then the real speech that knows what to display. (but i've only made one so i really have no experiance in making these engines =(). hmm now i'm wondering what's the better way...but let's not get side tracked...wow i'm like writing a story here...haha..i'll just post and see what i get ^^

^-^ Smile ^-^

Sir Derk

Edited by - Sir Derk on August 10, 2001 1:38:47 PM

Share this post

Link to post
Share on other sites
You should probably look at regular expressions (regex) to do that.

Boost.org got a class for using regex and you can find info on how to foumulate a regular expression to find a pattern in a text file. I think there''s some example on how to highlight text using HTML statement using this class and finding HTML links in a page. You could probably figure out the way to do what you want to do checking this code.


Share this post

Link to post
Share on other sites
Something I found about strtok() in a Linux manpage:
When using this funcion note that:

These functions modifies its first argument.

The identity of the delimiting character is lost.

These functions cannot be used on constant strings.

The strtok() function uses a static buffer while
parsing, so it''s not thread safe. Use strtok_r() if
this matters to you.

I don''t know whether that''s true for each and every C library out there, but I''d say that it''s pretty much it for strtok(). I even remember reading something like "DON''T USE IT!" somewhere else.

Share this post

Link to post
Share on other sites
Shadowdancer, you''re right, that is (as far as I know) the standard behavior of strtok(), it works the same way in Visual C++ 6. Wish I''d noticed that note the first time I played with strtok, gave me a royal headache trying to figure out how my source string kept getting corrupted .

I wouldn''t go so far as to say NEVER USE IT though, it''s pretty handy, just always make a copy of the original string to pass to the function so it doesn''t get destroyed. The only situation I''ve heard it is really bad in is multithreaded programming; it stores some data in static variables, so if two threads were both calling strtok, they''d mess up each other''s data.


Share this post

Link to post
Share on other sites
well if you are loading a whole bunch of stuff from a file and it is seperated by whitespace this is all you have to do:


vector<string> data;//vector to store strings

/*copies strings from file into the vector,
breaking them up by whitespace*/

copy (

//ok now that the file is loaded and broken up into

//words (yes in one line of code!) we want

//to find the table tag, to do that we can use

//find_if, but we need our own string to match

//assume we have other tags defined too

string tableTag("<table>");

vector<string>::iterator startPosition,endPosition;

startPosition = find_if(data.begin(),data.end(),tableTag);
//now we have an iterator to the opening tag;

endPosition = find_if(startPosition(),data.end(),tableCloseTag);
vector<string> tableData;//this will hold the table

//we had to add one to end because that is how iterators work

//just like how the length of an array is always one

//more than the index of the last element

I''m not going to write your whole program for you but can you see how much easier it is when using the standard library. In about eight lines of code I loaded a file, broke it apart into words and extracted a table. That''s why you should use string instead of char*, vector instead of arrays, copy with back_inserter instead of for loops, basically use C++ if you want to program in C++. This is just a very small example of what you can do with the standard library, it gets even more amazing when you get into function objects and some of the more advanced features. Pick up a book on the standard library, you won''t regret it. I have the one by Josuttis, it is pretty good.

Share this post

Link to post
Share on other sites
"''Hello world'';txtHello:4,''wowwie'';txtWow:20,''bah'';txtbah,''lala'';txtlala:8"
and i wouldn''t be able to use seperators cuz i need em in a specific order so
"Hello World", txtHello, and 4 is stored into a umm, type structure or a class
"wowwie", txtWow, and 20 in another"

Oh this is easy too. Do you recognize code like this:
int i;
double d;
MyStruct ms
cin >> i >> d >> ms;

wonder how that works? First let''s make one for a simple class that just has a char c followed by a float f:
istream& operator>> (istream& stream, MyStruct& ms)
stream >> ms.c >> ms.f;
return stream;//always do this so you can chain them

}//that''s it

now for more complex types (where you have to read in spaces as spaces for example, other than merely using them to seperate stuff) you need to read in a whole line using one of the stream functions and then you use string functions to break up the string and then I think you can use the input operator on a string_stream or whatever. You have to look this one up yourself. Or to keep things simple just replace all the spaces in your strings with some other character and then replace them, there is a standard function to do that easily, and it works on all containers too. Then you won''t have to worry about cutting up a whole line yourself. So in conclusion, use STL.

Share this post

Link to post
Share on other sites