Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        public static List <LogInfo> IniWriteTextLineOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

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

            string fileName = StringEscaper.Preprocess(s, infoOp.Infos[0].FileName);
            bool   append   = infoOp.Infos[0].Append;

            if (StringEscaper.PathSecurityCheck(fileName, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            List <IniKey> keyList = new List <IniKey>(infoOp.Infos.Count);

            for (int i = 0; i < infoOp.Infos.Count; i++)
            {
                CodeInfo_IniWriteTextLine info = infoOp.Infos[i];

                string sectionName = StringEscaper.Preprocess(s, info.Section);
                string line        = StringEscaper.Preprocess(s, info.Line);

                if (append)
                {
                    keyList.Add(new IniKey(sectionName, line));
                }
                else // prepend
                {
                    keyList.Insert(0, new IniKey(sectionName, line));
                }
            }
            IniKey[] keys = keyList.ToArray();

            string dirPath = Path.GetDirectoryName(fileName);

            if (Directory.Exists(dirPath) == false)
            {
                Directory.CreateDirectory(dirPath);
            }

            bool result = Ini.WriteRawLines(fileName, keyList, append);

            if (result)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    IniKey kv = keyList[i];
                    logs.Add(new LogInfo(LogState.Success, $"Line [{kv.Key}] written", infoOp.Cmds[i]));
                }
                logs.Add(new LogInfo(LogState.Success, $"Wrote [{keys.Length}] lines to [{fileName}]", cmd));
            }
            else
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    IniKey kv = keyList[i];
                    logs.Add(new LogInfo(LogState.Error, $"Could not write line [{kv.Key}]", infoOp.Cmds[i]));
                }
                logs.Add(new LogInfo(LogState.Error, $"Could not write [{keys.Length}] lines to [{fileName}]", cmd));
            }

            return(logs);
        }
Ejemplo n.º 3
0
        public static List <LogInfo> IniWriteTextLineOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

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

            string fileName = StringEscaper.Preprocess(s, infoOp.Infos[0].FileName);

            Debug.Assert(fileName != null, $"{nameof(fileName)} != null");

            bool append = infoOp.Infos[0].Append;

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            List <IniKey> keyList = new List <IniKey>(infoOp.Infos.Count);

            foreach (CodeInfo_IniWriteTextLine info in infoOp.Infos)
            {
                string sectionName = StringEscaper.Preprocess(s, info.Section);
                string line        = StringEscaper.Preprocess(s, info.Line);

                if (append)
                {
                    keyList.Add(new IniKey(sectionName, line));
                }
                else // prepend
                {
                    keyList.Insert(0, new IniKey(sectionName, line));
                }
            }
            IniKey[] keys = keyList.ToArray();

            string dirPath = Path.GetDirectoryName(fileName);

            if (dirPath == null)
            {
                throw new InternalException("Internal Logic Error at IniWriteTextLineOp");
            }
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            // If a dest file does not exist, create an empty file to force ANSI encoding as default in IniReadWriter.
            if (!File.Exists(fileName))
            {
                File.Create(fileName).Dispose();
            }

            bool result = IniReadWriter.WriteRawLines(fileName, keyList, append);

            if (result)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    IniKey kv = keyList[i];
                    logs.Add(new LogInfo(LogState.Success, $"Line [{kv.Key}] written", infoOp.Cmds[i]));
                }
                logs.Add(new LogInfo(LogState.Success, $"Wrote [{keys.Length}] lines to [{fileName}]", cmd));
            }
            else
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    IniKey kv = keyList[i];
                    logs.Add(new LogInfo(LogState.Error, $"Could not write line [{kv.Key}]", infoOp.Cmds[i]));
                }
                logs.Add(new LogInfo(LogState.Error, $"Could not write [{keys.Length}] lines to [{fileName}]", cmd));
            }

            return(logs);
        }