快速简便地评论多行打印报表

时间:2016-04-10 03:04:21

标签: c shell

是否有快速简便的方法来评论多行打印报表?像这样的东西?我有很多用于调试的printf语句,这些语句遍布我的程序。我想注释掉每个printf(),除了那些包含" ACCEPT"或"拒绝"。我有几百个,但它们分散在重要的代码之间,所以我不能使用块注释。

                   lower_bound_of_big_boy_counter++;
                    printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
                    strings_line_tokens[lower_bound_of_big_boy_counter]);
                    printf("3389 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
                }
                strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
                integer_match_flag = 0; 
                float_match_flag = 0;
            }
        }
        if(keywords_match_flag1 != 1)
        {
            lower_bound_of_big_boy_counter++;
        }
        cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);
        printf("4006 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
        strings_line_tokens[lower_bound_of_big_boy_counter]);
        //int
        if(cmp_str9 == 0)
        {
            printf("3402 checking return stuff \n");
            return_match_flag = 1;
            lower_bound_of_big_boy_counter++;
            get_function_type_for_proper_return(symbol_table_functions, type,id, 
                                     scope_char, symbol_table_functions_counter, function_type_for_proper_return);
            printf("3407 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
            printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
            strings_line_tokens[lower_bound_of_big_boy_counter]);
            printf("3410 function_type_for_proper_return %s \n", function_type_for_proper_return);

4 个答案:

答案 0 :(得分:2)

由于您的基本代码为Cprintf语句[显然]用于调试,请允许我建议另一种方法。

将用于调试的printf更改为(例如)DEBUGPRINTF。这是一个CPP宏,可以使用编译器命令行选项(如-DDEBUG)编译为某些内容。请参阅我最近的回答:Why doesn't my simple C macro work?以获得更正式的描述。

附注:我在注意到Jonathan关于以类似方式进行此操作的评论之前写了这个当前的答案。

因此,您只需要进行一次更改,并且可以在编译选项的翻转时打开和关闭调试打印。几十年来我一直在使用这种技术,它对我很有帮助。

注意:您仍需要进行全局编辑一次才能获得此功能。

这是一个perl脚本(将通过./script < input > output调用):

#!/usr/bin/perl
# convert debug printf's

master(@ARGV);
exit(0);

# master -- master control
sub master
{

    while ($bf = <STDIN>) {
        chomp($bf);

        if ($bf =~ /^\s*printf[(]/) {
            doprintf($bf);
            next
        }

        print($bf,"\n");
    }
}

# doprintf -- process printf
sub doprintf
{
    my($bf) = @_;
    my($allowflg);

    $allowflg = ($bf =~ /ACCEPT|Reject/);
    unless ($allowflg) {
        $bf =~ s/printf/DEBUGPRINTF/;
    }

    print($bf,"\n");

    while (1) {
        last if ($bf =~ /;\s*$/);

        $bf = <STDIN>;
        last unless (defined($bf));
        chomp($bf);

        print($bf,"\n");
    }
}

答案 1 :(得分:1)

使用GNU sed的多行解决方案:

if(isset($current_date, $last_payment_date)) {

    if(strtotime('now') <= strtotime($last_payment_date)) {
    // code if user is able to access the page
    } else echo "You cannot access this page";

} else echo "You cannot access this page";

排除包含; **************** MACROS ********************* ; START PROGRAM START MACRO MOV AX, DATA ; Data Segment to AX Register MOV DS, AX ; HAVE DS Point to Data Segment ENDM ; END PROGRAM END MACRO MOV AH, 4CH ; END Program INT 21H ; Call DOS Service ENDM ; PRINT STRING TO OUTPUT PSTRING MACRO STR MOV AH,09H LEA DX,STR INT 21H ENDM ; Creates a New Line NEWLINE MACRO MOV DX, 0AH ; Input of string to DX MOV AH, 02H ; Write Char to standard output INT 21H MOV DL, 0DH ; MOV AH, 02H ; Carriage Return INT 21H ; ENDM ; Get CHAR Input GETINPUT MACRO INPUT MOV AH, 01H ; Receive input INT 21H ; Call DOS Service MOV INPUT, AL ; Store into Input Variable ENDM ; ********** END MACROS ******************* .MODEL .STACK 100H .DATA MSG1 db 'Choose A Char to Convert To ASCII: $' CHAR db ? ; Store Input Char REM db ? ; Remainder 8-bit storate QUOT db ? ; Quotient 8-bit storage COUNT db 0 ; Counts the stacks .CODE MAIN PROC START PSTRING MSG1 GETINPUT CHAR NEWLINE MOV AX, 0 ; Clear AX Register MOV AL, CHAR ; Move input to AL MOV DX, 0 ; Clear DX Register ; ********************** ; ; QUOTIENT STORED IN AL ; ; REMAINDER STORED IN AH ; ; ********************** ; myLoop: MOV DL, 10 ; Set Divisor to 10 DIV DL ; Divide AX by 10 MOV REM, AH ; Move Remainder into REM Variable MOV QUOT, AL ; Move Quotient into QUOT Variable MOV AX, 0 ; Clear AX MOV AL, REM ; Move REM to AL PUSH AX ; Push AX to Stack INC COUNT ; Increase Count by 1 MOV AL, QUOT ; Place Quotient Into AL MOV AH, 0 ; AH was altered, Zero it out CMP AL, 0 ; If No Quotient Remains we can exit JNZ myLoop ; Jump if NOT zero myLoop2: POP DX ; Pop from the stack into DX ADD DX, '0' ; To Ascii Char MOV AH, 02H ; Print Char Command INT 21H ; Call to DOS System DEC COUNT ; Decrement COUNT CMP COUNT, 0 ; Compare COUNT to 0 JNZ myLoop2 END MAIN ENDP END MAIN sed '/ACCEPT\|Reject/!{/^[\t ]*printf(/{:a;s/^/\/\/ &/;/;/!{n;Ta}}}' file.c 的所有行,它会注释掉所有以ACCEPT开头并以Reject结尾的行。

如果在printf行找不到结尾;,它会循环并注释掉后续行,直到找到;

答案 2 :(得分:0)

快速,简单,有效,过于热情

使用printf()注释文件中的所有sed行非常简单,如果使用的话,即使是多行注释:

sed -e '/printf(.*);/ { s%^%//%; n; }' -e '/printf(/,/);/ s%^%//%'

在样本行上,这会产生:

                   lower_bound_of_big_boy_counter++;
//                    printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
//                    strings_line_tokens[lower_bound_of_big_boy_counter]);
//                    printf("3389 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
                }
                strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
                integer_match_flag = 0; 
                float_match_flag = 0;
            }
        }
        if(keywords_match_flag1 != 1)
        {
            lower_bound_of_big_boy_counter++;
        }
        cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);
//        printf("4006 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
//        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
//        strings_line_tokens[lower_bound_of_big_boy_counter]);
        //int
        if(cmp_str9 == 0)
        {
//            printf("3402 checking return stuff \n");
            return_match_flag = 1;
            lower_bound_of_big_boy_counter++;
            get_function_type_for_proper_return(symbol_table_functions, type,id, 
                                     scope_char, symbol_table_functions_counter, function_type_for_proper_return);
//            printf("3407 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
//            printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
//            strings_line_tokens[lower_bound_of_big_boy_counter]);
//            printf("3410 function_type_for_proper_return %s \n", function_type_for_proper_return);

但是,如果任何printf()语句包含ACCEPTReject,那么这些行也会被注释掉。鉴于你有数百行应该被注释掉,还有一些(十个数量级)不应该被注释,最好是撤消错误注释掉的行。

精致,复杂,有效

我有另一种工作方式,它在第一行正确处理printfACCEPT Reject语句。它不会在printf语句的第二行或后续行中发现这些单词。

${SED:-sed} \
    -e '/printf(.*ACCEPT.*);/ b' \
    -e '/printf(.*Reject.*);/ b' \
    -e '/printf(.*);/ { s%^%//%; b
        }' \
    -e '/printf(.*ACCEPT/, /);/ b' \
    -e '/printf(.*Reject/, /);/ b' \
    -e '/printf(/, /);/ s%^%//%' \
    frag.c

奇怪的换行需要安抚BSD(Mac OS X)sed

输入:

                   lower_bound_of_big_boy_counter++;
                    printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                    strings_line_tokens[lower_bound_of_big_boy_counter]);
                    printf("3389 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
                }
                strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
                integer_match_flag = 0; 
                float_match_flag = 0;
            }
        }
        if(keywords_match_flag1 != 1)
        {
            lower_bound_of_big_boy_counter++;
        }
        cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);

        printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
        printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
        wimbol();
        printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();
        printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();
        printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();

        printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
        printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);

        //int
        if(cmp_str9 == 0)
        {
            printf("3402 checking return stuff\n");
            return_match_flag = 1;
            lower_bound_of_big_boy_counter++;
            get_function_type_for_proper_return(symbol_table_functions, type,id, 
                                     scope_char, symbol_table_functions_counter, function_type_for_proper_return);
            printf("3407 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
            printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
            strings_line_tokens[lower_bound_of_big_boy_counter]);
            printf("3410 function_type_for_proper_return %s\n", function_type_for_proper_return);

输出:

                   lower_bound_of_big_boy_counter++;
//                    printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
//                    strings_line_tokens[lower_bound_of_big_boy_counter]);
//                    printf("3389 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
                }
                strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
                integer_match_flag = 0; 
                float_match_flag = 0;
            }
        }
        if(keywords_match_flag1 != 1)
        {
            lower_bound_of_big_boy_counter++;
        }
        cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);

//        printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
        printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
        wimbol();
        printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        wimbol();
//        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
//                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();
        printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();
        printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        wimbol();

//        printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
        printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
        printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
//        printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
//                strings_line_tokens[lower_bound_of_big_boy_counter]);
        printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);
        printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
                strings_line_tokens[lower_bound_of_big_boy_counter]);

        //int
        if(cmp_str9 == 0)
        {
//            printf("3402 checking return stuff\n");
            return_match_flag = 1;
            lower_bound_of_big_boy_counter++;
            get_function_type_for_proper_return(symbol_table_functions, type,id, 
                                     scope_char, symbol_table_functions_counter, function_type_for_proper_return);
//            printf("3407 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
//            printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n", 
//            strings_line_tokens[lower_bound_of_big_boy_counter]);
//            printf("3410 function_type_for_proper_return %s\n", function_type_for_proper_return);

我以前遇到过一些问题;我正在为其中一些人做准备(排序 - 在开始多线程之前处理所有单行printf()语句至关重要)。 b(跳转到脚本结束)和n(阅读下一行)之间存在更微妙的差异,这也是至关重要的。

我在Mac OS X 10.11.4,JFTR上使用BSD sed进行测试。 GNU sed不需要脚本中的额外换行符。

答案 3 :(得分:0)

使用sed是单向转换,在编译之前需要单独的步骤。

更好的方法是:

调试时使用编译器参数:

-DDEBUG=1 

不调试时使用编译器参数:

-DDEBUG=0 

然后,在代码中使用:

if( DEBUG )
{
     // your debugging printf()
    printf( ,,,, );
}

然后,当代码必须经过认证时,例如RTCA DO-178B,测试代码与发布代码之间的源代码没有区别。