delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/18/17:14:18

Message-ID: <F77915E7F086D31197F4009027CC81C91CC664@probe-2.as-london.acclaim.com>
From: Shawn Hargreaves <SHargreaves AT acclaimstudios DOT co DOT uk>
To: djgpp AT delorie DOT com
Subject: Re: Allegro datafiles and binary data
Date: Tue, 18 Jan 2000 16:28:46 -0000
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2650.21)
Reply-To: djgpp AT delorie DOT com

Matt Firewalker writes:
> If I were to place a binary file named foo.255 (the extension
> doesn't matter, or does it?)

Extensions are used by the dat utility to guess what type of object
you are trying to import, and by the grabber to filter which files
are displayed by the selector when importing into objects of a known
type. But the dat utility has options to override the default
guessing of file types, and grabber will list all files when importing
into an object of unknown type like raw binary data.

> into a Allegro datafile, and that binary file had a single string
> (e.g. "Big things"), how would I load that string into a char
> pointer or object?

You generally don't want to copy the data somewhere else: the datafile
system has already loaded it into memory for you (that's the whole
point of using a datafile, after all!) so it's better to just use the
existing copy.

In the case of a binary data object, the dat field points to the data,
and the size field points to the size. eg.

   char *my_data = (char *)datafile[objectnum].dat;

   for (i=0; i<datafile[objectnum].size; i++)
      printf("Character %d = %c\n", i, my_data[i]);

If you do want to copy this info somewhere else, just call memcpy().
But be warned: text strings in particular are terminated by a zero
in C, but binary objects just store whatever was in your original
file, so you'll either have to put a NULL terminator into your file
(assuming an editor capable of dealing with text files that contain
zeros, which many are not :-) or you'll need to add the zero yourself
at runtime.

If you are doing a lot of work with this sort of info, you may find
it useful to write your own loader function that will read binary 
data from disk directly into a NULL-terminated block of memory, and
then hook in this routine to be used when reading objects of your 
new type (using register_datafile_object()). If you are feeling
really fancy, you can also write a grabber plugin to add better
import/export routines and display/editing abilities for your new
type of data, rather than just having to treat them as raw binary
blocks all the time: see tools/plugins/plugins.txt for info about
this.


	Shawn Hargreaves.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019