public static List <LogInfo> IniReadOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_IniReadOp)); CodeInfo_IniReadOp infoOp = cmd.Info as CodeInfo_IniReadOp; 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); } IniKey[] keys = new IniKey[infoOp.Cmds.Count]; for (int i = 0; i < keys.Length; i++) { CodeInfo_IniRead info = infoOp.Infos[i]; string sectionName = StringEscaper.Preprocess(s, info.Section); // WB082 : 여기 값은 변수 Expand 안한다. string key = StringEscaper.Preprocess(s, info.Key); // WB082 : 여기 값은 변수 Expand는 안 하나, Escaping은 한다. if (sectionName.Equals(string.Empty, StringComparison.Ordinal)) { throw new ExecuteException("Section name cannot be empty"); } if (key.Equals(string.Empty, StringComparison.Ordinal)) { throw new ExecuteException("Key name cannot be empty"); } keys[i] = new IniKey(sectionName, key); } keys = Ini.GetKeys(fileName, keys); int successCount = 0; for (int i = 0; i < keys.Length; i++) { IniKey kv = keys[i]; CodeCommand subCmd = infoOp.Cmds[i]; if (kv.Value != null) { logs.Add(new LogInfo(LogState.Success, $"Key [{kv.Key}] and its value [{kv.Value}] successfully read", subCmd)); string escapedValue = StringEscaper.Escape(kv.Value, false, true); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, escapedValue, false, false, false); LogInfo.AddCommand(varLogs, subCmd); logs.AddRange(varLogs); successCount += 1; } else { logs.Add(new LogInfo(LogState.Ignore, $"Key [{kv.Key}] does not exist", subCmd)); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, string.Empty, false, false, false); logs.AddRange(varLogs); } } logs.Add(new LogInfo(LogState.Success, $"Read [{successCount}] values from [{fileName}]", cmd)); return(logs); }
public static List <LogInfo> IniReadOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_IniReadOp infoOp = cmd.Info.Cast <CodeInfo_IniReadOp>(); string fileName = StringEscaper.Preprocess(s, infoOp.Infos[0].FileName); Debug.Assert(fileName != null, $"{nameof(fileName)} != null"); if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } IniKey[] keys = new IniKey[infoOp.Cmds.Count]; for (int i = 0; i < keys.Length; i++) { CodeInfo_IniRead info = infoOp.Infos[i]; string sectionName = StringEscaper.Preprocess(s, info.Section); string key = StringEscaper.Preprocess(s, info.Key); if (sectionName.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Section name cannot be empty")); } if (key.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Key name cannot be empty")); } keys[i] = new IniKey(sectionName, key); } keys = IniReadWriter.ReadKeys(fileName, keys); int successCount = 0; for (int i = 0; i < keys.Length; i++) { IniKey kv = keys[i]; CodeCommand subCmd = infoOp.Cmds[i]; if (kv.Value != null) { logs.Add(new LogInfo(LogState.Success, $"Key [{kv.Key}] and it's value [{kv.Value}] successfully read", subCmd)); string escapedValue = StringEscaper.Escape(kv.Value, false, true); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, escapedValue, false, false, false); LogInfo.AddCommand(varLogs, subCmd); logs.AddRange(varLogs); successCount += 1; } else { if (infoOp.Infos[i].DefaultValue != null) { logs.Add(new LogInfo(LogState.Ignore, $"Key [{kv.Key}] does not exist. Assigning default value [{infoOp.Infos[i].DefaultValue}]")); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, infoOp.Infos[i].DefaultValue, false, false, false); logs.AddRange(varLogs); } else { logs.Add(new LogInfo(LogState.Ignore, $"Key [{kv.Key}] does not exist", subCmd)); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, string.Empty, false, false, false); logs.AddRange(varLogs); } } } logs.Add(new LogInfo(LogState.Success, $"Read [{successCount}] values from [{fileName}]", cmd)); return(logs); }
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); }
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); }