Updates from June, 2007 Toggle Comment Threads | Keyboard Shortcuts

  • tech0x20 11:29 pm on June 10, 2007 Permalink | Reply  

    Turn pseudo-enums expressed as #defines into strings. 

    I have an include file that has 50+ #defines with constants (0,1,2,3,…,etc). I have a dump program to dump the data structure that these #defines are used in into a .csv file. I used this little script to dump case statements out for the #defines, with readable strings instead of arbitrary constant values.


    cat $1 | awk '/#define/{
    print "ttcase " $2 ":";
    print "tttfprintf(out_csv_fp,"," $2 "");";
    print "tttbreak;";
    }' > out.txt
     
  • tech0x20 11:14 pm on June 10, 2007 Permalink | Reply  

    Awk script to quote all parameters for a given function name. 

    I was trying to maintain a program that wrote 200+ lines to a single file, using a single repeated function call–many of which had all NULL parameters. There were also occasional loops involved. The data was position based, so NULL writes counted.

    I was just trying to figure out:

    1. How many times a given function was called.
    2. At what position actual data occurred.

    I created a stub function of the original offending function that logged to a file what # call this was and the data passed. Then I copied the offending functions’ caller and substituted offending function calls using this script, which quotes all passed parameters (note: already quoted parameters would need a little more work):


    BEGIN {
    OFS=","
    }
    /repeated_function_name/{

    for(i=1; i<=NF; i++)
    {
    if(i==1)
    {
    sub("[(]", "("", $i);
    }
    else
    {
    sub("^[ ]*",""", $i);
    }
    if(i==NF)
    {
    sub("[)]", "")", $i);
    }
    else
    {
    sub("[ ]*$", """, $i);
    }
    }

    print $0
    next;
    }
    { print $0; }

     
  • tech0x20 3:19 pm on May 29, 2007 Permalink | Reply  

    Awk to split c functions into multiple files. 

    This, at present, is overly simplistic and makes many stupid assumptions:

    • That the return type is short.
    • That there are no short global variables declared.
    • That everything is well-formatted so that the return type is always first column, and that there is a space between both function name and return type (as in, only spaces–no comments) and function name and open parenthesis of the arg list.


    cat $1 | awk '
    BEGIN {
    functionNameFile="";
    }
    /^short/ {
    /* assumes return type of short */
    /* assumes space between return type, function name, and parm list */
    functionNameFile=$2.".c";
    print $0 > functionNameFile;
    }
    /^}/ {
    print $0 >> functionNameFile;
    functionNameFile="";
    next;
    }
    {
    if(functionNameFile != "") {
    print $0 >> functionNameFile;
    }
    }
    '
     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: