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