1# Util function for script to convert kde3 to kde4
2# laurent Montel <montel@kde.org> 2005-2006-2007-2008-2009 GPL
3
4package functionUtilkde;
5use strict;
6sub diffFile
7{
8	if (-d ".svn") {
9		system(qw(svn diff), $@);
10	} elsif (-d "CVS") {
11		system(qw(cvs diff -up), $@);
12	} elsif (-d ".git") {
13                system(qw(git diff), $@);
14	}
15	warn "files to commit: $@\n";
16}
17
18sub selectHeaderFile
19{
20    my ($newFile) = @_;
21    return 1 if -d $newFile;
22    if ( $newFile =~ /(\.h$)/ ) {
23       return 0;
24    }
25    return 1;
26}
27
28sub excludeFile
29{
30    my ($newFile) = @_;
31    return 1 if -d $newFile;
32    if ( $newFile =~ /(\.cpp$|\.cc$|\.h$|\.ui$|\.CC$|\.c\+\+$)/ ) {
33       return 0;
34    }
35    return 1;
36    #return 1 if $newFile =~ /\/\.svn\//;
37    #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])?$/;
38}
39
40sub removeObjectNameTwoArgument
41{
42    my ($newLine, $className) = @_;
43    my $result;
44    if ( $newLine =~ /$className\s*\(/ ) {
45	if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
46	    if ( my ($firstelement, $secondelement) = m!.*?\(\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
47                my $split = $before;
48                $split =~ s!$className!!;
49                $split =~ s!=!!;
50                $split =~ s! !!g;
51				$split =~ s!\*!!;
52
53		# normalize white space around the arguments in caller:
54		$secondelement =~ s/\s*$//;
55		$secondelement =~ s/^\s*//;
56
57		# do the actual conversion:
58                $result = $blank . $before . "$prefix\( $firstelement \);\n" . $blank . $split . "->setObjectName\( $secondelement \);\n";
59            }
60        }
61    }
62    return $result;
63}
64
65sub removeObjectNameThreeArgument
66{
67    my ($newLine, $className) = @_;
68    my $result;
69    if ( $newLine =~ /$className\s*\(/ ) {
70	if (my ($blank, $before, $prefix, $contenu) = $newLine =~ m!^(\s*)(\s*.*)(new $className.*?)\(([^()]*)\s*\);$!) {
71	    if ( my ($firstelement, $secondelement, $thirdelement) = m!.*?\(\s*([^,]*),\s*([^,]*),\s*(\"[^\"]*\")\s*\);\s*$!) {
72				my $split = $before;
73                $split =~ s!$className!!;
74                $split =~ s!=!!;
75                $split =~ s! !!g;
76				$split =~ s!\*!!;
77
78		# normalize white space around the arguments in caller:
79		$thirdelement =~ s/\s*$//;
80		$thirdelement =~ s/^\s*//;
81		# do the actual conversion:
82                $result = $blank . $before . "$prefix\( $firstelement, $secondelement \);\n" . $blank . $split . "->setObjectName\( $thirdelement \);\n";
83            }
84        }
85    }
86    return $result;
87}
88
89sub addAfterAllIncludes($$)
90{
91    local *F;
92    my ( $file, $theline ) = @_;
93    open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
94    my $str = join '', <F>;
95    if ( $str !~ /$theline/ ) {
96
97        # Add the include after all others
98        if ( !( $str =~ s!(#include <.*#include <[^\r\n]*)!$1\n$theline!smig ) ) {
99
100            # This failed, maybe there is only one include
101            if ( ! ($str =~ s!(#include <[^\r\n]*)!$1\n$theline!smig ) ) {
102                warn "Couldn't add $theline\n in $file\n";
103            }
104        }
105        seek F, 0, 0;
106        print F $str;
107        truncate F, tell(F);
108    }
109    close F;
110}
111
112sub addIncludeInFile($$)
113{
114    my ( $file, $includefile ) = @_;
115    addAfterAllIncludes($file, "#include <$includefile>");
116}
117
118sub removeIncludeInFile
119{
120   local *F;
121   my ($file, $includefile) = @_;
122   open F, "+<", $file or do { print STDOUT "open($file) failed : \"$!\"\n"; next };
123   my $str = join '', <F>;
124   if( $str =~ m/#include <$includefile>/ ) {
125     $str =~ s!#include <$includefile>!!smig;
126     seek F, 0, 0;
127     print F $str;
128     truncate F, tell(F);
129   }
130   close F;
131}
132
133
134# code from MDK::common package
135# Code from Drakx Mandriva code (GPL code)
136sub substInFile(&@) {
137    my ($f, $file) = @_;
138    my $linkdest;
139    #- try hard to keep symlinks as they were set
140    if (-l $file) {
141        my $targetfile = readlink $file;
142        unlink $file;
143        $linkdest = $file;
144        $file = $targetfile;
145    }
146    my $modified = 0;
147    if (-s $file) {
148	local @ARGV = $file;
149	local $^I = '';
150	local $_;
151	while (<>) {
152	    $_ .= "\n" if eof && !/\n/; # always terminate with a newline
153	    my $orig = $_;
154	    &$f($_);
155	    $modified ||= $orig ne $_;
156	    print;
157	}
158    } else {
159	local *F; my $old = select F; # that way eof return true
160	local $_ = '';
161	&$f($_);
162	select $old;
163	eval { output($file, $_) };
164    }
165    $linkdest and symlink $file, $linkdest;
166    return $modified;
167}
1681;
169