private static string getCmdOptions(CfgActions action) { return( (action.Options == null) ? "" : string.Join(", ", action.Options.Select(i => "'" + i + "'")) ); }
private static void infoParam(List <string> lines, CfgActions action) { FindParamResult fr = findParam(lines, action.Key); if (fr.IsError) { action.IsResult = false; action.ResultMsg = fr.Comment; } else { action.IsResult = true; action.ResultMsg = string.Format("'{0}', комментарий: {1}", fr.Value, fr.Comment); } }
private static void delParam(List <string> lines, CfgActions action) { FindParamResult fr = findParam(lines, action.Key); if ((fr.IsError) || (fr.Key == null) || (fr.LineIndex == 0)) { action.IsResult = false; action.ResultMsg = fr.Comment; return; } lines.RemoveAt(fr.LineIndex); // удалить комментарий if (fr.CommentIndex != 0) { deleteComment(lines, fr); } action.IsResult = true; action.ResultMsg = "параметр удален успешно"; }
private static void addCmdToActions(string cfgFile, string[] cmdArgs) { CfgCmdEnum cmd = CfgCmdEnum.None; string key = null; string[] options = null; string cmdName = null; if (cmdArgs != null) { if (cmdArgs.Length > 0) { cmdName = cmdArgs[0]; cmd = parseArgToCmd(cmdArgs[0]); } if (cmdArgs.Length > 1) { key = cmdArgs[1]; } if (cmdArgs.Length > 2) { options = cmdArgs.Skip(2).ToArray(); } } CfgActions action = new CfgActions() { CmdName = cmdName, Command = cmd, Key = key, Options = options }; if (_cfgActions.ContainsKey(cfgFile)) { _cfgActions[cfgFile].Add(action); } else { _cfgActions.Add(cfgFile, new List <CfgActions>() { action }); } }
private static void addParam(List <string> lines, CfgActions action) { if (string.IsNullOrEmpty(action.Key)) { action.IsResult = false; action.ResultMsg = "ошибка добавления параметра: не задан атрибут KEY"; return; } if ((action.Options == null) || (action.Options.Length == 0)) { action.IsResult = false; action.ResultMsg = "ошибка добавления параметра: не задано значение атрибута VALUE"; return; } string newValue = action.Options[0]; string newComment = (action.Options.Length > 1) ? "<!-- " + action.Options[1] + " -->" : null; bool isAdd = false; FindParamResult fr = findParam(lines, action.Key); // ключ найден - обновить значение if (fr.Key != null) { string lineUpd = fr.Line; int i1 = lineUpd.IndexOf("\"", lineUpd.IndexOf("value") + 5); int i2 = lineUpd.IndexOf("\"", i1 + 1); if ((i1 != -1) && (i2 != -1)) { string preValue = lineUpd.Substring(i1 + 1, i2 - i1 - 1); if (preValue == newValue) { action.IsResult = false; action.ResultMsg = "ошибка добавления параметра: параметр с таким значением уже существует, добавление не требуется"; } else { lineUpd = lineUpd.Substring(0, i1 + 1) + newValue + lineUpd.Substring(i2); lines[fr.LineIndex] = lineUpd; action.IsResult = true; action.ResultMsg = "параметр существует, атрибут VALUE обновлен новым значением"; } } else { action.IsResult = false; action.ResultMsg = "ошибка получения значения атрибута VALUE"; isAdd = true; } } else { isAdd = true; } // ключ не найден или ошибка - добавить if (isAdd) { string newLine = string.Format("<add key=\"{0}\" value=\"{1}\" />", action.Key, newValue); string prefixLine = getLinePrefix(lines); if (prefixLine != null) { newComment = prefixLine + newComment; newLine = prefixLine + newLine; } // вставить перед последней строкой if (lines.Last().TrimStart().StartsWith("</")) { lines.Insert(lines.Count - 1, ""); if (newComment != null) { lines.Insert(lines.Count - 1, newComment); } lines.Insert(lines.Count - 1, newLine); } // добавить к концу else { lines.Add(""); if (newComment != null) { lines.Add(newComment); } lines.Add(newLine); } action.IsResult = true; action.ResultMsg = "в файл добавлена строка " + newLine + ((newComment == null) ? "" : ", с комментарием " + newComment); } }
private static void updParam(List <string> lines, CfgActions action) { action.IsResult = false; FindParamResult fr = findParam(lines, action.Key); if ((fr.IsError) || (fr.Key == null) || (fr.LineIndex == 0)) { action.ResultMsg = fr.Comment; return; } if ((action.Options == null) || (action.Options.Length == 0)) { action.ResultMsg = "не указан обязательный режим обновления: -k[ey] | -v[alue] | -c[omment]"; return; } string mode = action.Options[0]; string newValue = ((action.Options.Length > 1) ? action.Options[1] : ""); // обновить имя параметра if ((mode == "-k") || (mode == "-key")) { if (newValue.Length == 0) { action.ResultMsg = "не указано обязательное новое ИМЯ параметра"; return; } int i1 = fr.Line.IndexOf("key"), i2 = -1; if (i1 > -1) { i1 = fr.Line.IndexOf("\"", i1 + 3); } if (i1 > -1) { i2 = fr.Line.IndexOf("\"", i1 + 1); } if ((i1 > -1) && (i2 > -1)) { string newLine = fr.Line.Substring(0, i1 + 1) + newValue + fr.Line.Substring(i2); lines[fr.LineIndex] = newLine; action.ResultMsg = "ИМЯ параметра изменено на '" + newValue + "'"; action.IsResult = true; } else { action.ResultMsg = "ошибка поиска значения атрибута KEY в исходной строке"; } } // обновить значение параметра else if ((mode == "-v") || (mode == "-value")) { if (newValue.Length == 0) { action.ResultMsg = "не указано обязательное новое ЗНАЧЕНИЕ параметра"; return; } int i1 = fr.Line.IndexOf("value"), i2 = -1; if (i1 > -1) { i1 = fr.Line.IndexOf("\"", i1 + 3); } if (i1 > -1) { i2 = fr.Line.IndexOf("\"", i1 + 1); } if ((i1 > -1) && (i2 > -1)) { string newLine = fr.Line.Substring(0, i1 + 1) + newValue + fr.Line.Substring(i2); lines[fr.LineIndex] = newLine; action.ResultMsg = "ЗНАЧЕНИЕ параметра изменено на '" + newValue + "'"; action.IsResult = true; } else { action.ResultMsg = "ошибка поиска значения атрибута VALUE в исходной строке"; } } // обновить комментарий else if ((mode == "-c") || (mode == "-comment")) { // есть комментарий в исходном файле if (fr.CommentIndex > 0) { // удалить комментарий if (string.IsNullOrEmpty(newValue)) { deleteComment(lines, fr); action.ResultMsg = "КОММЕНТАРИЙ удален"; action.IsResult = true; } // обновить комментарий else { string prefixLine = getLinePrefix(lines); string newLine = (prefixLine ?? "") + "<!-- " + newValue + " -->"; lines[fr.CommentIndex] = newLine; action.ResultMsg = "КОММЕНТАРИЙ изменен"; action.IsResult = true; } } // нет комментария и не задан - ошибка else if (string.IsNullOrEmpty(newValue)) { action.ResultMsg = "КОММЕНТАРИЙ к параметру НЕ найден и НЕ задан"; } // нет комментария и задан - добавить else { string prefixLine = getLinePrefix(lines); string newLine = (prefixLine ?? "") + "<!-- " + newValue + " -->"; lines.Insert(fr.LineIndex, newLine); lines.Insert(fr.LineIndex, ""); action.ResultMsg = "КОММЕНТАРИЙ добавлен"; action.IsResult = true; } } else { action.ResultMsg = "ошибочный режим обновления: " + mode + " (должен быть -k[ey], -v[alue] или -c[omment])"; } }