tests: ok(0,"foo") prints a '\n'!

Francois Gouget fgouget at free.fr
Tue Dec 31 15:25:34 CST 2002


On Mon, 30 Dec 2002, Glen wrote:

> Francois Gouget wrote:
>
> >So if noone objects I'll send a patch that stops 'ok' from appending
> >this '\n', and adds a '\n' to all calls to ok. Actually, I would be
> >pretty happy if someone could come up with a perl script doing the
> >latter...
> >
> >
> I came up with a perl script that does what you want.  It may not be
> perfect though so use it with caution.

Thanks for the script. It has some problems though:
 * it modifies all files. This makes later cvs diff veeeery slow as it
has to check each and every file for differences
 * it did not seem to match the 'ok' calls I have here

So I modified it and came up with the following which seems to be
working ok.

#!/usr/bin/perl
sub fix_oks
{
    my $c_file = $_[0];

    open(FILE_HANDLE, "<$c_file") or die("Error opening file $c_file\n");
    my @contents = <FILE_HANDLE>;
    close(FILE_HANDLE);

    #add a new line to the end of any ok calls we find
    my $modified;
    my $line2;
    foreach (@contents) {
        if (/\bok\s*\((.*)\);\s*$/) {
            my @components=split /,/,$1;
            foreach my $item (@components) {
                if ($item !~ /^\s*\".*\\n\"\s*$/) {
                    $item=~s/^(\s*\".*)(\"\s*)$/$1\\n$2/;
                }
            }
            my $args=join(",", at components);
            my $line=$_;
            $line=~s/(\bok\s*\()(.*)(\);\s*)$/$1$args$3/;
            if ($line ne $_) {
                $_=$line;
                $modified=1;
            }
        } elsif (/\bok\s*\(.*,\s*$/) {
            $line2=1;
        } else {
            if ($line2) {
                if (!/\".*\\n\"/) {
                    my $line=$_;
                    s/(\"([^\"]|\\\")*)(\")/$1\\n$3/;
                    $modified=1 if ($line ne $_);
                }
            }
            $line2=undef;
        }
    }

    if ($modified) {
        open(FILE_HANDLE, ">$c_file");
        print(FILE_HANDLE @contents);
        close(FILE_HANDLE);
    }
}

sub do_current_dir
{
    my $i = 0;

    #get the current directory's listing
    opendir(DIR_HANDLE, ".") or die("Error opening $directory\n");
    my @dir_listing = grep { !/^\.\.?/ } readdir(DIR_HANDLE);
    closedir(DIR_HANDLE);

    #for each file in the directory
    while($dir_listing[$i])
    {
        #if a directory is found then do that one too
        if(-d $dir_listing[$i])
        {
            chdir $dir_listing[$i];
            do_current_dir();
            chdir "..";
        }

        #if it's a c file then fix the calls to ok
        if ($dir_listing[$i] =~ /\.c$/)
        {
            fix_oks($dir_listing[$i]);
        }

        $i++;
    }
}

do_current_dir();



-- 
Francois Gouget         fgouget at free.fr        http://fgouget.free.fr/
     Linux, WinNT, MS-DOS - also known as the Good, the Bad and the Ugly.




More information about the wine-devel mailing list