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

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

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

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

            string[] sections = new string[infoOp.Cmds.Count];
            string[] destVars = new string[infoOp.Cmds.Count];
            for (int i = 0; i < sections.Length; i++)
            {
                CodeInfo_IniReadSection info = infoOp.Infos[i];

                string section = StringEscaper.Preprocess(s, info.Section);
                if (section.Equals(string.Empty, StringComparison.Ordinal))
                {
                    throw new ExecuteException("Section name cannot be empty");
                }

                sections[i] = section;
                destVars[i] = info.DestVar;
            }

            Dictionary <string, IniKey[]> keyDict = Ini.ReadSections(fileName, sections);

            int successCount = 0;

            for (int i = 0; i < sections.Length; i++)
            {
                string      section = sections[i];
                IniKey[]    keys    = keyDict[section];
                CodeCommand subCmd  = infoOp.Cmds[i];

                if (keys != null)
                {
                    StringBuilder b = new StringBuilder();
                    b.AppendLine($"[{section}]");
                    foreach (IniKey k in keys)
                    {
                        b.AppendLine($"{k.Key}={k.Value}");
                    }

                    logs.Add(new LogInfo(LogState.Success, $"Section [{section}] read", subCmd));

                    string         escapedValue = StringEscaper.Escape(b.ToString(), false, true);
                    List <LogInfo> varLogs      = Variables.SetVariable(s, destVars[i], escapedValue, false, false, false);
                    LogInfo.AddCommand(varLogs, subCmd);
                    logs.AddRange(varLogs);
                }
                else
                {
                    logs.Add(new LogInfo(LogState.Ignore, $"Section [{section}] does not exist", subCmd));

                    List <LogInfo> varLogs = Variables.SetVariable(s, destVars[i], string.Empty, false, false, false);
                    LogInfo.AddCommand(varLogs, subCmd);
                    logs.AddRange(varLogs);
                }
            }
            logs.Add(new LogInfo(LogState.Success, $"Read [{successCount}] sections from [{fileName}]", cmd));

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

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

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

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

            string[] sections = new string[infoOp.Cmds.Count];
            string[] destVars = new string[infoOp.Cmds.Count];
            string[] delims   = new string[infoOp.Cmds.Count];
            for (int i = 0; i < sections.Length; i++)
            {
                CodeInfo_IniReadSection info = infoOp.Infos[i];

                string section = StringEscaper.Preprocess(s, info.Section);
                if (section.Length == 0)
                {
                    return(LogInfo.LogErrorMessage(logs, "Section name cannot be empty"));
                }

                sections[i] = section;
                destVars[i] = info.DestVar;
                delims[i]   = "|";
                if (info.Delim != null)
                {
                    delims[i] = StringEscaper.Preprocess(s, info.Delim);
                }
            }

            Dictionary <string, IniKey[]> keyDict = IniReadWriter.ReadSections(fileName, sections);

            int successCount = 0;

            for (int i = 0; i < sections.Length; i++)
            {
                string      section = sections[i];
                string      delim   = delims[i];
                IniKey[]    keys    = keyDict[section];
                CodeCommand subCmd  = infoOp.Cmds[i];

                if (keys != null)
                {
                    List <string> kvList = new List <string>(keys.Length * 2);
                    foreach (IniKey k in keys)
                    {
                        kvList.Add(k.Key);
                        kvList.Add(k.Value);
                    }
                    string destStr = StringEscaper.PackListStr(kvList, delim);
                    logs.Add(new LogInfo(LogState.Success, $"Section [{section}] read", subCmd));

                    string         escapedValue = StringEscaper.Escape(destStr, false, true);
                    List <LogInfo> varLogs      = Variables.SetVariable(s, destVars[i], escapedValue, false, false, false);
                    LogInfo.AddCommand(varLogs, subCmd);
                    logs.AddRange(varLogs);
                }
                else
                {
                    logs.Add(new LogInfo(LogState.Ignore, $"Section [{section}] does not exist", subCmd));

                    List <LogInfo> varLogs = Variables.SetVariable(s, destVars[i], string.Empty, false, false, false);
                    LogInfo.AddCommand(varLogs, subCmd);
                    logs.AddRange(varLogs);
                }
            }
            logs.Add(new LogInfo(LogState.Success, $"Read [{successCount}] sections from [{fileName}]", cmd));

            return(logs);
        }