[plug] Reading a horrible C file in PHP.

Shayne O'Neill shayneo at bestflights.com.au
Mon Aug 7 18:05:32 WST 2006


I have infact been eyeing off writing a swig wrapper for the function
(I've done a fair bit with python and swig), but honestly its goofy as
hell, and I wonder whether it'd be kosher with the hosting company. 

Hmm. Must think thru. 

-----Original Message-----
From: plug-bounces at plug.org.au [mailto:plug-bounces at plug.org.au] On
Behalf Of Kirk Turner
Sent: Monday, 7 August 2006 4:49 PM
To: plug at plug.org.au
Subject: Re: [plug] Reading a horrible C file in PHP.

On 8/7/06, Shayne O'Neill <shayneo at bestflights.com.au> wrote:
>
> Ok. I've been tasked to convert this horrible C cgi to php.

>
> The stupid thing reads in a data file like this;-

...
>
> Nasty huh?
>


Its not exactly a nasty way - as more an efficient way - unless as you
have to read it in another language that doesn't support structs. Its
essentially just writing out memory for the struct directly in a form of
binary serialisation (you'll see this in many a C data function).

However that said it should be fairly straight forward in the way it is
handled looking at it (and a hexdump -C should reveal some of this too).
It should look something like this:

flightid - an int coming from 32 bytes (byte order and size will depend
on the platform) name - the next 256 bytes, you'll need to search for
the 0x00 byte (NULL, or \0 character) and termiate the string there -
how to convert it into a php string you'll have to work out, php is not
my strong point.
price - next 128 bytes
description - next 65535 bytes
50x depature struct following the same process

and then repeat ad nauseum for each part.

You might find it easier to convert the data in C itself into a format
that is a bit more compatible with PHP (XML for example) or if you need
to have compatability between C and PHP write an extension in php to
load it (there might already be one that converts structs to php classes
for example?)

Kirk
_______________________________________________
PLUG discussion list: plug at plug.org.au
http://www.plug.org.au/mailman/listinfo/plug
Committee e-mail: committee at plug.linux.org.au



More information about the plug mailing list