Using system;
Using system. Collections. Generic;
Using system. IO;
Using system. text;
Namespace source code modification
{
Public class convertencodingtoutf8: ioperatesource
{
Private int amountofhasconverted;
Public int amountofhasconverted
{
Get
{
Return amountofhasconverted;
}
}
Public convertencodingtoutf8 ()
{
Amountofhasconverted = 0;
}
Private encoding GetType (fileinfo sourcefileinfo)
{
Filestream FS = new filestream (sourcefileinfo. fullname, filemode. Open, fileaccess. Read );
Binaryreader reader = new binaryreader (FS, system. Text. encoding. Default );
Byte [] Ss = reader. readbytes (3 );
Reader. Close ();
Encoding encoding;
If (ss [0]> = 0xef)
{
If (ss [0] = 0xef & SS [1] = 0xbb & SS [2] = 0xbf)
{
Encoding = encoding. utf8;
}
Else if (ss [0] = 0xfe & SS [1] = 0xff)
{
Encoding = encoding. bigendianunicode;
}
Else if (ss [0] = 0xff & SS [1] = 0xfe)
{
Encoding = encoding. Unicode;
}
Else
{
Encoding = encoding. default;
}
}
Else
{
Encoding = encoding. default;
}
FS. Close ();
Return encoding;
}
Override public void go (fileinfo sourcefileinfo)
{
Encoding encoding = GetType (sourcefileinfo );
If (encoding! = Encoding. utf8)
{
++ Amountofhasconverted;
Streamreader reader = new streamreader (sourcefileinfo. fullname, encoding );
String Buffer = reader. readtoend ();
Reader. Close ();
Streamwriter writer = new streamwriter (sourcefileinfo. fullname, false, encoding. utf8 );
Writer. Write (buffer );
Writer. Close ();
}
}
}
}