public static List <LogInfo> TXTDelLineOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_TXTDelLineOp infoOp = cmd.Info.Cast <CodeInfo_TXTDelLineOp>(); CodeInfo_TXTDelLine firstInfo = infoOp.Infos[0]; string fileName = StringEscaper.Preprocess(s, firstInfo.FileName); if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } if (!File.Exists(fileName)) { return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist")); } List <(CodeCommand, string)> prepDeleteLine = new List <(CodeCommand, string)>(infoOp.Cmds.Count); foreach (CodeCommand subCmd in infoOp.Cmds) { CodeInfo_TXTDelLine info = subCmd.Info.Cast <CodeInfo_TXTDelLine>(); string deleteLine = StringEscaper.Preprocess(s, info.DeleteLine); prepDeleteLine.Add((subCmd, deleteLine)); } // Detect encoding of text. Encoding encoding = EncodingHelper.SmartDetectEncoding(fileName, prepDeleteLine.Select(t => t.Item2)); int count = 0; string tempPath = FileHelper.GetTempFile(); using (StreamReader r = new StreamReader(fileName, encoding, false)) using (StreamWriter w = new StreamWriter(tempPath, false, encoding)) { string srcLine; while ((srcLine = r.ReadLine()) != null) { bool writeLine = true; foreach ((CodeCommand _, string deleteLine) in prepDeleteLine) { // Strange enough, WB082 treat [deleteLine] as case sensitive string. if (srcLine.StartsWith(deleteLine, StringComparison.Ordinal)) { writeLine = false; count++; break; } } if (writeLine) { w.WriteLine(srcLine); } } } FileHelper.FileReplaceEx(tempPath, fileName); foreach ((CodeCommand subCmd, string deleteLine) in prepDeleteLine) { logs.Add(new LogInfo(LogState.Success, $"Line [{deleteLine}] deleted from [{fileName}]", subCmd)); } logs.Add(new LogInfo(LogState.Success, $"Deleted [{count}] lines from [{fileName}]")); return(logs); }
public static List <LogInfo> TXTDelLineOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_TXTDelLineOp)); CodeInfo_TXTDelLineOp infoOp = cmd.Info as CodeInfo_TXTDelLineOp; string fileName = StringEscaper.Preprocess(s, infoOp.InfoList[0].FileName); if (StringEscaper.PathSecurityCheck(fileName, out string errorMsg) == false) { logs.Add(new LogInfo(LogState.Error, errorMsg)); return(logs); } if (File.Exists(fileName) == false) { logs.Add(new LogInfo(LogState.Error, $"File [{fileName}] not exists")); return(logs); } List <string> prepDeleteLine = new List <string>(); foreach (CodeInfo_TXTDelLine info in infoOp.InfoList) { string deleteLine = StringEscaper.Preprocess(s, info.DeleteLine); prepDeleteLine.Add(deleteLine); } Encoding encoding = FileHelper.DetectTextEncoding(fileName); int count = 0; string tempPath = Path.GetTempFileName(); using (StreamReader reader = new StreamReader(fileName, encoding)) using (StreamWriter writer = new StreamWriter(tempPath, false, encoding)) { string srcLine; while ((srcLine = reader.ReadLine()) != null) { bool writeLine = true; foreach (string deleteLine in prepDeleteLine) { // Strange enough, WB082 treat [deleteLine] as case sensitive string. if (srcLine.StartsWith(deleteLine, StringComparison.Ordinal)) { writeLine = false; count++; break; } } if (writeLine) { writer.WriteLine(srcLine); } } } FileHelper.FileReplaceEx(tempPath, fileName); logs.Add(new LogInfo(LogState.Success, $"Deleted [{count}] lines from [{fileName}]")); return(logs); }
private static CodeCommand PackCommand(CodeType type, List <CodeCommand> cmds) { Debug.Assert(0 < cmds.Count); CodeType packType; CodeInfo packInfo; switch (type) { case CodeType.TXTAddLine: packType = CodeType.TXTAddLineOp; packInfo = new CodeInfo_TXTAddLineOp(cmds); break; case CodeType.TXTReplace: packType = CodeType.TXTReplaceOp; packInfo = new CodeInfo_TXTReplaceOp(cmds); break; case CodeType.TXTDelLine: packType = CodeType.TXTDelLineOp; packInfo = new CodeInfo_TXTDelLineOp(cmds); break; case CodeType.IniRead: packType = CodeType.IniReadOp; packInfo = new CodeInfo_IniReadOp(cmds); break; case CodeType.IniWrite: packType = CodeType.IniWriteOp; packInfo = new CodeInfo_IniWriteOp(cmds); break; case CodeType.IniDelete: packType = CodeType.IniDeleteOp; packInfo = new CodeInfo_IniDeleteOp(cmds); break; case CodeType.IniReadSection: packType = CodeType.IniReadSectionOp; packInfo = new CodeInfo_IniReadSectionOp(cmds); break; case CodeType.IniAddSection: packType = CodeType.IniAddSectionOp; packInfo = new CodeInfo_IniAddSectionOp(cmds); break; case CodeType.IniDeleteSection: packType = CodeType.IniDeleteSectionOp; packInfo = new CodeInfo_IniDeleteSectionOp(cmds); break; case CodeType.IniWriteTextLine: packType = CodeType.IniWriteTextLineOp; packInfo = new CodeInfo_IniWriteTextLineOp(cmds); break; case CodeType.Visible: packType = CodeType.VisibleOp; packInfo = new CodeInfo_VisibleOp(cmds); break; case CodeType.ReadInterface: packType = CodeType.ReadInterfaceOp; packInfo = new CodeInfo_ReadInterfaceOp(cmds); break; case CodeType.WriteInterface: packType = CodeType.WriteInterfaceOp; packInfo = new CodeInfo_WriteInterfaceOp(cmds); break; case CodeType.WimExtract: packType = CodeType.WimExtractOp; packInfo = new CodeInfo_WimExtractOp(cmds); break; case CodeType.WimPathAdd: // Use WimPathAdd as representative of WimPath* packType = CodeType.WimPathOp; packInfo = new CodeInfo_WimPathOp(cmds); break; default: throw new InternalException("Internal Logic Error at CodeOptimizer.InternalOptimize"); } return(new CodeCommand(MergeRawCodes(cmds), cmds[0].Section, packType, packInfo, cmds[0].LineIdx)); }