[plug] Reading a horrible C file in PHP.
Kirk Turner
gameldar at gmail.com
Mon Aug 7 16:49:10 WST 2006
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
More information about the plug
mailing list