예제 #1
0
        public static List <LogInfo> TXTReplaceOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            CodeInfo_TXTReplaceOp infoOp = cmd.Info.Cast <CodeInfo_TXTReplaceOp>();

            CodeInfo_TXTReplace 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, string)> prepReplace = new List <(CodeCommand, string, string)>();

            // foreach (CodeInfo_TXTReplace info in infoOp.Infos)
            foreach (CodeCommand subCmd in infoOp.Cmds)
            {
                CodeInfo_TXTReplace info = subCmd.Info.Cast <CodeInfo_TXTReplace>();

                string oldStr = StringEscaper.Preprocess(s, info.OldStr);
                string newStr = StringEscaper.Preprocess(s, info.NewStr);

                prepReplace.Add((subCmd, oldStr, newStr));
            }

            Encoding encoding = EncodingHelper.DetectBom(fileName);

            string tempPath = FileHelper.GetTempFile();
            string txtStr;

            using (StreamReader r = new StreamReader(fileName, encoding, false))
            {
                txtStr = r.ReadToEnd();
            }

            foreach ((CodeCommand subCmd, string oldStr, string newStr) in prepReplace)
            {
                txtStr = StringHelper.ReplaceEx(txtStr, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", subCmd));
            }

            using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
            {
                w.Write(txtStr);
            }
            logs.Add(new LogInfo(LogState.Success, $"Replaced [{prepReplace.Count}] strings from [{fileName}]"));

            FileHelper.FileReplaceEx(tempPath, fileName);

            return(logs);
        }
예제 #2
0
        public static List <LogInfo> TXTReplaceOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_TXTReplaceOp));
            CodeInfo_TXTReplaceOp infoOp = cmd.Info as CodeInfo_TXTReplaceOp;

            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 <Tuple <string, string> > prepReplace = new List <Tuple <string, string> >();

            foreach (CodeInfo_TXTReplace info in infoOp.InfoList)
            {
                string oldStr = StringEscaper.Preprocess(s, info.OldStr);
                string newStr = StringEscaper.Preprocess(s, info.NewStr);
                prepReplace.Add(new Tuple <string, string>(oldStr, newStr));
            }

            Encoding encoding = FileHelper.DetectTextEncoding(fileName);

            string tempPath = Path.GetTempFileName();

            using (StreamReader reader = new StreamReader(fileName, encoding))
                using (StreamWriter writer = new StreamWriter(tempPath, false, encoding))
                {
                    string str = reader.ReadToEnd();
                    foreach (var tup in prepReplace)
                    {
                        string oldStr = tup.Item1;
                        string newStr = tup.Item2;

                        str = StringHelper.ReplaceEx(str, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                        logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]"));
                    }
                    writer.Write(str);
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            return(logs);
        }
예제 #3
0
        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));
        }