From 6150318600b31c6c117ebf11f01b0510a47fd10c Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 19 Aug 2016 11:09:23 -0400 Subject: [PATCH] Created version 1, will autoformat, strip blank lines and trailing whitespace --- autoformat.sh | 27 +++++++++++++++++++++++++++ process.pl | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 autoformat.sh create mode 100755 process.pl diff --git a/autoformat.sh b/autoformat.sh new file mode 100755 index 0000000..46a2a7a --- /dev/null +++ b/autoformat.sh @@ -0,0 +1,27 @@ +#!/bin/zsh +# Format all c files +GLOB=( **/*.c ) +# Astyle options: +# --mode=c : c formatting +# -xc : Brace attached to class names +# --style=stroustrup : stroustrup style (similar to 1tbs) +# -j : Always add brackets (even on one line if statements) +# -s2 : Three spaces +# -xG : Indent modifiers +# -S : Indent switches +# -K : Indent cases +# -N : Indent namespaces +# -xn : Attach bracket to namespace +# -xl : Attach inlines +# -n : Don't make a backup +# -p : Pad operators +# -H : Pad header (space after if, for, while) +OPTS=( --mode=c -xc --style=stroustrup -j -s2 -xG -S -K -N -xn -xl -n -p -H ) +# Process the c files by adding the comment and removing newlines +perl "${0:h}/process.pl" $GLOB +# Colorize output if you can +if which colout>/dev/null; then + astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'|colout '(Formatted)' green||true +else + astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'||true +fi diff --git a/process.pl b/process.pl new file mode 100755 index 0000000..32411be --- /dev/null +++ b/process.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +use strict; +use autodie; +use warnings; +use File::Compare; +use File::Basename; +no warnings 'once'; + +foreach my $file (@ARGV) { + if (-e $file) { + my $data = process_file($file); + open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!"; + print MYOUTPUTFILE $data; + close(MYOUTPUTFILE); + if (compare($file, "$file.bak") != 0) { + print "Processed $file\n"; + } + unlink $file; + rename "$file.bak", $file; + } + +} +sub process_file { + my($filename) = "$_[0]"; + open(MYINPUTFILE, "<$filename") or die "x $!"; + my($data) = ""; + while() { + s/\s*$//; + my($line) = $_; + if($line !~ /^\s*$/) { + $data = $data . "$line\n"; + } + } + close(MYINPUTFILE); + chomp($data); + if ($data !~ /^\s*\/\*\*/) { + my $basename = basename($filename); + $data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data"; + } + return $data; +}