Esempio n. 1
0
        public static List <LogInfo> TXTDelSpaces(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_TXTDelSpaces));
            CodeInfo_TXTDelSpaces info = cmd.Info as CodeInfo_TXTDelSpaces;

            string fileName = StringEscaper.Preprocess(s, info.FileName);

            if (StringEscaper.PathSecurityCheck(fileName, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            if (File.Exists(fileName) == false)
            {
                logs.Add(new LogInfo(LogState.Error, $"File [{fileName}] not exists"));
                return(logs);
            }

            Encoding encoding = FileHelper.DetectTextEncoding(fileName);

            int    i        = 0;
            string tempPath = Path.GetTempFileName();

            using (StreamReader reader = new StreamReader(fileName, encoding))
                using (StreamWriter writer = new StreamWriter(tempPath, false, encoding))
                {
                    string srcLine;
                    while ((srcLine = reader.ReadLine()) != null)
                    {
                        // WB082 delete spaces only if spaces are placed in front of line.
                        // Same with C#'s string.TrimStart().
                        int count = StringHelper.CountOccurrences(srcLine, " ");
                        if (0 < count)
                        {
                            i++;
                            srcLine = srcLine.TrimStart();
                        }
                        writer.WriteLine(srcLine);
                    }
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Deleted [{i}] spaces"));

            return(logs);
        }
Esempio n. 2
0
        public static List <LogInfo> TXTDelSpaces(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

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

            string fileName = StringEscaper.Preprocess(s, info.FileName);

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

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            Encoding encoding = EncodingHelper.DetectEncoding(fileName);

            int    linesTrimmed = 0;
            string tempPath     = FileHelper.GetTempFile();

            using (StreamReader sr = new StreamReader(fileName, encoding, false))
                using (StreamWriter sw = new StreamWriter(tempPath, false, encoding))
                {
                    string srcLine;
                    while ((srcLine = sr.ReadLine()) != null)
                    {
                        int count = StringHelper.CountSubStr(srcLine, " ");
                        if (0 < count)
                        {
                            srcLine = srcLine.Trim();
                            if (!StringHelper.CountSubStr(srcLine, " ").Equals(count)) //only count lines that we actually trimmed
                            {
                                linesTrimmed++;
                            }
                        }
                        sw.WriteLine(srcLine);
                    }
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Deleted leading and trailing spaces from [{linesTrimmed}] lines"));

            return(logs);
        }
Esempio n. 3
0
        public static List <LogInfo> TXTDelSpaces(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

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

            string fileName = StringEscaper.Preprocess(s, info.FileName);

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

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            Encoding encoding = EncodingHelper.DetectBom(fileName);

            int    i        = 0;
            string tempPath = Path.GetTempFileName();

            using (StreamReader r = new StreamReader(fileName, encoding))
                using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
                {
                    string srcLine;
                    while ((srcLine = r.ReadLine()) != null)
                    {
                        // WB082 delete spaces only if spaces are placed in front of line.
                        // Same with C#'s string.TrimStart().
                        int count = StringHelper.CountSubStr(srcLine, " ");
                        if (0 < count)
                        {
                            i++;
                            srcLine = srcLine.TrimStart();
                        }
                        w.WriteLine(srcLine);
                    }
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            logs.Add(new LogInfo(LogState.Success, $"Deleted [{i}] spaces"));

            return(logs);
        }