diff_parser.sh

#!/bin/bash


srcfile=new.c
diff=diff.txt


function parse_src_to_regions()
{
gawk '
BEGIN{




}


{


judge = $0~/^[0-z_]+(\[|\]|\*)*\s+(\[|\]|\*)*[0-z_]+\s*\(.*$/
if (judge){
split($0, strs, "\\(")
split(strs[1], strs2, "\\s+")
func_name = strs2[length(strs2)]
func_to_start[func_name] = NR
print(func_name":"NR)
}
}


END{
}




' $1
}


function get_regions_from_ranges()
{
gawk -F ":" '


BEGIN{
start = '$1'
end = '$2'
prevfunc = ""
prevline = -1
}


{
if ($2 > end){
exit;
}
if ($2 == end){
print($1":"$2)
exit;
}


if ($2 < start){
prevline = $2
prevfunc = $1
next
}


if ($2 == start){
print($1":"$2)
prevline = -1
next
}


if(prevline >= 0){
print("%#%"prevfunc":"prevline)
}


print($1":"$2)
}


END{


}


' $3
}


function get_start_to_end()
{
regions=`more $2`




for region in ${regions[*]}; do
OLD_IFS="$IFS"
IFS=":" 
strs=($region)
IFS="$OLD_IFS"
func_name=${strs[0]}
nr=$((${strs[1]}))
gawk '


BEGIN{
in_double_quote=0
in_quote=0
level=0
in_func=0
in_comment=0
}


{
if(NR < '$nr'){
next
}


if ($0~/^[:blank:]*\/\//){
next
}


if($0~/^[:blank:]*\/\*/){
in_comment = 1
}


split($0, char_array, "")
for(i=1; i<=length(char_array); ++i){
char = char_array[i]
if(char == "\\"){
i++
continue
}


if(in_comment == 1){
if(char != "*"){
continue
}
if(i==length(char_array) || char_array[i+1]!="/"){
continue
}
i++
in_comment = 0
continue
}


if(in_quote==0 && in_double_quote==0){
if(char=="/" && i<length(char_array)){
if(char_array[i+1]=="/"){
next
}else if(char_array[i+1]=="*"){
i++
in_comment=1
continue
}
}


if(char=="\x27"){
in_quote=1
continue
}


if(char=="\""){
in_double_quote=1
continue
}
}else{
if(in_quote==1){
if(match(char, /'\''/)){
in_quote=0
}
continue
}
if(in_double_quote==1){
if(match(char, /\"/)){
in_double_quote=0
}
continue
}
}


if(in_func==0){
if(!match(char, /\{/)){
continue
}
in_func = 1;
level = 1;
}else{


if(match(char, /\{/) || match(char, /\}/)){
if(match(char, /\{/)){
level=level+1
}else{
level=level-1
if(level==0){
print('$nr'":"NR)
exit
}
}
}


}
}


/* print(NR": "in_func" "level) */


if(in_func>0 && level==0){
print('$nr'":"NR)
exit
}
}s


END{


}


' $1
done
}


function print_by_ranges()
{
ranges=`more $2`
whole=""


for range in ${ranges[*]}; do
whole=$whole$range":"
done


gawk '


BEGIN{
cur=1
str="'$whole'"
split(str, strs, ":")
}


{


if(NR>=strs[cur] && NR<=strs[cur+1]){
print($0)
}
if(NR==strs[cur+1]){
cur+=2
}
}


' $1
}




parse_src_to_regions $srcfile \
| get_regions_from_ranges $1 $2 \
| get_start_to_end $srcfile \
| print_by_ranges $srcfile
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值