public static List <LogInfo> GetParam(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(2); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_GetParam)); CodeInfo_GetParam info = cmd.Info as CodeInfo_GetParam; string indexStr = StringEscaper.Preprocess(s, info.Index); if (!NumberHelper.ParseInt32(indexStr, out int index)) { logs.Add(new LogInfo(LogState.Error, $"[{indexStr}] is not a valid integer")); return(logs); } if (s.CurSectionParams.ContainsKey(index) && index <= s.CurSectionParamsCount) { string parameter = StringEscaper.Escape(s.CurSectionParams[index], true, false); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, parameter, false, false); logs.AddRange(varLogs); } else { logs.Add(new LogInfo(LogState.Ignore, $"Section parameter [#{index}] does not exist")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, string.Empty, false, false); logs.AddRange(varLogs); } return(logs); }
public static void EscapeThenUnescapeTemplate(string src, bool fullEscape, bool escapePercent) { string escaped = StringEscaper.Escape(src, fullEscape, escapePercent); string unescaped = StringEscaper.Unescape(escaped, escapePercent); Assert.IsTrue(unescaped.Equals(src, StringComparison.Ordinal)); }
private static string getValueQuery(string text) { var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (lines.Length == 0) { return(string.Empty); } var builder = new StringBuilder(); if (lines.Length == 1 && lines[0].IndexOf(' ') < 0) { builder.Append(StringEscaper.Escape(lines[0])); } else if (lines.Length >= 1) { builder.Append('"'); for (int i = 0; i < lines.Length; i++) { if (i > 0) { builder.Append(' '); } builder.Append(StringEscaper.Escape(lines[i])); } builder.Append('"'); } return(builder.ToString()); }
public static List <LogInfo> IniRead(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_IniRead info = cmd.Info.Cast <CodeInfo_IniRead>(); string fileName = StringEscaper.Preprocess(s, info.FileName); string sectionName = StringEscaper.Preprocess(s, info.Section); string key = StringEscaper.Preprocess(s, info.Key); string defaultValue = null; if (info.DefaultValue != null) { defaultValue = StringEscaper.Preprocess(s, info.DefaultValue); } Debug.Assert(fileName != null, $"{nameof(fileName)} != null"); Debug.Assert(sectionName != null, $"{nameof(sectionName)} != null"); Debug.Assert(key != null, $"{nameof(key)} != null"); 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")); } string value = IniReadWriter.ReadKey(fileName, sectionName, key); if (value != null) { logs.Add(new LogInfo(LogState.Success, $"Key [{key}] and it's value [{value}] read from [{fileName}]")); string escapedValue = StringEscaper.Escape(value, false, true); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, escapedValue, false, false, false); logs.AddRange(varLogs); } else { if (defaultValue != null) { logs.Add(new LogInfo(LogState.Ignore, $"Key [{key}] does not exist in [{fileName}]. Assigning default value [{defaultValue}]")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, defaultValue, false, false, false); logs.AddRange(varLogs); } else { logs.Add(new LogInfo(LogState.Ignore, $"Key [{key}] does not exist in [{fileName}]")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, string.Empty, false, false, false); logs.AddRange(varLogs); } } return(logs); }
public void Escape_3() { string src = StringEscaperTests.SampleString; string dest = StringEscaper.Escape(src, true, true); const string comp = "Comma#$s[#$c]#$xPercent#$s[#$p]#$xDoubleQuote#$s[#$q]#$xSpace#$s[#$s]#$xTab#$s[#$t]#$xSharp#$s[##]#$xNewLine#$s[#$x]"; Assert.IsTrue(dest.Equals(comp, StringComparison.Ordinal)); }
public void Escape_1() { string src = StringEscaperTests.SampleString; string dest = StringEscaper.Escape(src, false, false); const string comp = "Comma [,]#$xPercent [%]#$xDoubleQuote [#$q]#$xSpace [ ]#$xTab [#$t]#$xSharp [##]#$xNewLine [#$x]"; Assert.IsTrue(dest.Equals(comp, StringComparison.Ordinal)); }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (!(value is string rawStr)) { return(Binding.DoNothing); } return(StringEscaper.Escape(rawStr, false, true)); }
public static void EscapeArrayTemplate(string[] srcStrs, bool fullEscape, bool escapePercent, string[] expected) { List <string> destStrs = StringEscaper.Escape(srcStrs, fullEscape, escapePercent); for (int i = 0; i < destStrs.Count; i++) { Assert.IsTrue(destStrs[i].Equals(expected[i], StringComparison.Ordinal)); } }
public static void EscapeThenUnescapeArrayTemplate(string[] srcs, bool fullEscape, bool escapePercent) { List <string> escaped = StringEscaper.Escape(srcs, fullEscape, escapePercent); List <string> unescaped = StringEscaper.Unescape(escaped, escapePercent); for (int i = 0; i < unescaped.Count; i++) { Assert.IsTrue(unescaped[i].Equals(srcs[i], StringComparison.Ordinal)); } }
private static string getValueExpression(string value) { string escaped = StringEscaper.Escape(value); if (!value.Any(char.IsWhiteSpace)) { return(escaped); } return($"\"{escaped}\""); }
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); }
public static List <LogInfo> PackParam(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(4); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_PackParam)); CodeInfo_PackParam info = cmd.Info as CodeInfo_PackParam; string startIndexStr = StringEscaper.Preprocess(s, info.StartIndex); if (!NumberHelper.ParseInt32(startIndexStr, out int startIndex)) { logs.Add(new LogInfo(LogState.Error, $"[{startIndexStr}] is not a valid integer")); return(logs); } int varCount = s.CurSectionParamsCount; if (startIndex <= varCount) { StringBuilder b = new StringBuilder(); for (int i = 1; i <= varCount; i++) { b.Append('"'); if (s.CurSectionParams.ContainsKey(i)) { b.Append(StringEscaper.Escape(s.CurSectionParams[i], true, false)); } b.Append('"'); if (i + 1 <= varCount) { b.Append(','); } } logs.AddRange(Variables.SetVariable(s, info.DestVar, b.ToString(), false, false)); } else { logs.Add(new LogInfo(LogState.Ignore, $"StartIndex [#{startIndex}] is invalid, [{varCount}] section parameters provided.")); logs.AddRange(Variables.SetVariable(s, info.DestVar, string.Empty, false, false)); } if (info.VarCount != null) { logs.AddRange(Variables.SetVariable(s, info.VarCount, varCount.ToString(), false, false)); } return(logs); }
public void Escape_4() { string[] srcs = { "Comma [,]", "Space [ ]", "DoubleQuote [\"]" }; List <string> dests = StringEscaper.Escape(srcs, true); string[] comps = { "Comma#$s[#$c]", "Space#$s[#$s]", "DoubleQuote#$s[#$q]", }; for (int i = 0; i < dests.Count; i++) { Assert.IsTrue(dests[i].Equals(comps[i], StringComparison.Ordinal)); } }
public static List <LogInfo> IniReadSection(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_IniReadSection)); CodeInfo_IniReadSection info = cmd.Info as CodeInfo_IniReadSection; string fileName = StringEscaper.Preprocess(s, info.FileName); string section = StringEscaper.Preprocess(s, info.Section); if (section.Equals(string.Empty, StringComparison.Ordinal)) { throw new ExecuteException("Section name cannot be empty"); } IniKey[] keys = Ini.ReadSection(fileName, section); 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 in [{fileName}]")); string escapedValue = StringEscaper.Escape(b.ToString(), 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); }
public static List <LogInfo> IniRead(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_IniRead)); CodeInfo_IniRead info = cmd.Info as CodeInfo_IniRead; string fileName = StringEscaper.Preprocess(s, info.FileName); string sectionName = StringEscaper.Preprocess(s, info.Section); string key = StringEscaper.Preprocess(s, info.Key); 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"); } string value = Ini.GetKey(fileName, sectionName, key); if (value != null) { logs.Add(new LogInfo(LogState.Success, $"Key [{key}] and its value [{value}] read from [{fileName}]")); string escapedValue = StringEscaper.Escape(value, false, true); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, escapedValue, false, false, false); logs.AddRange(varLogs); } else { logs.Add(new LogInfo(LogState.Ignore, $"Key [{key}] does not exist in [{fileName}]")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, string.Empty, false, false, false); logs.AddRange(varLogs); } return(logs); }
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> 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); }
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 void EscapeTemplate(string srcStr, bool fullEscape, bool escapePercent, string expected) { string dest = StringEscaper.Escape(srcStr, fullEscape, escapePercent); Assert.IsTrue(dest.Equals(expected, StringComparison.Ordinal)); }
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); }