Indent tool and K&R code style

来源:互联网 发布:淘宝上找代理商 编辑:程序博客网 时间:2024/06/05 09:00

          为了使得我们写的code代码看上去更加工整美观,我们可以借助一些工作来完成这种技术含量低却耗时的工作,用的比较多的有Indent和Astyle.

        Indent工具可以指定某种style,例如K&R style,GNU style,Kerkely或者Linux style,其实style只是一些options的集合,所以我们也可以自己指定这些option来定制自己喜欢的style,这些具体的option项包括如何放置函数或者句子后面的“{}”, 如何空行,空格,断行,缩进,设置comment的格式等等,具体的选项可以去查阅官方文档.

        Astyle工具也可以指定style,没有那么多控制选项,难以去定制,没有Indent灵活。


        下面有两个shell脚本例子,第一个是用Indent工具将code调整成K&R style, 其中的注释列出了K&R stye所包含的option,看英文基本可以明白其意思,如还有不清楚就可去查文档; 第二个脚本,是用在git 项目管理中,用于检查patch的格式是否符合K&R style,这在实际项目管理中很有用,例如gstreamer就在pre-commit中就用Indent去做格式检查。

   1) shell script to  convert *h and *c code to K&R style:

#! /bin/bash

# Convert your file to a K&R indent code style through indent tool
# All *.c and *.h file or files in specified folder will be converted
# to K&R code indent style.

# Kernighan & Ritchie code style
# The Kernighan & Ritchie style is used throughout their well-known
# book The C programming Language. It is enabled with the '-kr' option.
# The Kernighan & Ritchie style corresponding to following set of
# options, both long term options and abbreviation works:

# -nbad : --no-blank-lines-after-declarations
# -bap  : --blank-lines-after-procedures
# -bbo  : --break-before-boolean-operator
# -nbc  : --no-blank-lines-after-commas
# -br   : --braces-on-if-line
# -brs  : --braces-on-struct-decl-line
# -c33  : --comment-indentation33
# -cd33 : --declaration-comment-column33
# -ncdb : --no-comments-delimiters-on-blank-lines
# -ce   : --cuddle-else
# -ci4  : --continuation-indentation4
# -cli0  : --case-indentation0
# -cp33 : --else-endif-column33
# -cs   : --space-after-cast
# -d0   : --line-comments-indentation
# -di1  : --declaration-indentation
# -nfc1 : --dont-format-first-column-comments
# -nfca : --dont-format-comments
# -hnl  : --honour-newlines
# -i4   : --indent-level4
# -ip0  : --parameter-indentation0
# -l75  : --line-length75
# -lp   : --continue-at-parentheses
# -npcs : --no-space-after-function-call-names
# -nprs : --no-space-after-parentheses
# -saf  : --space-after-for
# -sai  : --space-after-if
# -saw  : --space-after-while
# -nsc  : --no-space-after-casts
# -nsob : --leave-optional-blank-lines
# -nss  : --dont-space-special-semicolon

# Check for existence of indent, and error out if not present.
# On some *bsd systems the binary seems to be called gnunindent,
# so check for that first.
version=`gnuindent --version 2>/dev/null`
if test "x$version" = "x"; then
  version=`indent --version 2>/dev/null`
  if test "x$version" = "x"; then
    echo "Did not find GNU indent, please install it before continuing."
    exit 1
  fi
  INDENT=indent
else
  INDENT=gnuindent
fi

case `$INDENT --version` in
  GNU*)
      ;;
  default)
      echo "Did not find GNU indent, please install it before continuing."
      echo "(Found $INDENT, but it doesn't seem to be GNU indent)"
      exit 1
      ;;
esac

# Convert code indent style
INDENT_PARAMETERS="-kr"
echo "Convert code to Kernighan & Ritchie style: "
for file in `find $1 -name "*.[hc]*"`; do
   echo "processing file : $file"
   $INDENT ${INDENT_PARAMETERS} $file -o $file 2>> /dev/NULL
done

echo "conversion done!"


     2) Shell script used in Git to check code style when commit patch

         This script named pre-commit , you need to place it in one repo project source code,  specifically ./git/hooks/pre-commit, when you need to execute "git commit" command to create one patch,  this "pre-commit" will be triggered and code style will be checked, if not comply to pre-defined style, git commit command will failed.

#!/bin/sh
#
# Check that the code follows a consistant code style
#

# Check for existence of indent, and error out if not present.
# On some *bsd systems the binary seems to be called gnunindent,
# so check for that first.

version=`gnuindent --version 2>/dev/null`
if test "x$version" = "x"; then
  version=`indent --version 2>/dev/null`
  if test "x$version" = "x"; then
    echo " git pre-commit hook:"
    echo "Did not find GNU indent, please install it before continuing."
    exit 1
  fi
  INDENT=indent
else
  INDENT=gnuindent
fi

case `$INDENT --version` in
  GNU*)
      ;;
  default)
      echo " git pre-commit hook:"
      echo "Did not find GNU indent, please install it before continuing."
      echo "(Found $INDENT, but it doesn't seem to be GNU indent)"
      exit 1
      ;;
esac

INDENT_PARAMETERS="-kr"

echo "--Checking style--"
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.[hc]"` ; do
    # nf is the temporary checkout. This makes sure we check against the
    # revision in the index (and not the checked out version).
    nf=`git checkout-index --temp ${file} | cut -f 1`
    newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
    $INDENT ${INDENT_PARAMETERS} \
    $nf -o $newfile 2>> /dev/null
    # FIXME: Call indent twice as it tends to do line-breaks
    # different for every second call.
    $INDENT ${INDENT_PARAMETERS} \
        $newfile 2>> /dev/null
    diff -u -p "${nf}" "${newfile}"
    r=$?
    rm "${newfile}"
    rm "${nf}"
    if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Code style error in: $file                                                                      "
echo "                                                                                                 "
echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
echo " If the whole file is to be committed, this should work (run from the top-level directory):      "
echo "                                                                                                 "
echo "   ./kr_style.sh $file; git add $file; git commit"
echo "                                                                                                 "
echo "================================================================================================="
        exit 1
    fi
done
echo "--Checking style pass--"








0 0
原创粉丝点击