[Tutor] Tutor Digest, Vol 106, Issue 42
Peter Otten
__peter__ at web.de
Tue Dec 18 08:56:47 CET 2012
Marefe Serentas wrote:
> On 12/18/2012 12:38 AM, tutor-request at python.org wrote:
>> Re: Get the structure values from a c file
> I apologize for some syntax errors in my c file. I would like to correct
> my mistakes and append some lines in the c file to make it clearer.
>
>
> ----
> #define max (3)
> #define max_data_size (9600*2*8)
>
> typedef unsigned char u8;
> typedef signed int u32;
>
> int some_data[] =
> {
> -288, -153, 31, 24, 205, 110, 39, 88,
> -281, 145, 35, 266, 63, -79, -103, -25,
> 53, 145, -114, -274, 46, 60, 220, 205
> };
>
> typedef struct A
> {
> u32 a;
> u8 b;
> }A;
>
> typedef struct MainStruct
> {
> A var1;
> u32 var2;
> u32 var3[max];
> u32 var4;
> u32 var5[max];
> }MainStruct;
>
> void generate()
> {
> MainStruct myMain = {0};
> myMain.var1.a = 1;
> myMain.var1.b = 'a';
> myMain.var2 = 3;
> myMain.var3[0] = -3;
> myMain.var3[1] = 6;
> myMain.var3[2] = 18;
>
> (void)memcpy((void *)myMain.var4,
> (void *)some_data,
> sizeof(some_data));
>
> myMain.var5[0] = 1;
> }
>
> ----
>
>
> This is a work-problem. Given a c file as input, the customer wants me
> to write a python script that will retrieve the values of myMain.
> He wants those values converted to binary data and write it in a .bin
> file. As a result, the size of the bin file is equal to the size of
> myMain. The c file input content might change in the future like
> different values assigned, added fields in the MainStruct, etc. Also I
> am not permitted to change the c file.
>
>
> About me, I'm a fresh graduate. I just started learning python a month
> ago. I learned c in school, we had it for 2 years.
>
>
> I'm using python 2.6. I'm running on Windows 7 64-bit OS.
>
> What I did so far is parse the C source code.
> But having a problem parsing the value of myMain.var4.
How about running the C source with an added dump() function
dump(MainStruct *s, char *filename)
{
FILE * f = fopen(filename, "wb");
fwrite(f, sizeof(MainStruct), 1, f);
fclose(f);
}
in the right places?
More information about the Tutor
mailing list