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

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

            string srcFile  = StringEscaper.Preprocess(s, info.SrcFile);
            string destFile = StringEscaper.Preprocess(s, info.DestFile);

            Debug.Assert(srcFile != null, $"{nameof(srcFile)} != null");
            Debug.Assert(destFile != null, $"{nameof(destFile)} != null");

            if (!StringEscaper.PathSecurityCheck(destFile, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            // If a dest file does not exist, create an empty file to force ANSI encoding as default in IniReadWriter.
            if (!File.Exists(destFile))
            {
                File.Create(destFile).Dispose();
            }

            bool result;

            if (s.CompatAutoCompactIniWriteCommand)
            {
                result = IniReadWriter.MergeCompact(srcFile, destFile);
            }
            else
            {
                result = IniReadWriter.Merge(srcFile, destFile);
            }

            if (result)
            {
                logs.Add(new LogInfo(LogState.Success, $"[{srcFile}] merged into [{destFile}]", cmd));
            }
            else
            {
                logs.Add(new LogInfo(LogState.Error, $"Could not merge [{srcFile}] into [{destFile}]", cmd));
            }

            return(logs);
        }