Beispiel #1
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);
        }