File indexing completed on 2024-06-09 09:25:56

0001 # Util function for script to convert kde3 to kde4
0002 # laurent Montel <montel@kde.org> 2005-2006-2007-2008-2009 GPL
0003 
0004 package functionUtilkde;
0005 use strict;
0006 
0007 our $paren_begin = '(\((?:(?>[^()]+)|(?';
0008 our $paren_end = '))*\))';
0009 
0010 sub extraVariableFromUiFile($$)
0011 {
0012     my ($ref_localvarname, $ref_uiclassname) = @_;
0013     open(my $ALLFILE, "-|", qw(find . -type f));
0014     my $uifile;
0015     while ($uifile = <$ALLFILE>) {
0016       next if not $uifile =~ /\.ui/;
0017       chomp $uifile;
0018       open(my $FILE, "<", $uifile) or warn "We can't open file $uifile:$!\n";
0019       warn "Open file $uifile\n";
0020       my $mainClassFound;
0021       my @lui = map {
0022         #<widget class="QProgressBar" name="progressBar" >
0023         if (/\<widget class=\"(.*)\" name=\"(\w+)\"/) {
0024            my $className = $1;
0025            my $variableName = $2;
0026            warn "Found class in ui file \'$uifile\', className: \'$className\', variable: \'$variableName\'\n";
0027            ${$ref_localvarname}{$variableName} = ${className};
0028         }
0029         if (/\<class\>(.*)\<\/class\>/) {
0030            if (not defined $mainClassFound) {
0031              my $name = $1;
0032              ${$ref_uiclassname}{$name} = 1;
0033              warn "Found Class Name in file \'$uifile\': name \'$name\'\n";
0034              $mainClassFound = 1;
0035            }
0036         }
0037         # Special case buttongroup
0038         if ( /\<buttongroup name=\"(.*)\"\/\>/ ) {
0039            my $className = "QButtonGroup";
0040            my $variableName = $1;
0041            ${$ref_localvarname}{$variableName} = ${className};
0042            warn "Found QButtonGroup in file \'$uifile\': name \'$variableName\'\n";
0043         }
0044 
0045         $_;
0046       } <$FILE>
0047     }
0048 }
0049 
0050 sub diffFile
0051 {
0052     if ( system("git rev-parse --is-inside-work-tree 2>/dev/null >/dev/null") == 0 ) {
0053         system( qw(git diff --), @_ );
0054     } elsif ( system("svn info 2>/dev/null >/dev/null") == 0 ) {
0055         system( qw(svn diff), @_ );
0056     } elsif ( system("hg identify 2>/dev/null >/dev/null") == 0 ) {
0057         system( qw(hg diff), @_ );
0058     } elsif ( -d "CVS" ) {
0059         system( qw(cvs diff -up), @_ );
0060     }
0061 }
0062 
0063 sub headerName
0064 {
0065     my ($cppfile) = @_;
0066     my $headerfile;
0067     if ( $cppfile =~ /.cpp$/ ) {
0068        $cppfile =~ s/.cpp$//;
0069        $headerfile = $cppfile . ".h";
0070        unless ( -e $headerfile ) {
0071           $headerfile = $cppfile . "_p.h";
0072        }
0073     } elsif ( $cppfile =~ /.cc$/ ) {
0074        $cppfile =~ s/.cc$//;
0075        $headerfile = $cppfile . ".h";
0076        unless ( -e $headerfile ) {
0077           $headerfile = $cppfile . "_p.h";
0078        }
0079     }
0080     return $headerfile;
0081 }
0082 
0083 sub excludeFile
0084 {
0085     my ($newFile) = @_;
0086     return 1 if -d $newFile;
0087     if ( $newFile =~ /(\.cpp$|\.cc$|\.h$|\.ui$|\.CC$|\.c\+\+$)/ ) {
0088        return 0; 
0089     }
0090     return 1;
0091     #return 1 if $newFile =~ /\/\.svn\//;
0092     #return $newFile =~ /TODO|Changelog|ChangeLog|README|Readme|portinglog.txt|Makefile(|\.(in|am))|\.(jpg|png|svgz|svg|html|HOWTO|README|svn|ui|kidl|desktop|pl|libs|o|moc|docbook|gz|ogg|sh|wav|cmake|dat|au|dox|l[ao])?$/;
0093 }
0094 
0095 sub removeObjectNameTwoArgument
0096 {
0097     my ($newLine, $className) = @_;
0098     my $result;
0099     if ( $newLine =~ /$className\s*\(/ ) {
0100     if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
0101         if ( my ($firstelement, $secondelement) = m!.*?\(\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
0102                 my $split = $before;
0103                 $split =~ s!$className!!;
0104                 $split =~ s!=!!;
0105                 $split =~ s! !!g;
0106                 $split =~ s!\*!!;
0107         
0108         # normalize white space around the arguments in caller:
0109         $secondelement =~ s/\s*$//;
0110         $secondelement =~ s/^\s*//;
0111         
0112         # do the actual conversion:
0113                 $result = $blank . $before . "$prefix\( $firstelement \);\n" . $blank . $split . "->setObjectName\( $secondelement \);\n";
0114             }
0115         }
0116     }
0117     return $result;
0118 }
0119 
0120 sub removeObjectNameThreeArgument
0121 {
0122     my ($newLine, $className) = @_;
0123     my $result;
0124     if ( $newLine =~ /$className\s*\(/ ) {
0125     if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
0126         if ( my ($firstelement, $secondelement, $thirdelement) = m!.*?\(\s*([^,]*),\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
0127                 my $split = $before;
0128                 $split =~ s!$className!!;
0129                 $split =~ s!=!!;
0130                 $split =~ s! !!g;
0131                 $split =~ s!\*!!;
0132         
0133         # normalize white space around the arguments in caller:
0134         $thirdelement =~ s/\s*$//;
0135         $thirdelement =~ s/^\s*//;
0136         # do the actual conversion:
0137                 $result = $blank . $before . "$prefix\( $firstelement, $secondelement \);\n" . $blank . $split . "->setObjectName\( $thirdelement \);\n";
0138             }
0139         }
0140     }
0141     return $result;
0142 }
0143 
0144 sub addIncludeInFile
0145 {
0146    local *F;
0147    my ($file, $includefile) = @_;
0148    open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
0149    my $str = join '', <F>;
0150    if( $str !~ /#include <$includefile>/ ) {
0151     # Add the include after all others
0152     if (!($str =~ s!(#include <.*#include <[^\n]*)!$1\n#include <$includefile>!smig)) {
0153        # This failed, maybe there is only one include
0154        $str =~ s!(#include <[^\n]*)!$1\n#include <$includefile>!smig;
0155     }
0156     seek F, 0, 0;
0157     print F $str;
0158     truncate F, tell(F);
0159    }
0160    close F;
0161 }
0162 
0163 sub removeIncludeInFile
0164 {
0165    local *F;
0166    my ($file, $includefile) = @_;
0167    open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
0168    my $str = join '', <F>;
0169    if( $str =~ m/#include <$includefile>/ ) {
0170      $str =~ s!#include <$includefile>!!smig;
0171      seek F, 0, 0;
0172      print F $str;
0173      truncate F, tell(F);
0174    }
0175    close F;
0176 }
0177 
0178 
0179 sub cleanSpace
0180 {
0181   my ($var) = @_;
0182   $var =~ s/ //g;
0183   return $var;
0184 }
0185 
0186 # code from MDK::common package
0187 # Code from Drakx Mandriva code (GPL code)
0188 sub substInFile(&@) {
0189     my ($f, $file) = @_;
0190     my $linkdest;
0191     #- try hard to keep symlinks as they were set
0192     if (-l $file) {
0193         my $targetfile = readlink $file;
0194         unlink $file;
0195         $linkdest = $file;
0196         $file = $targetfile;
0197     }
0198     my $modified = 0;
0199     if (-s $file) {
0200     local @ARGV = $file;
0201     local $^I = '';
0202     local $_;
0203     while (<>) {
0204         $_ .= "\n" if eof && !/\n/; # always terminate with a newline
0205         my $orig = $_;
0206         &$f($_);
0207         $modified ||= $orig ne $_;
0208         print;
0209     }
0210     } else {
0211     local *F; my $old = select F; # that way eof return true
0212     local $_ = '';
0213     &$f($_);
0214     select $old;
0215     eval { output($file, $_) };
0216     }
0217     $linkdest and symlink $file, $linkdest;
0218     return $modified;
0219 }
0220 1;