+/** @file\r
+ \r
+ Copyright (c) 2006, Intel Corporation\r
+ All rights reserved. This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+ \r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+ \r
+ **/\r
package org.tianocore.migration;\r
\r
import java.io.*;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-public class SourceFileReplacer {\r
- SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {\r
- modulepath = path;\r
- mi = moduleinfo;\r
- db = database;\r
- ui = fp;\r
- }\r
- private String modulepath;\r
- private ModuleInfo mi;\r
- private Database db;\r
- private UI ui;\r
- private boolean showdetails = false;\r
+public final class SourceFileReplacer {\r
+ private static ModuleInfo mi;\r
+ private static boolean showdetails = false;\r
\r
- private class r8tor9 {\r
+ private static class r8tor9 {\r
r8tor9(String r8, String r9) {\r
r8thing = r8;\r
r9thing = r9;\r
}\r
\r
// these sets are used only for printing log of the changes in current file\r
- private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
- private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
- private Set<String> filer8only = new HashSet<String>();\r
- \r
- private String r8only = "EfiLibInstallDriverBinding " +\r
- "EfiLibInstallAllDriverProtocols " +\r
- "EfiLibCompareLanguage " +\r
- "BufToHexString " +\r
- "EfiStrTrim " +\r
- "EfiValueToHexStr " +\r
- "HexStringToBuf " +\r
- "IsHexDigit " +\r
- "NibbleToHexChar " +\r
- "GetHob " +\r
- "GetHobListSize " +\r
- "GetHobVersion " +\r
- "GetHobBootMode " +\r
- "GetCpuHobInfo " +\r
- "GetDxeCoreHobInfo " +\r
- "GetNextFirmwareVolumeHob " +\r
- "GetNextGuidHob " +\r
- "GetPalEntryHobInfo " +\r
- "GetIoPortSpaceAddressHobInfo ";\r
+ private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
+ private static final Set<String> filer8only = new HashSet<String>();\r
\r
- public void flush() throws Exception {\r
- PrintWriter outfile;\r
- String temp = null;\r
- if (ui.yesOrNo("Change Source Code is to be doing . See details ?")) {\r
+ public static final void flush(ModuleInfo moduleinfo) throws Exception {\r
+ \r
+ mi = moduleinfo;\r
+ \r
+ String outname = null;\r
+ String inname = null;\r
+ if (MigrationTool.ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
showdetails = true;\r
}\r
- File tempdir = new File(modulepath + File.separator + "result" + File.separator);\r
- if (!tempdir.exists()) tempdir.mkdir();\r
- String[] list = new File(modulepath + File.separator + "temp").list(); //what I change is the non-local .h commented-out files\r
- for (int i = 0 ; i < list.length ; i++) {\r
- if (list[i].contains(".c")) {\r
- ui.println("\nModifying file : " + list[i]);\r
- outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + list[i])));\r
- outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + list[i]));\r
- outfile.flush();\r
- outfile.close();\r
- } else {\r
- if (list[i].contains(".h")) {\r
- temp = list[i];\r
- } else if (list[i].contains(".C")) {\r
- temp = list[i].replaceFirst(".C", ".c");\r
- } else if (list[i].contains(".H")) {\r
- temp = list[i].replaceFirst(".H", ".h");\r
+ \r
+ Iterator<String> di = mi.localmodulesources.iterator();\r
+ while (di.hasNext()) {\r
+ inname = di.next();\r
+ if (inname.contains(".c") || inname.contains(".C")) {\r
+ if (inname.contains(".C")) {\r
+ outname = inname.replaceFirst(".C", ".c");\r
} else {\r
- continue;\r
+ outname = inname;\r
}\r
- ui.println("\nCopying file : " + temp);\r
- outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + temp)));\r
- outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + list[i]));\r
- outfile.flush();\r
- outfile.close();\r
+ MigrationTool.ui.println("\nModifying file: " + inname);\r
+ Common.string2file(sourcefilereplace(mi.modulepath + File.separator + "temp" + File.separator + inname), mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
+ } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
+ if (inname.contains(".H")) {\r
+ outname = inname.replaceFirst(".H", ".h");\r
+ } else {\r
+ outname = inname;\r
+ }\r
+ MigrationTool.ui.println("\nCopying file: " + inname);\r
+ Common.string2file(Common.file2string(mi.modulepath + File.separator + "temp" + File.separator + inname), mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
}\r
}\r
\r
}\r
}\r
\r
- private void addr8only() throws Exception {\r
+ private static final void addr8only() throws Exception {\r
String paragraph = null;\r
- String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
- PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
- PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
- //outfile1.append("#include \"R8Lib.h\"\n\n");\r
- //outfile2.append("#include \"R8Lib.h\"\n\n");\r
+ String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
+ Common.ensureDir(mi.modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
+ PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
+ PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
Matcher mtrr8only = ptnr8only.matcher(line);\r
Matcher mtrr8onlyhead;\r
outfile1.close();\r
outfile2.flush();\r
outfile2.close();\r
- }\r
- \r
- private String sourcefiletostring(String filename) throws Exception {\r
- BufferedReader rd = new BufferedReader(new FileReader(filename));\r
- StringBuffer wholefile = new StringBuffer();\r
- String line;\r
- while ((line = rd.readLine()) != null) {\r
- wholefile.append(line + "\n");\r
- }\r
- return wholefile.toString();\r
+ \r
+ mi.localmodulesources.add("R8Lib.h");\r
+ mi.localmodulesources.add("R8Lib.c");\r
}\r
\r
// Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
- private String sourcefilereplace(String filename) throws Exception {\r
+ private static final String sourcefilereplace(String filename) throws Exception {\r
BufferedReader rd = new BufferedReader(new FileReader(filename));\r
StringBuffer wholefile = new StringBuffer();\r
String line;\r
// replace BS -> gBS , RT -> gRT\r
Matcher mat = pat.matcher(line);\r
if (mat.find()) { // add a library here\r
- ui.println("Converting all BS->gBS,RT->gRT");\r
+ MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");\r
line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
}\r
mat.reset();\r
Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
Matcher matentrypoint = patentrypoint.matcher(line);\r
if (matentrypoint.find()) {\r
- ui.println("Deleting Entry_Point");\r
+ MigrationTool.ui.println("Deleting Entry_Point");\r
line = matentrypoint.replaceAll("");\r
}\r
*/\r
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
} else { //\r
- mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
+ mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
}\r
\r
- if ((r9thing = db.getR9Func(r8thing)) != null) {\r
+ if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
if (!r8thing.equals(r9thing)) {\r
if (line.contains(r8thing)) {\r
line = line.replaceAll(r8thing, r9thing);\r
Iterator<r8tor9> rt = filefunc.iterator();\r
while (rt.hasNext()) {\r
temp = rt.next();\r
- if (r8only.contains(temp.r8thing)) {\r
- mi.localmodulesources.add("R8Lib.h");\r
- mi.localmodulesources.add("R8Lib.c");\r
- mi.localmoduleheaders.add("R8Lib.h");\r
+ if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
filer8only.add(r8thing);\r
mi.hashr8only.add(r8thing);\r
addr8 = true;\r
it = mi.hashnonlocalmacro.iterator();\r
while (it.hasNext()) { //macros are all assumed MdePkg currently\r
r8thing = it.next();\r
- //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
- if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
+ //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
+ if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
if (line.contains(r8thing)) {\r
line = line.replaceAll(r8thing, r9thing);\r
filemacro.add(new r8tor9(r8thing, r9thing));\r
show(fileppi, "ppi");\r
show(fileprotocol, "protocol");\r
if (!filer8only.isEmpty()) {\r
- ui.println("Converting r8only : " + filer8only);\r
+ MigrationTool.ui.println("Converting r8only : " + filer8only);\r
}\r
\r
filefunc.clear();\r
fileppi.clear();\r
fileprotocol.clear();\r
filer8only.clear();\r
- \r
+\r
return line;\r
}\r
\r
- private void show(Set<r8tor9> hash, String sh) {\r
+ private static final void show(Set<r8tor9> hash, String sh) {\r
Iterator<r8tor9> it = hash.iterator();\r
r8tor9 temp;\r
if (showdetails) {\r
if (!hash.isEmpty()) {\r
- ui.print("Converting " + sh + " : ");\r
+ MigrationTool.ui.print("Converting " + sh + " : ");\r
while (it.hasNext()) {\r
temp = it.next();\r
- ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
+ MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
}\r
- ui.println("");\r
+ MigrationTool.ui.println("");\r
}\r
}\r
}\r
\r
- private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
+ private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
Iterator<String> it;\r
String r8thing;\r
String r9thing;\r
it = hash.iterator();\r
while (it.hasNext()) {\r
r8thing = it.next();\r
- if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
+ if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
if (!r8thing.equals(r9thing)) {\r
if (line.contains(r8thing)) {\r
line = line.replaceAll(r8thing, r9thing);\r