File indexing completed on 2024-04-14 05:35:09

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 sub diffFile
0007 {
0008     if (-d ".svn") {
0009         system(qw(svn diff), @_);
0010     } elsif (-d "CVS") {
0011         system(qw(cvs diff -up), @_);
0012     } elsif (-d ".git") {
0013                 system(qw(git diff), @_);
0014     }
0015     warn "files to commit: @_\n";
0016 }
0017 
0018 sub selectHeaderFile
0019 {
0020     my ($newFile) = @_;
0021     return 1 if -d $newFile;
0022     if ( $newFile =~ /(\.h$)/ ) {
0023        return 0;
0024     }
0025     return 1;
0026 }
0027 
0028 sub excludeFile
0029 {
0030     my ($newFile) = @_;
0031     return 1 if -d $newFile;
0032     if ( $newFile =~ /(\.cpp$|\.cc$|\.h$|\.ui$|\.CC$|\.c\+\+$)/ ) {
0033        return 0; 
0034     }
0035     return 1;
0036     #return 1 if $newFile =~ /\/\.svn\//;
0037     #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])?$/;
0038 }
0039 
0040 sub removeObjectNameTwoArgument
0041 {
0042     my ($newLine, $className) = @_;
0043     my $result;
0044     if ( $newLine =~ /$className\s*\(/ ) {
0045     if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
0046         if ( my ($firstelement, $secondelement) = m!.*?\(\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
0047                 my $split = $before;
0048                 $split =~ s!$className!!;
0049                 $split =~ s!=!!;
0050                 $split =~ s! !!g;
0051                 $split =~ s!\*!!;
0052         
0053         # normalize white space around the arguments in caller:
0054         $secondelement =~ s/\s*$//;
0055         $secondelement =~ s/^\s*//;
0056         
0057         # do the actual conversion:
0058                 $result = $blank . $before . "$prefix\( $firstelement \);\n" . $blank . $split . "->setObjectName\( $secondelement \);\n";
0059             }
0060         }
0061     }
0062     return $result;
0063 }
0064 
0065 sub removeObjectNameThreeArgument
0066 {
0067     my ($newLine, $className) = @_;
0068     my $result;
0069     if ( $newLine =~ /$className\s*\(/ ) {
0070     if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
0071         if ( my ($firstelement, $secondelement, $thirdelement) = m!.*?\(\s*([^,]*),\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
0072                 my $split = $before;
0073                 $split =~ s!$className!!;
0074                 $split =~ s!=!!;
0075                 $split =~ s! !!g;
0076                 $split =~ s!\*!!;
0077         
0078         # normalize white space around the arguments in caller:
0079         $thirdelement =~ s/\s*$//;
0080         $thirdelement =~ s/^\s*//;
0081         # do the actual conversion:
0082                 $result = $blank . $before . "$prefix\( $firstelement, $secondelement \);\n" . $blank . $split . "->setObjectName\( $thirdelement \);\n";
0083             }
0084         }
0085     }
0086     return $result;
0087 }
0088 
0089 sub addAfterAllIncludes($$)
0090 {
0091     local *F;
0092     my ( $file, $theline ) = @_;
0093     open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
0094     my $str = join '', <F>;
0095     if ( $str !~ /$theline/ ) {
0096 
0097         # Add the include after all others
0098         if ( !( $str =~ s!(#include <.*#include <[^\r\n]*)!$1\n$theline!smig ) ) {
0099 
0100             # This failed, maybe there is only one include
0101             if ( ! ($str =~ s!(#include <[^\r\n]*)!$1\n$theline!smig ) ) {
0102                 warn "Couldn't add $theline\n in $file\n";
0103             }
0104         }
0105         seek F, 0, 0;
0106         print F $str;
0107         truncate F, tell(F);
0108     }
0109     close F;
0110 }
0111 
0112 sub addIncludeInFile($$)
0113 {
0114     my ( $file, $includefile ) = @_;
0115     addAfterAllIncludes($file, "#include <$includefile>");
0116 }
0117 
0118 sub removeIncludeInFile
0119 {
0120    local *F;
0121    my ($file, $includefile) = @_;
0122    open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
0123    my $str = join '', <F>;
0124    if( $str =~ m/#include <$includefile>/ ) {
0125      $str =~ s!#include <$includefile>!!smig;
0126      seek F, 0, 0;
0127      print F $str;
0128      truncate F, tell(F);
0129    }
0130    close F;
0131 }
0132 
0133 
0134 # code from MDK::common package
0135 # Code from Drakx Mandriva code (GPL code)
0136 sub substInFile(&@) {
0137     my ($f, $file) = @_;
0138     my $linkdest;
0139     #- try hard to keep symlinks as they were set
0140     if (-l $file) {
0141         my $targetfile = readlink $file;
0142         unlink $file;
0143         $linkdest = $file;
0144         $file = $targetfile;
0145     }
0146     my $modified = 0;
0147     if (-s $file) {
0148     local @ARGV = $file;
0149     local $^I = '';
0150     local $_;
0151     while (<>) {
0152         $_ .= "\n" if eof && !/\n/; # always terminate with a newline
0153         my $orig = $_;
0154         &$f($_);
0155         $modified ||= $orig ne $_;
0156         print;
0157     }
0158     } else {
0159     local *F; my $old = select F; # that way eof return true
0160     local $_ = '';
0161     &$f($_);
0162     select $old;
0163     eval { output($file, $_) };
0164     }
0165     $linkdest and symlink $file, $linkdest;
0166     return $modified;
0167 }
0168 1;