LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2015, 03:09 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Python Results Converted To C Struct Header File


I created python code that produce output in the form of:
Code:
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:
Code:
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.
 
Old 10-21-2015, 08:34 PM   #2
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
I guess py2c did not do what you want or expect??? What have you gotten so far?
 
Old 10-22-2015, 06:04 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

Why don't you just use the output from your first Python code as input to another Python program, that can then write the C header to file?

With your output as my input, and my short (28 lines) Python program, I get this result (I obviously use print() instead of write() here):
Code:
./met1973.py 
struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

{"moses-red-sea","1.00.03"},
{"genesis-snake","2.03"},
{"deliverance","5.0.010"},
        {NULL, NULL}
};
Maybe I am missing the point, but this is how I would do it. And, well, have actually done it! So, to reiterate; use the output from Python program 1 as input to Python program 2. Or even better, why don't you write a single Python program that does everything at once. Seems the most efficient to me, but I don't have the whole picture.

Best regards,
HMW

Last edited by HMW; 10-22-2015 at 06:05 AM.
 
1 members found this post helpful.
Old 10-22-2015, 09:55 AM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
HMW, thanks for your reply. It was suggested to me basically what you are saying. So essentially just treat it as regular Python 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.
 
Old 10-22-2015, 11:25 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by metallica1973 View Post
HMW, thanks for your reply. It was suggested to me basically what you are saying. So essentially just treat it as regular Python 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.
Yeah, it makes sense to me. Holler if you want some assistance!

Best regards,
HMW
 
Old 10-22-2015, 12:10 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I got a 19 line bash script
 
Old 11-09-2015, 09:30 AM   #7
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Thanks everyone 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:
Code:
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:
Code:
struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

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

Last edited by metallica1973; 11-09-2015 at 09:31 AM.
 
Old 11-09-2015, 10:07 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
ummm ... don't we need to see the 'blah blah blah' if we to know how you got the data in the correct format? I mean the bit you have shown is nice but is essentially just a list of print statements.
 
Old 11-09-2015, 10:50 AM   #9
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by metallica1973 View Post
Thanks everyone 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:
Code:
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:
Code:
struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

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

I opted for saving the static information in a variable instead. Thus you don't have to write more than once (or print in my case).
Here's my approach:
Code:
#!/usr/bin/env python3

out = """struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

"""

with open("infile.txt") as infile:
    for line in infile:
        line = line.rstrip()
        lineSplit = line.split("=")
        out += "{\"" + lineSplit[0] + "\",\"" + lineSplit[1] + "\"},\n"

out = out[:-1] # Remove the unwanted \n from the final loop

out += """
        {NULL, NULL}
};
"""

print(out)

exit(0)
Best regards,
HMW
 
Old 11-09-2015, 01:26 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I understand keeping it all in python was easier, but here was my shell script all the same
Code:
#!/usr/bin/env bash

cat >wp.h<<EOF
struct {

        char *name;
        char *fixed_version;

} filename_versions[] = {

$(while IFS== read -r name num
  do
    echo "{\"$name\",\"$num\"},"
  done)
         {NULL, NULL}
};
EOF
Which I ran as:
Code:
cat data | ./struct.sh
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split header from data in file using python pwc101 Programming 13 06-16-2009 07:27 PM
import c header file into a python script deathalele Programming 1 05-26-2009 09:50 AM
in which header file the "struct omap_dev" can find in 2.6.22 hallimanju Linux - Software 1 06-02-2008 07:25 AM
Header file for struct ifstats yhus Programming 1 12-28-2007 07:59 AM
Problem with extern struct object, in header file. Undefined type. RHLinuxGUY Programming 7 07-24-2006 12:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration