On Sat, Mar 15, 2008 at 4:50 AM, Gowri Chandra Sekhar Barla, TLS,
Chennai <[EMAIL PROTECTED]> wrote:
>
>
>  Hi
>
>  Please help me in writing the script to check that all switch statements
>  have the "default" case in a file
>
>  Thanks and regards
>  Chandu
snip

In which language?  Regardless of the language, you can probably only
get a %80 effective solution without massive amounts of work.  You can
identify the majority of the switch statements, but to be certain you
get all of them (and don't match ones inside comments, strings, etc)
you will need a parser for the language you are dealing with.  Than
can be quite a lot of work.  For instance, here is a simple version
(note, it does not handle nested case statements, bracing styles other
than OTB, and many other things correctly):

#!/usr/bin/perl

use strict;
use warnings;


while (<DATA>) {
        next unless /switch/;
        #number of open braces - number of close braces
        my $braces   = tr/{// - tr/}//;
        my $fragment = $_;
        if ($braces) {
                while (<DATA>) {
                        $braces   += tr/{// - tr/}//;
                        $fragment .= $_;
                        last unless $braces;
                }
        }
        unless ($fragment =~ /default:/) {
                print "around line $. there is a default missing:\n",
                        map "\t$_\n", split "\n", $fragment;
        }
}

__DATA__
int main (int argc, char** argv) {
        int c = atoi(argv[0]);

        switch (c) {
                case 1:
                        printf("one\n");
                        break;
                case 2:
                        printf("two\n");
                        break;
                default:
                        printf("not one or two\n");
                        break;
        }

        switch (c) {
                case 1:
                        printf("one\n");
                        break;
                case 2:
                        printf("two\n");
                        break;
        }

        return 0;
}



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to