unpacking an array of structs...
unpacking an array of structs...
am 23.02.2010 07:29:07 von php.list
I have a desktop app that has a data structure that looks like this:
typedef struct MANGOpie
{
unsigned char mango;
unsigned short pie;
}
MANGOpie;
I manage a C array of these things in memory:
MANGOpie * pies = (MANGOpie *)malloc(count*sizeof(MANGOpie));
I pass these to a PHP script on my webserver who needs to unpack the
array of structs.
The unpack() PHP function appears to be what I need, but it doesn't
like the formatting I'm using to describe an array of these structs:
"(Cmango/npie)*"
What it doesn't like are the parentheses. I've tried brackets and
curlies too, but nothing works. I have to have the parentheses to tell
the parser to repeat the entire struct:
mango
pie
mango
pie
mango
pie
....
Formatting without the parentheses -- "Cmango/npie*" -- is:
mango
pie
pie
pie
pie
pie
....
One workaround is to drop the struct and just manage two separate
parallel arrays of each data type in the desktop app:
unsigned char * mangos = (unsigned char
*)malloc(count*sizeof(unsigned char));
unsigned short * pies = (unsigned short
*)malloc(count*sizeof(unsigned short));
With PHP unpack() format strings:
"Cmango*"
"npie*"
But, I'd rather keep the struct for the sake of code clarity and neatness.
Another would be to iterate thru the binary data, unpacking one struct
at a time, but that would be slower, presumably.
Anyone know the trick to this?
Thanks.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: unpacking an array of structs...
am 23.02.2010 07:56:28 von Rene Veerman
have you considered using json as transport?
http://json.org/ has code you can re-use.
On Tue, Feb 23, 2010 at 7:29 AM, php.list@juun.com wrot=
e:
>
> I have a desktop app that has a data structure that looks like this:
>
> typedef struct MANGOpie
> {
> =A0 unsigned char =A0 mango;
> =A0 unsigned short =A0pie;
> }
> MANGOpie;
>
>
>
> I manage a C array of these things in memory:
>
> MANGOpie * pies =3D (MANGOpie *)malloc(count*sizeof(MANGOpie));
>
>
>
>
> I pass these to a PHP script on my webserver who needs to unpack the arra=
y
> of structs.
>
> The unpack() PHP function appears to be what I need, but it doesn't like =
the
> formatting I'm using to describe an array of these structs:
>
> "(Cmango/npie)*"
>
> What it doesn't like are the parentheses. =A0I've tried brackets and curl=
ies
> too, but nothing works. =A0I have to have the parentheses to tell the par=
ser
> to repeat the entire struct:
>
> mango
> pie
> mango
> pie
> mango
> pie
> ...
>
>
>
> Formatting without the parentheses -- "Cmango/npie*" -- is:
>
> mango
> pie
> pie
> pie
> pie
> pie
> ...
>
>
>
> One workaround is to drop the struct and just manage two separate paralle=
l
> arrays of each data type in the desktop app:
>
> unsigned char * =A0 mangos =3D (unsigned char =A0*)malloc(count*sizeof(un=
signed
> char));
> unsigned short * =A0pies =A0 =3D (unsigned short *)malloc(count*sizeof(un=
signed
> short));
>
> With PHP unpack() format strings:
>
> "Cmango*"
> "npie*"
>
> But, I'd rather keep the struct for the sake of code clarity and neatness=
..
>
>
>
> Another would be to iterate thru the binary data, unpacking one struct at=
a
> time, but that would be slower, presumably.
>
>
>
>
>
>
>
>
>
> Anyone know the trick to this?
>
> Thanks.
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: unpacking an array of structs...
am 23.02.2010 08:17:41 von Nathan Nobbe
On Monday, February 22, 2010, php.list@juun.com wrote:
>
> I have a desktop app that has a data structure that looks like this:
>
> typedef struct MANGOpie
> {
>  unsigned char  mango;
>  unsigned short  pie;
> }
> MANGOpie;
>
>
>
> I manage a C array of these things in memory:
>
> MANGOpie * pies =3D (MANGOpie *)malloc(count*sizeof(MANGOpie));
>
>
>
>
> I pass these to a PHP script on my webserver who needs to unpack the arra=
y of structs.
>
> The unpack() PHP function appears to be what I need, but it doesn't like =
the formatting I'm using to describe an array of these structs:
>
> "(Cmango/npie)*"
>
> What it doesn't like are the parentheses. Â I've tried brackets and c=
urlies too, but nothing works. Â I have to have the parentheses to tell=
the parser to repeat the entire struct:
>
> mango
> pie
> mango
> pie
> mango
> pie
> ...
>
>
>
> Formatting without the parentheses -- "Cmango/npie*" -- is:
>
> mango
> pie
> pie
> pie
> pie
> pie
> ...
>
>
>
> One workaround is to drop the struct and just manage two separate paralle=
l arrays of each data type in the desktop app:
>
> unsigned char *  mangos =3D (unsigned char  *)malloc(count*siz=
eof(unsigned char));
> unsigned short *  pies  =3D (unsigned short *)malloc(count*siz=
eof(unsigned short));
>
> With PHP unpack() format strings:
>
> "Cmango*"
> "npie*"
>
> But, I'd rather keep the struct for the sake of code clarity and neatness=
..
>
>
>
> Another would be to iterate thru the binary data, unpacking one struct at=
a time, but that would be slower, presumably.
>
>
>
>
>
>
>
>
>
> Anyone know the trick to this?
I'm curious how you are getting to the point of calling pack() in the
first place. can we see the bit of your script that interacts with
this c code?
-nathan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: unpacking an array of structs...
am 23.02.2010 08:20:12 von php.list
I'm actually moving from a string-encoded transport to binary for
compactness. The array can potentially get pretty large. I'm shooting
for the smallest possible representation of the data, which is 1 char
and 1 short per data point.
On February 23, 2010, Rene Veerman wrote:
> have you considered using json as transport?
> http://json.org/ has code you can re-use.
>
> On Tue, Feb 23, 2010 at 7:29 AM, php.list@juun.com
> wrote:
>>
>> I have a desktop app that has a data structure that looks like this:
>>
>> typedef struct MANGOpie
>> {
>> unsigned char mango;
>> unsigned short pie;
>> }
>> MANGOpie;
>>
>>
>>
>> I manage a C array of these things in memory:
>>
>> MANGOpie * pies = (MANGOpie *)malloc(count*sizeof(MANGOpie));
>>
>>
>>
>>
>> I pass these to a PHP script on my webserver who needs to unpack the array
>> of structs.
>>
>> The unpack() PHP function appears to be what I need, but it doesn't
>> like the
>> formatting I'm using to describe an array of these structs:
>>
>> "(Cmango/npie)*"
>>
>> What it doesn't like are the parentheses. I've tried brackets and curlies
>> too, but nothing works. I have to have the parentheses to tell the parser
>> to repeat the entire struct:
>>
>> mango
>> pie
>> mango
>> pie
>> mango
>> pie
>> ...
>>
>>
>>
>> Formatting without the parentheses -- "Cmango/npie*" -- is:
>>
>> mango
>> pie
>> pie
>> pie
>> pie
>> pie
>> ...
>>
>>
>>
>> One workaround is to drop the struct and just manage two separate parallel
>> arrays of each data type in the desktop app:
>>
>> unsigned char * mangos = (unsigned char *)malloc(count*sizeof(unsigned
>> char));
>> unsigned short * pies = (unsigned short *)malloc(count*sizeof(unsigned
>> short));
>>
>> With PHP unpack() format strings:
>>
>> "Cmango*"
>> "npie*"
>>
>> But, I'd rather keep the struct for the sake of code clarity and neatness.
>>
>>
>>
>> Another would be to iterate thru the binary data, unpacking one struct at a
>> time, but that would be slower, presumably.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Anyone know the trick to this?
>>
>> Thanks.
>>
>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: unpacking an array of structs...
am 23.02.2010 08:34:54 von php.list
In the desktop app's memory the data is packed end-to-end already:
typedef struct MANGOpie
{
unsigned char mango;
unsigned short pie;
}
MANGOpie;
MANGOpie * pies = (MANGOpie *)malloc(count*sizeof(MANGOpie));
....and the entire 'pies' array is sent to the PHP script as binary data
using PUT.
On February 23, 2010, Nathan Nobbe wrote:
> On Monday, February 22, 2010, php.list@juun.com wrote:
>>
>> I have a desktop app that has a data structure that looks like this:
>>
>> typedef struct MANGOpie
>> {
>> unsigned char mango;
>> unsigned short pie;
>> }
>> MANGOpie;
>>
>>
>>
>> I manage a C array of these things in memory:
>>
>> MANGOpie * pies = (MANGOpie *)malloc(count*sizeof(MANGOpie));
>>
>>
>>
>>
>> I pass these to a PHP script on my webserver who needs to unpack the
>> array of structs.
>>
>> The unpack() PHP function appears to be what I need, but it doesn't
>> like the formatting I'm using to describe an array of these structs:
>>
>> "(Cmango/npie)*"
>>
>> What it doesn't like are the parentheses. I've tried brackets and
>> curlies too, but nothing works. I have to have the parentheses to
>> tell the parser to repeat the entire struct:
>>
>> mango
>> pie
>> mango
>> pie
>> mango
>> pie
>> ...
>>
>>
>>
>> Formatting without the parentheses -- "Cmango/npie*" -- is:
>>
>> mango
>> pie
>> pie
>> pie
>> pie
>> pie
>> ...
>>
>>
>>
>> One workaround is to drop the struct and just manage two separate
>> parallel arrays of each data type in the desktop app:
>>
>> unsigned char * mangos = (unsigned char
>> *)malloc(count*sizeof(unsigned char));
>> unsigned short * pies = (unsigned short
>> *)malloc(count*sizeof(unsigned short));
>>
>> With PHP unpack() format strings:
>>
>> "Cmango*"
>> "npie*"
>>
>> But, I'd rather keep the struct for the sake of code clarity and neatness.
>>
>>
>>
>> Another would be to iterate thru the binary data, unpacking one
>> struct at a time, but that would be slower, presumably.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Anyone know the trick to this?
>
>
> I'm curious how you are getting to the point of calling pack() in the
> first place. can we see the bit of your script that interacts with
> this c code?
>
> -nathan
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php