コード例 #1
0
ファイル: Json.cs プロジェクト: umerov1999/CSharpLib
        /// <summary>
        ///     Formats the specified JSON file and overwrites it if necessary.
        /// </summary>
        /// <exception cref="ArgumentNullException">
        ///     path is null.
        /// </exception>
        /// <exception cref="IOException">
        ///     path is invalid.
        /// </exception>
        public static bool FormatFile(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            var srcFile = PathEx.Combine(path);

            if (!PathEx.IsValidPath(srcFile))
            {
                throw new IOException();
            }
            var srcDir  = Path.GetDirectoryName(srcFile);
            var newFile = PathEx.GetUniquePath(srcDir, "tmp", ".json");

            using (var sr = new StreamReader(srcFile))
            {
                using var fs = new FileStream(newFile, FileMode.Create);
                int count;
                var ca       = new char[4096];
                var depth    = 0;
                var isEscape = false;
                var isValue  = false;
                while ((count = sr.Read(ca, 0, ca.Length)) > 0)
                {
                    Format(fs, ca, count, ' ', ref depth, ref isEscape, ref isValue);
                }
            }
            if (!FileEx.ContentIsEqual(srcFile, newFile))
            {
                return(FileEx.Move(newFile, srcFile, true));
            }
            FileEx.TryDelete(newFile);
            return(false);
        }
コード例 #2
0
        /// <summary>
        ///     Changes the character encoding of the specified file. This function
        ///     supports big files as well.
        /// </summary>
        /// <param name="file">
        ///     The file to change.
        /// </param>
        /// <param name="encoding">
        ///     The new character encoding. If <see langword="null"/>, <see cref="Ansi"/>
        ///     is used.
        /// </param>
        public static bool ChangeEncoding(string file, Encoding encoding = default)
        {
            if (string.IsNullOrEmpty(file))
            {
                return(false);
            }
            var srcFile = PathEx.Combine(file);

            if (!File.Exists(srcFile))
            {
                return(false);
            }
            encoding ??= Ansi;
            if (encoding.Equals(GetEncoding(srcFile)))
            {
                return(true);
            }
            try
            {
                var srcDir  = Path.GetDirectoryName(srcFile);
                var newFile = PathEx.Combine(srcDir, Path.GetRandomFileName());
                File.Create(newFile).Close();
                using (var sr = new StreamReader(srcFile))
                {
                    using var sw = new StreamWriter(newFile, true, encoding);
                    int i;
                    var ca = new char[4096];
                    while ((i = sr.Read(ca, 0, ca.Length)) > 0)
                    {
                        sw.Write(ca, 0, i);
                    }
                }
                if (!FileEx.ContentIsEqual(srcFile, newFile))
                {
                    return(FileEx.Move(newFile, srcFile, true));
                }
                FileEx.TryDelete(newFile);
                return(false);
            }
            catch (Exception ex) when(ex.IsCaught())
            {
                Log.Write(ex);
                return(false);
            }
        }