''' <summary>
''' 引数内のコマンド解析
''' </summary>
''' <remarks></remarks>
Public Class CommandParser
''' <summary>
''' コマンドを解析する
'''
''' 例. "-a bbb ccc -D eee"
''' このような引数を受け取ったとき下記のDictionaryに変換する
''' a,[bbb,ccc]
''' d,[eee]
''' </summary>
''' <param name="args">引数</param>
''' <returns>
''' コマンド名と値が対応したDictionaryを返す
''' ※コマンド名は小文字に変換される
''' ※解析できなかった場合も空のDictionaryを返す
''' </returns>
''' <remarks></remarks>
Public Function Parse(ByVal args As String()) As Dictionary(Of String, List(Of String))
Dim result As New Dictionary(Of String, List(Of String))
If args Is Nothing Then
Return result
End If
Dim commandName As String = String.Empty 'コマンド名
Dim commandArgs As List(Of String) = Nothing 'そのコマンドの引数
'1つずつ解析
For Each currentStr As String In args
If currentStr.StartsWith("-") Then
'コマンド名なら
'前に別のコマンドがあった場合は、登録
If Not String.IsNullOrWhiteSpace(commandName) Then
result.Add(commandName, commandArgs)
End If
commandName = currentStr.Substring(1).Trim.ToLower
commandArgs = New List(Of String)
Else
'そのコマンドの引数なら
If Not String.IsNullOrWhiteSpace(commandName) Then
commandArgs.Add(currentStr.Trim)
End If
End If
Next
'前に別のコマンドがあった場合は、登録
If Not String.IsNullOrWhiteSpace(commandName) Then
result.Add(commandName, commandArgs)
End If
Return result
End Function
End Class
Imports System.IO
''' <summary>
''' Encode解析
''' ※インスタンス生成不可
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class EncodingParser
#Region "Initialize"
''' <summary>
''' コンスタントラクタ
''' ※インスタンス生成不可
''' </summary>
''' <remarks></remarks>
Private Sub New()
End Sub
#End Region
#Region "Shared Methods"
''' <summary>
''' エンコードを取得
''' </summary>
''' <param name="file">
''' ファイルのパス
''' </param>
''' <returns></returns>
''' <exception cref="ArgumentNullException">引数がNothingのとき</exception>
''' <exception cref="FileNotFoundException">指定したファイルが見つからなかったとき</exception>
''' <remarks></remarks>
Public Shared Function GetCode(ByVal file As String) As System.Text.Encoding
If String.IsNullOrEmpty(file) Then Throw New ArgumentNullException("file ")
Dim fileInfo As New IO.FileInfo(file)
If Not fileInfo.Exists Then
Throw New IO.FileNotFoundException("file ")
End If
Return GetCode(My.Computer.FileSystem.ReadAllBytes(file))
End Function
#End Region
#Region "Private Methods"
''' <summary>
''' 文字コードを判別する
''' </summary>
''' <remarks>
''' Jcode.pmのgetcodeメソッドを移植したものです。
''' Jcode.pm(http://openlab.ring.gr.jp/Jcode/index-j.html)
''' Jcode.pmのCopyright: Copyright 1999-2005 Dan Kogai
''' </remarks>
''' <param name="bytes">文字コードを調べるデータ</param>
''' <returns>適当と思われるEncodingオブジェクト。
''' 判断できなかった時はnull。</returns>
Private Shared Function GetCode(ByVal bytes As Byte()) As System.Text.Encoding
Const bEscape As Byte = &H1B
Const bAt As Byte = &H40
Const bDollar As Byte = &H24
Const bAnd As Byte = &H26
Const bOpen As Byte = &H28 ''('
Const bB As Byte = &H42
Const bD As Byte = &H44
Const bJ As Byte = &H4A
Const bI As Byte = &H49
Dim len As Integer = bytes.Length
Dim b1 As Byte, b2 As Byte, b3 As Byte, b4 As Byte
'Encode::is_utf8 は無視
Dim isBinary As Boolean = False
Dim i As Integer
For i = 0 To len - 1
b1 = bytes(i)
If b1 <= &H6 OrElse b1 = &H7F OrElse b1 = &HFF Then
''binary'
isBinary = True
If b1 = &H0 AndAlso i < len - 1 AndAlso bytes(i + 1) <= &H7F Then
'smells like raw unicode
Return System.Text.Encoding.Unicode
End If
End If
Next
If isBinary Then
Return Nothing
End If
'not Japanese
Dim notJapanese As Boolean = True
For i = 0 To len - 1
b1 = bytes(i)
If b1 = bEscape OrElse &H80 <= b1 Then
notJapanese = False
Exit For
End If
Next
If notJapanese Then
Return System.Text.Encoding.ASCII
End If
For i = 0 To len - 3
b1 = bytes(i)
b2 = bytes(i + 1)
b3 = bytes(i + 2)
If b1 = bEscape Then
If b2 = bDollar AndAlso b3 = bAt Then
'JIS_0208 1978
'JIS
Return System.Text.Encoding.GetEncoding(50220)
ElseIf b2 = bDollar AndAlso b3 = bB Then
'JIS_0208 1983
'JIS
Return System.Text.Encoding.GetEncoding(50220)
ElseIf b2 = bOpen AndAlso (b3 = bB OrElse b3 = bJ) Then
'JIS_ASC
'JIS
Return System.Text.Encoding.GetEncoding(50220)
ElseIf b2 = bOpen AndAlso b3 = bI Then
'JIS_KANA
'JIS
Return System.Text.Encoding.GetEncoding(50220)
End If
If i < len - 3 Then
b4 = bytes(i + 3)
If b2 = bDollar AndAlso b3 = bOpen AndAlso b4 = bD Then
'JIS_0212
'JIS
Return System.Text.Encoding.GetEncoding(50220)
End If
If i < len - 5 AndAlso _
b2 = bAnd AndAlso b3 = bAt AndAlso b4 = bEscape AndAlso _
bytes(i + 4) = bDollar AndAlso bytes(i + 5) = bB Then
'JIS_0208 1990
'JIS
Return System.Text.Encoding.GetEncoding(50220)
End If
End If
End If
Next
'should be euc|sjis|utf8
'use of (?:) by Hiroki Ohzaki <
[email protected]>
Dim sjis As Integer = 0
Dim euc As Integer = 0
Dim utf8 As Integer = 0
For i = 0 To len - 2
b1 = bytes(i)
b2 = bytes(i + 1)
If ((&H81 <= b1 AndAlso b1 <= &H9F) OrElse _
(&HE0 <= b1 AndAlso b1 <= &HFC)) AndAlso _
((&H40 <= b2 AndAlso b2 <= &H7E) OrElse _
(&H80 <= b2 AndAlso b2 <= &HFC)) Then
'SJIS_C
sjis += 2
i += 1
End If
Next
For i = 0 To len - 2
b1 = bytes(i)
b2 = bytes(i + 1)
If ((&HA1 <= b1 AndAlso b1 <= &HFE) AndAlso _
(&HA1 <= b2 AndAlso b2 <= &HFE)) OrElse _
(b1 = &H8E AndAlso (&HA1 <= b2 AndAlso b2 <= &HDF)) Then
'EUC_C
'EUC_KANA
euc += 2
i += 1
ElseIf i < len - 2 Then
b3 = bytes(i + 2)
If b1 = &H8F AndAlso (&HA1 <= b2 AndAlso b2 <= &HFE) AndAlso _
(&HA1 <= b3 AndAlso b3 <= &HFE) Then
'EUC_0212
euc += 3
i += 2
End If
End If
Next
For i = 0 To len - 2
b1 = bytes(i)
b2 = bytes(i + 1)
If (&HC0 <= b1 AndAlso b1 <= &HDF) AndAlso _
(&H80 <= b2 AndAlso b2 <= &HBF) Then
'UTF8
utf8 += 2
i += 1
ElseIf i < len - 2 Then
b3 = bytes(i + 2)
If (&HE0 <= b1 AndAlso b1 <= &HEF) AndAlso _
(&H80 <= b2 AndAlso b2 <= &HBF) AndAlso _
(&H80 <= b3 AndAlso b3 <= &HBF) Then
'UTF8
utf8 += 3
i += 2
End If
End If
Next
'M. Takahashi's suggestion
'utf8 += utf8 / 2;
System.Diagnostics.Debug.WriteLine( _
String.Format("sjis = {0}, euc = {1}, utf8 = {2}", sjis, euc, utf8))
If euc > sjis AndAlso euc > utf8 Then
'EUC
Return System.Text.Encoding.GetEncoding(51932)
ElseIf sjis > euc AndAlso sjis > utf8 Then
'SJIS
Return System.Text.Encoding.GetEncoding(932)
ElseIf utf8 > euc AndAlso utf8 > sjis Then
'UTF8
Return System.Text.Encoding.UTF8
End If
Return Nothing
End Function
#End Region
End Class
Imports System.Text
''' <summary>
''' ファイルの要約
''' </summary>
''' <remarks></remarks>
Public Structure FileSummary
#Region "Variables"
Private _filepath As String
Private _encode As System.Text.Encoding
#End Region
#Region "Properties"
''' <summary>
''' ファイルのパス
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property FilePath() As String
Get
Return _filepath
End Get
Set(ByVal value As String)
_filepath = value
End Set
End Property
''' <summary>
''' ファイルのエンコード
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Encode() As Encoding
Get
Return _encode
End Get
Set(ByVal value As Encoding)
_encode = value
End Set
End Property
#End Region
End Structure
最終更新:2014年11月27日 01:17