Python Results Converted To C Struct Header File

I created python code that produce output in the form of:

moses-red-sea=1.00.03
genesis-snake=2.03
deliverance=5.0.010

I need to take this output and create a "C" header file and have it look like this:

struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

{"moses-red-sea","1.00.03"},
{"genesis-snake","2.03"},
{"deliverance","5.0.010"},
        {NULL, NULL}
};

I have looked at Python modules "struct":

https://docs.python.org/2/library/struct.html

and "ctype"

https://docs.python.org/2/library/ctypes.html

but am not sure if thats the ticket. Can someone shed some light? Thank you in advance.

Its just more string processing. You don't need extra modules to do the job. Whatever code writes output in this form var=value , make it write as {"var","val"}

1 Like

balajesuri, thank you for your response. So essentially just treat it as regular output and just redirect the output to a regular file (ex. versions.h) so it can be used by the existing C code in place correct?

Yup. Something of this sort:

fh_out = open('versions.h', 'w')
...
...
fh_out.write('{"deliverance","5.0.010"},\n')
...
..
fh_out.close()

Do not include this file in more than one C file in the same program, this would either cause mass duplication of the data, or symbol errors on compilation due to it being defined repeatedly in every file you use.

Awesome, I will give you an update when its all said and done. Thanks

Thanks for pointing me in the right direction. Here is a snippet of what I did to help anyone else out who is trying to do the samething:

dest=open('wp.h', 'a+')
dest.write('struct {\n')
dest.write('{0:>12} {1:6}\n'.format('char','*name;'))
dest.write('{0:>12} {1:16}\n\n'.format('char','*fixed_versions;'))
dest.write('} filename_versions[] = {\n\n')
   blah blah blah .....
dest.write('{0:>19}\n'.format('{NULL, NULL}'))
dest.write('{0}\n'.format('};'))
dest.close()

A sample output:

struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

{"moses-red-sea","1.00.03"},
{"genesis-snake","2.03"},
{"deliverance","5.0.010"},
        {NULL, NULL}
};

Here's a simplified and easier version:

dest = open('wp.h', 'a+')

dest.write(
'''
struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

{"moses-red-sea","1.00.03"},
{"genesis-snake","2.03"},
{"deliverance","5.0.010"},
        {NULL, NULL}
};
'''
);

dest.close()

Trick here is to use the triple quotes (''' ... ''') ; ain't python cool B-)