Exemple #1
0
        public static List <LogInfo> IniReadSection(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_IniReadSection info = cmd.Info.Cast <CodeInfo_IniReadSection>();

            string fileName = StringEscaper.Preprocess(s, info.FileName);
            string section  = StringEscaper.Preprocess(s, info.Section);
            string delim    = "|";

            if (info.Delim != null)
            {
                delim = StringEscaper.Preprocess(s, info.Delim);
            }

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

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

            IniKey[] keys = IniReadWriter.ReadSection(fileName, section);
            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 from [{fileName}]"));

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

                List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, string.Empty, false, false, false);
                logs.AddRange(varLogs);
            }

            return(logs);
        }