gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Writing a DAT file parser

#105113 - shadowghost21 - Fri Oct 06, 2006 2:28 am

I am writing a LegoCad program for the NDS in C using the PAlib. The parts library for the pc version of the program are in dats. Each DAT contains some coordinates ill post an example of a 1x1 brick

Code:

0 Brick  1 x  1
0 Name: 3005.dat
0 Author: James Jessiman
0 Original LDraw Part
0 LDRAW_ORG Part UPDATE 2002-03

0 BFC CERTIFY CCW

0 2002-05-07 KJM BFC Certification

0 BFC INVERTNEXT
1 16 0 24 0 6 0 0 0 -20 0 0 0 6 box5.dat

4 16 10 24 10 6 24 6 -6 24 6 -10 24 10
4 16 -10 24 10 -6 24 6 -6 24 -6 -10 24 -10
4 16 -10 24 -10 -6 24 -6 6 24 -6 10 24 -10
4 16 10 24 -10 6 24 -6 6 24 6 10 24 10

1 16 0 24 0 10 0 0 0 -24 0 0 0 10 box5.dat

1 16 0 0 0 1 0 0 0 1 0 0 0 1 stud.dat
0

0 is a comment 1, is an include (with the file to include at the end) and 4, is a quad. Now I need to take info from this DAT and put them into coordinates to draw bricks. im not quite sure how to go about this. If any one can give me some advice, example code (quick and dirty) to get an idea, it would save me many a headache for the next few days.

#105145 - sgeos - Fri Oct 06, 2006 12:03 pm

You'll want to write a filter that takes this file and spits out a C file. I'd use perl.

This is what I'm using to generate header files from tab delimited spreadsheet files:
Code:
#!/usr/bin/perl -w

while (defined($line = <STDIN>))
{
   $line =~ s/([^\t\n]+|)([\t]|)([^\t\n]+|)(?:[\t]|)([^\t\n]+|)(?:[\t]|)[\n]/#define\t$1$2$3\n/g;
   print STDOUT $line;
}

You'll want to write something similar. This can be run from the command line using cygwin. This runs the file jp.csv through two filters, amp2cr.pl and csv2def.pl, and spits out def.h.:
Code:
cat jp.csv | ./amp2cr.pl | ./csv2def.pl > def.h

You'll want to write a "dat2c.pl" filter. It can be used like this:
Code:
cat in.dat | ./dat2c.pl > out.c

Google is your friend. These keywords might help:
"perl" "tutorial" "regular expression" "backreference"

-Brendan

#105170 - wintermute - Fri Oct 06, 2006 3:23 pm

I'd be more inclined to use libfat and use sscanf to read the file into C/C++ variables, that way you're not restricted to parts which are defined at compile time.

Here's a code snippet which illustrates use

http://www.daniweb.com/code/snippet281.html
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#105207 - sgeos - Fri Oct 06, 2006 6:45 pm

I can't see compiling adding much to the build time, especially if the doesn't change (often). The data is going to live in ROM either as a struct/LUT or a file in the filesystem. Is there something inherently better about the file approach that I'm missing?

-Brendan

#105208 - Sausage Boy - Fri Oct 06, 2006 6:55 pm

It's for the DS, so most people will use memory cards. It's nicer to just drop in another file on your memory card than to rebuild the entire program.


edit: spelling
_________________
"no offense, but this is the gayest game ever"


Last edited by Sausage Boy on Sat Oct 07, 2006 11:44 am; edited 1 time in total

#105267 - sgeos - Sat Oct 07, 2006 3:17 am

Sausage Boy wrote:
It's for the DS, so most people will use memory cards. It's nicer to just drop in another file on your memory card than to rebuilt the entire program.

Neat. That is a good reason.

-Brendan