Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeFactorの警告対策として2つ以上のクラスを定義しているcsファイルを分割する #1442

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="EncoderEscapingFallbackBuffer.cs" />
<Compile Include="EncoderEscapingFallback.cs" />
<Compile Include="LineEnumerator.cs" />
<Compile Include="FileContents.cs" />
<Compile Include="ChmSourceConverterApp.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
Expand Down
153 changes: 153 additions & 0 deletions tools/ChmSourceConverter/ChmSourceConverter/ChmSourceConverterApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Copyright (C) 2018-2020 Sakura Editor Organization

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.

2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ChmSourceConverter
{
/// <summary>
/// 変換アプリ本体
/// </summary>
internal class ChmSourceConverterApp
{
/// <summary>
/// 設定
/// </summary>
private readonly Properties.Settings Settings;

/// <summary>
/// 変換対象の拡張子
/// </summary>
private readonly IEnumerable<string> TargetExtensions;

/// <summary>
/// 入力ファイルのエンコーディング
/// </summary>
private readonly Encoding InputEncoding;

/// <summary>
/// HTML中に現れる文字セット定義のパターン(正規表現)
/// </summary>
private readonly Regex HtmlCharsetPattern;

/// <summary>
/// コンストラクタ
/// </summary>
public ChmSourceConverterApp(Properties.Settings settings)
{
Settings = settings;

TargetExtensions = Settings.TargetExtensions.Cast<string>().ToList();
InputEncoding = Encoding.GetEncoding(Settings.InputEncoding);
HtmlCharsetPattern = new Regex(Settings.ReplacePattern, RegexOptions.IgnoreCase);
}

/// <summary>
/// プログラムを開始します
/// </summary>
/// <param name="args"></param>
public void Start(string[] args)
{
if (args.Length < 1 && string.IsNullOrEmpty(args.FirstOrDefault()))
throw new ArgumentException("argument missing! Pass the target directory.", "args[0]");

string baseDirectory = args.First();
if (!Directory.Exists(baseDirectory))
throw new ArgumentException("invalid argument! the target directory does not exist.", "args[0]");

var files = Directory.EnumerateFiles(baseDirectory, "*.*", SearchOption.AllDirectories)
.Where(file => TargetExtensions.Any(ext => file.EndsWith(ext)));

ParallelOptions options = new ParallelOptions() { MaxDegreeOfParallelism = Settings.MaxDegreeOfParallelism };
Parallel.ForEach(files, options, (file) => DoConvertFile(file));
}

/// <summary>
/// ファイルを変換する
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public void DoConvertFile(string filename)
{
// 作業用ストリームを生成する
using (var memStream = new MemoryStream())
{
// 作業用ストリームにファイルを読み込む
ReadFileIntoMemory(filename, memStream);

// 作業用ストリームに蓄積したデータをbyte配列に書き出す
using (var destStream = new FileStream(filename, FileMode.Truncate, FileAccess.Write, FileShare.None))
memStream.WriteTo(destStream);
}
}

/// <summary>
/// ファイルをメモリに書き込む
/// </summary>
/// <param name="filename"></param>
/// <param name="stream"></param>
private void ReadFileIntoMemory(string filename, Stream stream)
{
// 変換用エンコーディングを生成する
Encoding outputEncoding = Encoding.GetEncoding(Settings.OutputEncoding,
new EncoderEscapingFallback(Settings.EscapingFormat),
new DecoderExceptionFallback());

// テキストライターを使ってストリームに読み込む
using (var writer = new StreamWriter(stream, outputEncoding, Settings.OutputBufferSize, true))
ReadLinesIntoMemory(filename, writer);
}

/// <summary>
/// ファイルから行を読み取ってメモリに書き込む
/// </summary>
/// <param name="filename"></param>
/// <param name="writer"></param>
private void ReadLinesIntoMemory(string filename, TextWriter writer)
{
bool IsHtml = Path.GetExtension(filename) == TargetExtensions.FirstOrDefault();

// 入力ファイルから行データを読み取る
using (var contents = new FileContents(filename, InputEncoding))
{
foreach (var line in contents)
{
// コンテンツフィルターを適用する
if (IsHtml && HtmlCharsetPattern.IsMatch(line))
{
IsHtml = false;
writer.WriteLine(HtmlCharsetPattern.Replace(line, $"$1{Settings.OutputEncoding}$2"));
continue;
}
writer.WriteLine(line);
}
}
}
}
}
100 changes: 0 additions & 100 deletions tools/ChmSourceConverter/ChmSourceConverter/EncoderEscapingFallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ 3. This notice may not be removed or altered from any source
distribution.
*/
using System;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -71,102 +69,4 @@ public override EncoderFallbackBuffer CreateFallbackBuffer()
return new EncoderEscapingFallbackBuffer(this);
}
}

/// <summary>
/// Represents a substitute input string that is used when the original input character cannot be encoded.
/// This class cannot be inherited.
/// </summary>
internal sealed class EncoderEscapingFallbackBuffer : EncoderFallbackBuffer
{
private ArraySegment<char> Buffer;
private EncoderEscapingFallback FallbackRef;

/// <inheritdoc />
public override int Remaining => Buffer.Count();

/// <summary>
/// Initializes a new instance of the EncoderEscapingFallbackBuffer class
/// using the value of a EncoderEscapingFallback object.
/// </summary>
/// <param name="fallback">A EncoderEscapingFallback object.</param>
public EncoderEscapingFallbackBuffer(EncoderEscapingFallback fallback)
{
Buffer = new ArraySegment<char>(Array.Empty<char>());
FallbackRef = fallback;
}

/// <summary>
/// Prepares the escaping fallback buffer to use the current format string.
/// </summary>
/// <param name="charUnknown"></param>
/// <returns></returns>
private bool Fallback(int charUnknown)
{
var escapedChar = string.Format(FallbackRef.Format, charUnknown);
Buffer = new ArraySegment<char>(escapedChar.ToCharArray());
return true;
}

/// <inheritdoc />
public override bool Fallback(char charUnknown, int index)
{
if (Buffer.Any())
{
throw new ArgumentException("This method is called again before the GetNextChar method has read all the replacement string characters.");
}

return Fallback((Int32)charUnknown);
}

/// <inheritdoc />
public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
{
if (Buffer.Any())
{
throw new ArgumentException("This method is called again before the GetNextChar method has read all the replacement string characters.");
}
if (charUnknownHigh < 0xD800 || charUnknownHigh > 0xD8FF)
{
throw new ArgumentOutOfRangeException("charUnknownHigh", charUnknownHigh, "The value of charUnknownHigh is out of range");
}
if (charUnknownLow < 0xDC00 || charUnknownLow > 0xDFFF)
{
throw new ArgumentOutOfRangeException("charUnknownLow", charUnknownLow, "The value of charUnknownLow is out of range");
}

int charUnknown = Char.ConvertToUtf32(charUnknownHigh, charUnknownLow);
return Fallback(charUnknown);
}

/// <inheritdoc />
public override char GetNextChar()
{
char ch = Buffer.FirstOrDefault();
if (Buffer.Any())
{
Buffer = new ArraySegment<char>(Buffer.Array, Buffer.Offset + 1, Buffer.Count - 1);
}
return ch;
}

/// <inheritdoc />
public override bool MovePrevious()
{
if (Buffer.Offset == 0) return false;
Buffer = new ArraySegment<char>(Buffer.Array, Buffer.Offset - 1, Buffer.Count + 1);
return true;
}

/// <summary>
/// Initializes all internal state information and data in this instance of System.Text.EncoderReplacementFallbackBuffer.
/// </summary>
[SecuritySafeCritical]
public override void Reset()
{
if (Buffer.Any())
{
Buffer = new ArraySegment<char>(Buffer.Array, 0, Buffer.Array.Length);
}
}
}
}
Loading