File indexing completed on 2024-02-18 05:41:44

0001 #!/usr/bin/perl -w
0002 
0003 # Laurent Montel <montel@kde.org> (2014)
0004 # KTempDir->QTemporaryDir
0005 # find -iname "*.cpp" -o -iname "*.h" |xargs kde-dev-scripts/kf5/convert-ktempdir.pl
0006 # TODO need to improve it.
0007 use strict;
0008 use File::Basename;
0009 use lib dirname($0);
0010 use functionUtilkde;
0011 
0012 
0013 foreach my $file (@ARGV) {
0014 
0015     my $modified;
0016     open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
0017     my %varname = ();
0018     my %finalized_called = ();
0019     my @l = map {
0020         my $orig = $_;
0021 
0022         my $regexpKTempDirLocal = qr/
0023           ^(\s*)           # (1) Indentation
0024           KTempDir\s+
0025           (\w+)            # (2) variable name
0026           /x; # /x Enables extended whitespace mode
0027         if (my ($left, $var) = $_ =~ $regexpKTempDirLocal) {
0028            $varname{$var} = 1;
0029            s/\bKTempDir\b/QTemporaryDir/;
0030            warn "Be sure that you create temporary dir. if you don't use default constructor\n";
0031         }
0032 
0033         my $regexp = qr/
0034            ^(\s*)                        # (1) Indentation, possibly "Classname *" (the ? means non-greedy)
0035            (.*?)                         # (2) Possibly "Classname *" (the ? means non-greedy)
0036            (\w+)                         # (3) variable name
0037            \s*=\s*                       #   assignment
0038            new\s+KTempDir\s*\((.*)\)        # (4)  new KTempDir(...,...,...,...);
0039            (.*)$                         # (5) afterreg
0040            /x; # /x Enables extended whitespace mode
0041         if (my ($indent, $left, $var, $argument, $afterreg) = $_ =~ $regexp) {
0042           warn "KTempDir found \'$var\' \'$argument\'\n";
0043            $varname{$var} = 1;
0044            s/\bKTempDir\b/QTemporaryDir/;
0045            warn "Be sure that you create temporary dir. if you don't use default constructor\n";
0046         }
0047 
0048         my $regexpKTempDirFunction = qr/
0049           (.*?)
0050           KTempDir\s*\*
0051           (\w+)            # (2) variable name
0052           /x; # /x Enables extended whitespace mode
0053         if (my ($left, $var) = $_ =~ $regexpKTempDirFunction) {
0054            warn "Found KTempDir in function!!!! $var\n";
0055            $varname{$var} = 1;
0056            s/\bKTempDir\b/QTemporaryDir/;
0057            warn "Be sure that you create temporary dir. if you don't use default constructor\n";
0058         }
0059 
0060         if (/(\w+)\.name\(/) {
0061            my $var = $1;
0062            if ( defined $varname{$var} ) {
0063               s/$var\.name/$var\.path/;
0064            }
0065         }
0066 
0067         if (/(\w+)\->name\(/) {
0068            my $var = $1;
0069            if ( defined $varname{$var} ) {
0070               s/$var\->name/$var\->path/;
0071            }
0072         }
0073 
0074         if (/(\w+)\.unlink\(/) {
0075            my $var = $1;
0076            if ( defined $varname{$var} ) {
0077               s/$var\.unlink/$var\.remove/;
0078            }
0079         }
0080 
0081         if (/(\w+)\.exists\(/) {
0082            my $var = $1;
0083            if ( defined $varname{$var} ) {
0084               warn "$file: replace exists by QTemporaryDir::isValid() + QDir::exists(path) if necessary\n";
0085               s/$var\.exists/$var\.isValid/;
0086            }
0087         }
0088         if (/(\w+)\->exists\(/) {
0089            my $var = $1;
0090            if ( defined $varname{$var} ) {
0091               warn "$file: replace exists by QTemporaryDir::isValid() + QDir::exists(path) if necessary\n";
0092               s/$var\->exists/$var\->isValid/;
0093            }
0094         }
0095 
0096 
0097         if (/(\w+)\->unlink\(/) {
0098            my $var = $1;
0099            if ( defined $varname{$var} ) {
0100               s/$var\->unlink/$var\->remove/;
0101            }
0102         }
0103         my $regexpRemoveDir = qr/
0104            ^(\s*)                         # (1) Indentation, possibly "Classname *" (the ? means non-greedy)
0105            (.*?)                          # (2) before KTempDir::removeDir
0106            KTempDir::removeDir
0107            ${functionUtilkde::paren_begin}3${functionUtilkde::paren_end}    # (3) (args)
0108            (.*)$                         # (4) afterreg
0109            /x; # /x Enables extended whitespace mode
0110         if ( my ($indent, $before, $argument, $after) = $_ =~ $regexpRemoveDir) {
0111            $_ = $indent . $before . "QDir($argument).removeRecursively()" . $after . "\n";
0112         } 
0113 
0114         s/\bKTempDir\b/QTemporaryDir/g;
0115         s/\<KTempDir\b\>/\<QTemporaryDir>/ if (/#include/);
0116         s/\<ktempdir.h\>/\<QTemporaryDir>/ if (/#include/);
0117         $modified ||= $orig ne $_;
0118         $_;
0119     } <$FILE>;
0120 
0121     if ($modified) {
0122         open (my $OUT, ">", $file);
0123         print $OUT @l;
0124         close ($OUT);
0125     }
0126 }
0127 
0128 functionUtilkde::diffFile( "@ARGV" );