C, C++ 주석 인식해 비교하는 diff 스크립트 (sh, sed)

Posted at 2014/07/03 17:05 // in Programming // by Daniel

제가 잊어버릴까봐 공유합니다.

사용자 스크립트로 먼저 코멘트를 strip 하고
그 결과를 가지고 비교하여 diff 여부를 리턴합니다.

#!/bin/sh
	
  1. C, C++ comments aware diff
  2. this uses stripcomment script to strip comments

Usage=“Usage: {script name} {file1} {file2}”
if [ $# -eq 0 ]; then # if no argument specified echo $Usage # print Usage string value exit 1
fi
if [ $# -eq 1 ]; then # if no argument specified echo $Usage # print Usage string value exit 1
fi
FILE1=$1
FILE2=$2
TMPFILE1=/tmp/aa$$
TMPFILE2=/tmp/bb$$

~/bin/stripcomment $FILE1 > $TMPFILE1
~/bin/stripcomment $FILE2 > $TMPFILE2

#echo diff $TMPFILE1 $TMPFILE2
diff $TMPFILE1 $TMPFILE2
exit $?


위 스크립트는 제가 짠 거고

아래는 http://bash.cyberciti.biz/academic/sed-remove-c-cpp-comments/ 여기서 퍼왔습니다.

#! /bin/sed -nf

  1. Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)
  1. Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
  2. Works its way through the line, copying to hold space the text up to the
  3. first special character (/, “, ‘). The original version went exactly a
  4. character at a time, hence the greater speed of this one. But the concept
  5. and especially the trick of building the line in hold space are entirely
  6. merit of Brian.

:loop

  1. This line is sufficient to remove C++ comments!

/^\/\// s,.*,,

/^$/{

x p n b loop

}

/^”/{

:double /^$/{ x p n /^”/b break b double } H x s,\n\(.[^\”]*\).*,\1, x s,.[^\”]*,, /^”/b break /^\\/{ H x s,\n\(.\).*,\1, x s/.// } b double

}

/^’/{

:single /^$/{ x p n /^’/b break b single } H x s,\n\(.[^\’]*\).*,\1, x s,.[^\’]*,, /^’/b break /^\\/{ H x s,\n\(.\).*,\1, x s/.// } b single

}

/^\/\*/{

s/.// :ccom s,.[*]*,, /^$/ n /^\*\//{ s/..// b loop } b ccom

}

:break

H

x

s,\n\(.[^”’/]*\).*,\1,

x

s/.[^”’/]*//

b loop

크리에이티브 커먼즈 라이센스
Creative Commons License

이 글에는 트랙백을 보낼 수 없습니다