Re: porting C code to php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jul 06, 2009 at 05:24:32PM +0100, Steve Hanselman wrote:

> Hi,
> 
> I've some C code that processes files, I want to port this to PHP and am trying to decide the best way to port the structs and unions over so as to still be readable?
> 
> The files are multi-line files with differing layouts depending on the data contained within them, so at present unions of structs work very well for this.
> 
> I'm loathe to turn this code into substr's of the input line, has anybody any other ideas?

You could get fancy and handle this with different classes for each
different filetype. Build a parent class which is extended for each
child class.

Or you could just use arrays. Since arrays in PHP can store any type of
data in virtually any configuration, they are roughly equivalent to a
struct in C. The union part isn't necessary, since you can simply
redefine an array or define a different array, depending on the
circumstances.

Your last paragraph puzzled me. I'm guessing that you're reading from
the file some header info (to determine the file type) and then reading
the rest of the file one struct at a time? If so, no, you can't do that
in PHP that way. You could use an fread() call, and read in the number
of bytes you want for a given struct/array, but you're still going to
have to parse the bytes to put them into their respective array members.
You might also try fscanf(), but I'm not sure it will understand binary
data in your file (like floats). Also look at pack() and unpack(), as
these will, to some extent, read and write binary values.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux