Пример #1
0
        public static List <LogInfo> IniDeleteOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_IniDeleteOp infoOp = cmd.Info.Cast <CodeInfo_IniDeleteOp>();

            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_IniDelete 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);
            }

            bool[] result;
            if (s.CompatAutoCompactIniWriteCommand)
            {
                result = IniReadWriter.DeleteCompactKeys(fileName, keys);
            }
            else
            {
                result = IniReadWriter.DeleteKeys(fileName, keys);
            }

            int successCount = 0;

            for (int i = 0; i < keys.Length; i++)
            {
                IniKey kv = keys[i];
                if (result[i])
                {
                    successCount += 1;
                    logs.Add(new LogInfo(LogState.Success, $"Key [{kv.Key}] deleted", infoOp.Cmds[i]));
                }
                else
                {
                    logs.Add(new LogInfo(LogState.Ignore, $"Could not delete key [{kv.Key}]", infoOp.Cmds[i]));
                }
            }

            if (0 < successCount)
            {
                logs.Add(new LogInfo(LogState.Success, $"Deleted [{keys.Length}] values from [{fileName}]", cmd));
            }
            if (0 < keys.Length - successCount)
            {
                logs.Add(new LogInfo(LogState.Ignore, $"Could not delete [{keys.Length}] values from [{fileName}]", cmd));
            }

            return(logs);
        }