converting from c struct to function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting converting from c struct to function
# 1  
Old 08-15-2011
converting from c struct to function

Hey Guys,

I need your help where I have a C structure and I want it to be converted into corresponding function.

Example:
Code:
typedef    struct
{
    unsigned long    LineNum;      //1025-4032
    unsigned short    KeyNum;    /*tbd*/
    char        Key;                      /*between 1-3*/
    char        Choice ;                /*1 if just changed*/
}MY_KEY_STATE;

...needs to be converted into function below:
Code:
void export_MY_KEY_STATE(cOBJ *OutStr, MY_KEY_STATE a)
{
    AddULongToObject(OutStr,   "LineNum",           a.LineNum     );  //1025-4032
    AddUShortToObject(OutStr,  "KeyNum",           a.KeyNum      ); /*tbd*/
    AddCharToObject(OutStr,    "Key",       a.Key  ); /*between 1-3*/
    AddCharToObject(OutStr,    "Choice",    a.Choice);                /*1 if just changed*/
}

So, can we do the above conversion using shell script on Linux/Unix?

Note: The comments in the struct as intact in the function.

Thanks!
skyOS

Last edited by Franklin52; 08-16-2011 at 08:40 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-24-2011
How about this perl script ? It would work also for an input file with various typedefs one after the other.
Code:
#!/usr/bin/perl
$debug = 0;
$/ = "typedef\s+struct";
while(<>) {
  %type2method = (
    unsigned_long => AddULongToObject,
    unsigned_short => AddUShortToObject,
    char => AddCharToObject,
  );
  if (m,\s*\{\s*(.*)\}\s*(.*?)\s*;,s) {
    $funtype = $2;
    print STDERR "funtype=$funtype"."\n" if $debug >= 1;
    @lines = split(/\r?\n/, $1);
    print "void export_$funtype(cOBJ *OutStr, $funtype a)\n\{\n";
    foreach $line (@lines) {
      $line =~ s/^\s*//;
      @parts = split(/;/, $line);
      $comment = @parts[$#parts];
      @tokens = split(/\s+/, @parts[0]);
      $varname = @tokens[$#tokens];
      $type = join("_", @tokens[0..$#tokens-1]);
      $method = $type2method{$type};
      print STDERR "type=$type, method=$method, varname=$varname, comment=$comment\n" if $debug >= 1;
      print "\t${method}(OutStr,\ŧ\"$varname\",\ta.$varname\t);$comment\n";
    }
    print "\}\n";
  } else {
    print STDERR "no typedef pattern match found\n";
    exit 1;
  }
}


Last edited by hfreyer; 08-24-2011 at 10:11 AM.. Reason: added explication
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Programming

Get struct definition

I have many headers with huge amount of structures in them, typical one looks like this: $ cat a.h struct Rec1 { int f1; int f2; }; struct Rec2 { char r1; char r2; }; struct Rec3 { int f1; float k1; float ... (6 Replies)
Discussion started by: migurus
6 Replies

3. Programming

Implementing function outside struct

I have this code where I have declared a struct with some functions. Trying to write the function implementation outside the struct declaration and do not know how to proceed. #ifndef ParseEl_hh #define ParseEl_hh #include <iostream> #include <fstream> #include "DynBaseObj.hh"... (7 Replies)
Discussion started by: kristinu
7 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

6. UNIX for Dummies Questions & Answers

struct winsize

what is struct winsize used for? i tried looking it up, but no luck. (0 Replies)
Discussion started by: l flipboi l
0 Replies

7. Programming

help with struct command in C

in C i am using this code to get the c time or a time or m time struct dirent *dir; struct stat my; stat(what, &my); thetime = my.st_ctime; How can i check if i have permission to check the c time of the file? (1 Reply)
Discussion started by: omega666
1 Replies

8. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

9. UNIX for Advanced & Expert Users

problem with netfilter hook function struct skbuff *sock is null..

iam trying to built a firewall.so i have used netfilter for it. in function main_hook sock_buff is returning null and in my log file continuously "sock buff null" is printed plse help to solve this problem.. (using print_string iam printing strings on current terminal (terminal we ping)) ... (1 Reply)
Discussion started by: pavan6754
1 Replies

10. Programming

struct tm problem

I receive an integer as argument for a function. within function definition i want it to be of type struct tm. eg.. main() { int a; ...... } function(...,..,a,..) int a; { struct tm tm; if(!a) ^ time(&a); ^ ... (4 Replies)
Discussion started by: bankpro
4 Replies
Login or Register to Ask a Question