Browse Source

Add astyle code formatter usage script and change .gitignore to exclude astyle preserved files

tags/lab02
Silver Kits 3 years ago
parent
commit
5abb360450
2 changed files with 51 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 48
    0
      tooling/format-code.sh

+ 3
- 0
.gitignore View File

@@ -1,6 +1,9 @@
1 1
 # Build artefacts
2 2
 bin/*
3 3
 
4
+# astyle formatter preserved files
5
+*.orig
6
+
4 7
 #
5 8
 # Templates from https://github.com/github/gitignore
6 9
 #

+ 48
- 0
tooling/format-code.sh View File

@@ -0,0 +1,48 @@
1
+#!/bin/bash
2
+
3
+# Simple script for code formatting in 1tbs
4
+# See http://astyle.sourceforge.net/astyle.html for syntax and defaults
5
+
6
+MINPARAMS=1
7
+ORIG_SUFFIX=orig
8
+
9
+if [ $# -lt "$MINPARAMS" ]
10
+then
11
+    echo "This script needs C source files passed as arguments"
12
+    echo "USAGE: format-code.sh src/main.c src/somecode.c ..."
13
+    exit 1
14
+fi
15
+
16
+# 
17
+for FILE in "$@"
18
+do
19
+    RESULT="$(astyle    --style=1tbs \
20
+                        --indent-col1-comments \
21
+                        --break-blocks \
22
+                        --pad-oper \
23
+                        --pad-header \
24
+                        --delete-empty-lines \
25
+                        --add-brackets \
26
+                        --convert-tabs \
27
+                        --max-code-length=80 \
28
+                        --break-after-logical \
29
+                        --mode=c \
30
+                        --suffix=.$ORIG_SUFFIX \
31
+                        --lineend=linux \
32
+                        $FILE)"
33
+
34
+    # if file unchanged print unchanged result message
35
+    if [[ "$RESULT" = Unchanged* ]]
36
+    then
37
+        echo "$RESULT"
38
+    fi
39
+
40
+    # if file formatted print result and renamed file name
41
+    if [[ "$RESULT" = Formatted* ]]
42
+    then
43
+        echo "$RESULT"
44
+        echo "Original code was preserved in file $FILE.$ORIG_SUFFIX"
45
+    fi
46
+done
47
+
48
+exit 0

Loading…
Cancel
Save