cflat

cflat filters C source for easier searching. It should also work for reasonably C-like languages like C++, Java, or C#. If you are brave you might even use it on Perl code.

Basically cflat prints the source code in a tabular format where each line is prefixed with the scope of the line (usually the function declaration). This makes it easy to filter function bodies with grep or to do interesting things with awk, perl, etc. To get a better idea look at the example below.

cflat can also strip comments (C and C++ style) and string literals.

See the man page for details.

Download

cflat is free software distributed under the GPL.

cflat-0.1.0.tar.gz (source)
cflat-0.0.1.tar.gz (source)

See the files README and INSTALL in the tarball for more information.

Examples

Basic Operation

Source file:

#include <stdio.h>

int main()
{
    /* my first program */
    printf("Hello, world.\n");
    return 0;
}

Output (with TABs expanded to 20 spaces):

hello.c:1                               #include <stdio.h>
hello.c:2
hello.c:3                               int main()
hello.c:4                               {
hello.c:5           int main() {            /* my first program */
hello.c:6           int main() {            printf("Hello, world.\n");
hello.c:7           int main() {            return 0;
hello.c:8           int main() {        }

Comment Stripping

cflat -Tc hello.c prints hello.c without comments:

#include <stdio.h>

int main()
{
    
    printf("Hello, world.\n");
    return 0;
}

Comment Extraction

cflat -T --only c sourcefile.c | grep . can be used to print only the comments in sourcefile.c

Counting Functions

cflat sourcefile.c | cut -f2 | grep '()' | sort -u | wc -l counts the number of functions in sourcefile.c.

Listing Functions

cflat sourcefile.c | cut -f2 | grep '()' | uniq -c prints a list of the functions in sourcefile.c with an approximate line count for each function. The output for cflat.c for example looks like this:

      3 static void error_nomem() {
      6 static void error() {
      6 static void outc() {
      6 static void outs() {
      6 static void outint() {
     17 static scope_t * scope_new() {
      3 static void scope_free() {
      3 static void scope_clear() {
     26 static void scope_add() {
     10 static void scope_print_prefix() {
     17 static void start_line() {
    214 static int dofile() {
     20 static int dofilename() {
      6 static void init_states() {
     37 static void set_strip() {
      7 static void print_version() {
     24 static void print_help() {
    109 int main() {

Valid HTML 4.01!