Hi,
I’m trying to write a tile-engine for my game, but unfortunately i’m not
very good at stuff like string parsing in c/c++. Originally i
implemented all this in ruby, where it was very easy (simple language +
regexp support = happy me), but in c/c++ i’m kindof at a loss (despite
the easy format). Anyway, this is basically how i’m storing information
in my tileset file:
Name: Foo
Description: something about Bar
Tiles: 2
Name: Grass,grass.bmp,#,1
Name: Mountain,mntn.bmp,^,2
Anyway, the spec isn’t too important, i just need to know how to get the
’Foo’ seperate from the 'Name: ’ and also how to parse the data between
the commas. I was thinking probably something like strchr() might work,
but i thought i would consult the experts. (I was hoping i could
somehow incorporate awk into a c program, but that doesn’t look possible).
Thanks!
Cameron Matheson
Cameron Matheson wrote:
Hi,
I’m trying to write a tile-engine for my game, but unfortunately i’m not
very good at stuff like string parsing in c/c++. Originally i
implemented all this in ruby, where it was very easy (simple language +
regexp support = happy me), but in c/c++ i’m kindof at a loss (despite
the easy format). Anyway, this is basically how i’m storing information
in my tileset file:
Name: Foo
Description: something about Bar
Tiles: 2
Name: Grass,grass.bmp,#,1
Name: Mountain,mntn.bmp,^,2
something like this :
char name[100]; // resize those correctly
char desc[100];
char buffer[1024];
fgets(buffer,1024,file);
sscanf (buffer,“Name: %s\n”,name);
fgets(buffer,1024,file);
sscanf (buffer,“Description: %s\n”,desc);
and so on…
Stephane
Anyway, this is basically how i’m storing information in my tileset
file:
Name: Foo
Description: something about Bar
Tiles: 2
Name: Grass,grass.bmp,#,1
Name: Mountain,mntn.bmp,^,2
Anyway, the spec isn’t too important, i just need to know how to get
the ‘Foo’ seperate from the 'Name: ’
While C has no builtin regex engine, the scanf() functions can do some
very simple parsing. For example, “%s” will find strings delimited by
whitespace characters, and “%” allows you to indirectly specify your
own delimiter. For example:
while (fgets(line, sizeof line, buf)) {
++lineno;
n = sscanf(line, "%[^:]: %s", name, value);
if (n != 2) {
fprintf(stderr, "syntax error on line %d", lineno);
return FALSE;
}
if (!store_information(name, value))
return FALSE;
}
See the sscanf manpages (or help screens) for more info…
b
Hey,
Thanks, i didn’t know about the *scanf functions (coming from a c++
background). Unfortunately, i’m kind of stuck right now… i’m using a
line similar to this one:
sscanf (buffer,“Name: %s\n”,name);
but it doesn’t seem to be able to handle white-space in %s? The line in
my file is like this:
Name: Test Tileset
but using that sscanf function call, the name variable (actually i’m
using tileSetName) only contains ‘Test’. Any idea why? I was under the
impression that %s woud be everything up to a newline.
Thanks again,
Cameron Matheson
Stephane Marchesin wrote:> Cameron Matheson wrote:
Hi,
I’m trying to write a tile-engine for my game, but unfortunately i’m
not very good at stuff like string parsing in c/c++. Originally i
implemented all this in ruby, where it was very easy (simple language
- regexp support = happy me), but in c/c++ i’m kindof at a loss
(despite the easy format). Anyway, this is basically how i’m storing
information in my tileset file:
Name: Foo
Description: something about Bar
Tiles: 2
Name: Grass,grass.bmp,#,1
Name: Mountain,mntn.bmp,^,2
something like this :
char name[100]; // resize those correctly
char desc[100];
char buffer[1024];
fgets(buffer,1024,file);
sscanf (buffer,“Name: %s\n”,name);
fgets(buffer,1024,file);
sscanf (buffer,“Description: %s\n”,desc);
and so on…
Stephane
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
Wow, sorry to reply to my own post like this, i guess i was a little hasty:
but it doesn’t seem to be able to handle white-space in %s? The line in
my file is like this:
Name: Test Tileset
but using that sscanf function call, the name variable (actually i’m
using tileSetName) only contains ‘Test’. Any idea why? I was under the
impression that %s woud be everything up to a newline.
I didn’t notice in the man page that it said ‘s’ was only non-whitespace
characters. I didn’t see a key that did include whitespace though. Is
there anything i can do short of using multiple keys and strcat()-ing
them together?
Thanks,
Cam
Cameron Matheson wrote:> Hey,
Thanks, i didn’t know about the *scanf functions (coming from a c++
background). Unfortunately, i’m kind of stuck right now… i’m using a
line similar to this one:
sscanf (buffer,“Name: %s\n”,name);
but it doesn’t seem to be able to handle white-space in %s? The line in
my file is like this:
Name: Test Tileset
but using that sscanf function call, the name variable (actually i’m
using tileSetName) only contains ‘Test’. Any idea why? I was under the
impression that %s woud be everything up to a newline.
Thanks again,
Cameron Matheson
Stephane Marchesin wrote:
Cameron Matheson wrote:
Hi,
I’m trying to write a tile-engine for my game, but unfortunately i’m
not very good at stuff like string parsing in c/c++. Originally i
implemented all this in ruby, where it was very easy (simple language
- regexp support = happy me), but in c/c++ i’m kindof at a loss
(despite the easy format). Anyway, this is basically how i’m storing
information in my tileset file:
Name: Foo
Description: something about Bar
Tiles: 2
Name: Grass,grass.bmp,#,1
Name: Mountain,mntn.bmp,^,2
something like this :
char name[100]; // resize those correctly
char desc[100];
char buffer[1024];
fgets(buffer,1024,file);
sscanf (buffer,“Name: %s\n”,name);
fgets(buffer,1024,file);
sscanf (buffer,“Description: %s\n”,desc);
and so on…
Stephane
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
As someone suggested try using different separator.
Below is example which reads from stdin.
The "Name: " prefix is first parsed if matched then the rest of the line is
returned in the buffer. It is also safe, because the buffer can’t be overrun.On Saturday 23 November 2002 10:41, Cameron Matheson wrote:
Wow, sorry to reply to my own post like this, i guess i was a little hasty:
but it doesn’t seem to be able to handle white-space in %s? The line in
my file is like this:
Name: Test Tileset
but using that sscanf function call, the name variable (actually i’m
using tileSetName) only contains ‘Test’. Any idea why? I was under the
impression that %s woud be everything up to a newline.
I didn’t notice in the man page that it said ‘s’ was only non-whitespace
characters. I didn’t see a key that did include whitespace though. Is
there anything i can do short of using multiple keys and strcat()-ing
them together?
#include <stdlib.h>
#include <stdio.h>
int main()
{
char buffer[1000];
int matches;
matches=fscanf(stdin,“Name: %1000[^\n]”, buffer);
printf(“\n”);
if(matches == 1)
printf(buffer);
printf(“\n”);
printf(“\n”);
return 0;
}