|
1 <?php |
|
2 function add_file_to_list($n,$nn){ |
|
3 global $names,$nNames,$i; |
|
4 if(!in_array($n,$names)){ |
|
5 $names[$i]=$n; |
|
6 $nNames[$i]=$nn; |
|
7 $i++; |
|
8 } |
|
9 } |
|
10 function add_file_to_list2($n,$nn){ |
|
11 global $tnames,$tnNames,$u; |
|
12 if(!in_array($n,$tnames)){ |
|
13 $tnames[$u]=$n; |
|
14 $tnNames[$u]=$nn; |
|
15 $u++; |
|
16 } |
|
17 } |
|
18 function renameFiles($path,$prefix,array $ext){ |
|
19 $dr = opendir($path); |
|
20 $names = array(); |
|
21 $nNames = array(); |
|
22 while (($file = readdir($dr)) !== false) { |
|
23 if($file != "." && $file != "..") { |
|
24 if(is_dir($path.'/'.$file)){ |
|
25 renameFiles($path.'/'.$file,$prefix,$ext); |
|
26 }else{ |
|
27 if(in_array(strtolower(substr($file,strpos($file,'.')+1)),$ext)){ |
|
28 if(strpos($file,$prefix)===false){ |
|
29 rename($path.'/'.$file,$path.'/'.$prefix.$file); |
|
30 //print $file.'=>'.$prefix.$file.'<br />'; |
|
31 if(strtolower(substr($file,strpos($file,'.')+1))=='rsc'){ |
|
32 add_file_to_list2($file,$prefix.$file); |
|
33 } |
|
34 add_file_to_list($file,$prefix.$file); |
|
35 } |
|
36 } |
|
37 } |
|
38 } |
|
39 } |
|
40 closedir($dr); |
|
41 //print_r($names); |
|
42 //print_r($nNames); |
|
43 } |
|
44 |
|
45 function replaceFileNameIn($path,$name,$nName,$ext){ |
|
46 $dh = opendir($path); |
|
47 while (($file = readdir($dh)) !== false) { |
|
48 if($file != "." && $file != "..") { |
|
49 if(is_dir($path.'/'.$file)){ |
|
50 replaceFileNameIn($path.'/'.$file,$name,$nName,$ext); |
|
51 }else{ |
|
52 if(!in_array(strtolower(substr($file,strpos($file,'.')+1)),$ext)){ |
|
53 $c = file_get_contents($path.'/'.$file); |
|
54 foreach($name as $k=>$v){ |
|
55 $c = preg_replace('^("|<)('.$name[$k].')^','$1'.$nName[$k].'$3',$c);//("|>) |
|
56 } |
|
57 //print '<i>'.$file.'</i><br><pre>'.$c.'</pre>'; |
|
58 file_put_contents($path.'/'.$file,$c); |
|
59 } |
|
60 } |
|
61 } |
|
62 } |
|
63 closedir($dh); |
|
64 } |
|
65 |
|
66 function replaceFileNames2In($path,$name,$nName,$ext,$prefix){ |
|
67 Global $prefix; |
|
68 $dh = opendir($path); |
|
69 while (($file = readdir($dh)) !== false) { |
|
70 if($file != "." && $file != "..") { |
|
71 if(is_dir($path.'/'.$file)){ |
|
72 replaceFileNames2In($path.'/'.$file,$name,$nName,$ext,$prefix); |
|
73 }else{ |
|
74 if(in_array(strtolower(substr($file,strpos($file,'.')+1)),$ext)){ |
|
75 $c = file_get_contents($path.'/'.$file); |
|
76 |
|
77 $t = preg_match_all('^TARGET\x20+((.*))\x0A^',$c,$matches); |
|
78 //$c = preg_replace('^(TARGET\x20+)^ ','$1'.$prefix,$c);//add prefix to target name |
|
79 |
|
80 if($t>0){ |
|
81 //$matches[0]=null; |
|
82 foreach($matches[1] as $k=>$v){ |
|
83 if(preg_match('^'.$prefix.'^',$v)==0){ |
|
84 if(strtolower(substr($v,strpos($v,'.')+1))=='dll'){ |
|
85 $a = str_replace('.dll','.lib',$v); |
|
86 add_file_to_list2($a,$prefix.$a); |
|
87 } |
|
88 add_file_to_list2($v,$prefix.$v); |
|
89 } |
|
90 } |
|
91 } |
|
92 |
|
93 foreach($name as $k=>$v){ |
|
94 $c = preg_replace('^(\x2F|\x28|\x20|\x09)('.$name[$k].')^','$1'.$nName[$k].'$3',$c);//(\x20|\x29|\x0A|\x09) |
|
95 } |
|
96 //print '<i>'.$file.'</i><br><pre>'.$c.'</pre>'; |
|
97 file_put_contents($path.'/'.$file,$c); |
|
98 } |
|
99 } |
|
100 } |
|
101 } |
|
102 closedir($dh); |
|
103 } |
|
104 function replaceFileNames3In($path,$name,$nName,$ext){ |
|
105 $dh = opendir($path); |
|
106 while (($file = readdir($dh)) !== false) { |
|
107 if($file != "." && $file != "..") { |
|
108 if(is_dir($path.'/'.$file)){ |
|
109 replaceFileNames3In($path.'/'.$file,$name,$nName,$ext); |
|
110 }else{ |
|
111 //if(in_array(strtolower(substr($file,strpos($file,'.')+1)),$ext)){ |
|
112 $c = file_get_contents($path.'/'.$file); |
|
113 //foreach($name as $k=>$v){ |
|
114 //$c = preg_replace('^(\x2F|\x28|\x20|\x09)('.$name.')^','$1'.$nName,$c); |
|
115 $c = str_replace($name,$nName,$c); |
|
116 //} |
|
117 //print '<i>'.$file.'</i><br><pre>'.$c.'</pre>'; |
|
118 file_put_contents($path.'/'.$file,$c); |
|
119 //} |
|
120 } |
|
121 } |
|
122 } |
|
123 closedir($dh); |
|
124 } |
|
125 |
|
126 function display_files($path){ |
|
127 $dh = opendir($path); |
|
128 while (($file = readdir($dh)) !== false) { |
|
129 if($file != "." && $file != "..") { |
|
130 if(is_dir($path.'/'.$file)){ |
|
131 display_files($path.'/'.$file); |
|
132 }else{ |
|
133 $c = file_get_contents($path.'/'.$file); |
|
134 print '<i>'.$file.'</i><br><pre>'.$c.'</pre>'; |
|
135 } |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 //***** Used for storing edited files data **************************** |
|
141 $names= array(); //original names |
|
142 $nNames=array(); //edited names |
|
143 $i=0; //number of edited files |
|
144 |
|
145 $tnames= array(); //original target names |
|
146 $tnNames=array(); //edited target names |
|
147 $u=0; //number of edited files |
|
148 |
|
149 //********************************************************************* |
|
150 $root = 'sfr'; //source root |
|
151 chdir($root); //change current working directory |
|
152 //********************************************************************* |
|
153 $folder_to_check_for_renamed_files = 'homescreensrv/hs_app_idlefw'; |
|
154 $folder_to_rename = 'homescreensrv/hs_app_idlefw/'; |
|
155 $prefix = 'hs_app_'; |
|
156 $ext = array('h','cpp','inl','rss','loc','def','rh','hrh','lib','iby'); //extensions of the files that needs to be renamed. |
|
157 $ext2 = array('inf','mmp'); //extensions for which method 2 is used to search and replace filenames in certain file. |
|
158 |
|
159 |
|
160 /*************************************************************************************** |
|
161 Renames all the files in specified folder if the extension is found in array $ext. |
|
162 Edited files original names are stored in $names array, modified names in $nNames array. |
|
163 *******************************************/ |
|
164 renameFiles($folder_to_rename,$prefix,$ext); |
|
165 |
|
166 /*************************************************************************************************** |
|
167 This opens every file (if its extension is found in $ext array) located in specified folder and |
|
168 searches for each edited file's original name and replaces with edited vesrion. |
|
169 ***************************************************************************/ |
|
170 replaceFileNameIn($folder_to_check_for_renamed_files,$names,$nNames,$ext2); |
|
171 |
|
172 /*********************************************************************************************** |
|
173 This opens every file (if its extension is found in $ext2 array) located in specified folder and |
|
174 searches for each edited file's original name and replaces with edited vesrion. |
|
175 It alsoe adds prefix for 'TARGET' name |
|
176 ************************************************************************************/ |
|
177 replaceFileNames2In($folder_to_check_for_renamed_files,$names,$nNames,$ext2,$prefix); |
|
178 |
|
179 /******************************************************************************************************************************* |
|
180 This opens every file if its extension is found in $ext array. |
|
181 It attempts to replace all renamed TARGET files(e.g. homescreen.exe,aifw.dll) with names with prefix(e.g. hs_app_homescreen.exe); |
|
182 *************************************************************************************/ |
|
183 replaceFileNames3In($folder_to_check_for_renamed_files,$tnames,$tnNames,$ext); |
|
184 |
|
185 print '<b>************************************************************************************************************************</b><br/>'; |
|
186 foreach($names as $k=>$v){ |
|
187 print '<i>'.$v.'</i> <b>=></b> <i>'.$nNames[$k].'</i><br>'; |
|
188 } |
|
189 |
|
190 print '<b>************************************************************************************************************************</b><br/>'; |
|
191 foreach($tnames as $k=>$v){ |
|
192 print '<i>'.$v.'</i> <b>=></b> <i>'.$tnNames[$k].'</i><br>'; |
|
193 } |
|
194 display_files($folder_to_check_for_renamed_files); |
|
195 ?> |