Hi Guys,
Please accept my apologies for the email clutter but I forgot the
attachment ....
On Wed, 2009-09-02 at 13:08 -0400, Neil Webster wrote:
> I am trying to create a reusable structure for reading in data from a
> binary data stream. I have attached a ragel file that demonstrates the
> problem I am having and would appreciate tips and advice on what I am
> doing wrong or better ways to go about solving the problem.
>
> I have defined Word8b, Word16b and Word32b to represent the blocks I
> will be reading from the data stream. The data is accumulated into a
> temporary variable (temp) and then assigned to the specific variable on
> exit from the appropriate machine. It works fine for small numbers but
> does strange things with numbers >127. It seems to be a problem with
> signed vs unsigned but I can't figure out what it is.
>
> What I expect
> a8 1 b8 ff a16 2 b16 fe a32 3 b32 fd
>
> What I get
>
> a8 1 b8 ffff a16 2 b16 fffe a32 3 b32 fffffffd
>
#include <stdio.h>
%%{
machine adcpData;
action clear {temp=0;}
action accum {temp = (temp << 8) + fc;}
Word8b = extend > clear $ accum ;
Word16b = extend{2} > clear $ accum ;
Word32b = extend{4} > clear $ accum ;
dataBlock = Word8b @{a8=temp;} Word8b @{b8=temp;} Word16b @{a16=temp;} Word16b @{b16=temp;} Word32b @{a32=temp;} Word32b @{b32=temp;} ;
main := dataBlock* ;
write data;
}%%
int main( )
{
//for ragel
int cs;
char *p;
char *pe;
//for my use
unsigned short int a8, b8, a16, b16 = 0;
unsigned int a32, b32 = 0;
unsigned int temp = 0;
unsigned char data[14] = {0x01,
0xff,
0x00,0x02,
0x00,0xfe,
0x00,0x00,0x00,0x03,
0x00,0x00,0x00,0xfd};
int offset = 0;
p = data;
pe = data + sizeof(data);
%%write init;
%%write exec;
printf("a8 %x b8 %x a16 %x b16 %x a32 %x b32 %x \n",a8,b8,a16,b16,a32,b32);
return 0;
}
_______________________________________________
ragel-users mailing list
[email protected]
http://www.complang.org/mailman/listinfo/ragel-users