-
Notifications
You must be signed in to change notification settings - Fork 63
Create Автоматическое определение кодировки при чтении файла.cs #42
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
Open
leotop
wants to merge
1
commit into
ZennoHelpers:master
Choose a base branch
from
leotop:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Сниппеты/[Текст]/[Декодирование]/Автоматическое определение кодировки при чтении файла.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Общий код | ||
namespace Analizing | ||
{ | ||
public static class EncodingTest | ||
{ | ||
/// <summary> | ||
/// UTF8 : EF BB BF | ||
/// UTF16 BE : FE FF | ||
/// UTF16 LE : FF FE | ||
/// UTF32 BE : 00 00 FE FF | ||
/// UTF32 LE : FF FE 00 00 | ||
/// </summary> | ||
public static Encoding DetectEncoding(string path) | ||
{ | ||
FileStream fstream = new FileStream(path, FileMode.OpenOrCreate); | ||
Encoding result=Encoding.Default; | ||
if (!fstream.CanSeek || !fstream.CanRead){ | ||
fstream.Close(); | ||
throw new Exception("DetectEncoding() файл не может быть прочитан"); | ||
} | ||
long Length_File = fstream.Length; | ||
int Length_Probe_Read = 1000; | ||
if (Length_Probe_Read >Length_File )Length_Probe_Read = Convert.ToInt32(Length_File); | ||
Byte[] u8_Buf = new Byte[Length_Probe_Read]; | ||
int s32_Count = fstream.Read(u8_Buf, 0, Length_Probe_Read); | ||
if (s32_Count >= 2) | ||
{ | ||
if (u8_Buf[0] == 0xFE && u8_Buf[1] == 0xFF) { | ||
result = new UnicodeEncoding(true, true); | ||
} | ||
if (u8_Buf[0] == 0xFF && u8_Buf[1] == 0xFE) { | ||
if (s32_Count >= 4 && u8_Buf[2] == 0 && u8_Buf[3] == 0) { | ||
result = new UTF32Encoding(false, true); | ||
} | ||
else { | ||
result = new UnicodeEncoding(false, true); | ||
} | ||
} | ||
if (s32_Count >= 3 && u8_Buf[0] == 0xEF && u8_Buf[1] == 0xBB && u8_Buf[2] == 0xBF) { | ||
result = Encoding.UTF8; | ||
} | ||
if (s32_Count >= 4 && u8_Buf[0] == 0 && u8_Buf[1] == 0 && u8_Buf[2] == 0xFE && u8_Buf[3] == 0xFF) { | ||
result = new UTF32Encoding(true, true); | ||
} | ||
// проверка по коду 0xD0 | ||
double res= 0.0; double p = 0.0 ; | ||
for (int i=0; i<u8_Buf.Length; i++) { | ||
if (u8_Buf[i] == 0xD0 ) res++; | ||
} | ||
p = res/u8_Buf.Length*100 ; | ||
if ( p > 5 ) { // при ниличии символа 0xD0 больше чем 5%, можно предположить что кодировка UTF8 | ||
result = Encoding.UTF8; | ||
} | ||
///////////////////////////// | ||
} | ||
fstream.Close(); | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
// В кубике | ||
|
||
string path = project.Directory + @"\" + project.Variables["input"].Value ; // путь к файлу | ||
Encoding ecn1 = Analizing.EncodingTest.DetectEncoding(path); // попытка определить кодировку | ||
project.SendInfoToLog("Определили кодировку как : " + ecn1.ToString()); // вывод кодировки в PM | ||
|
||
var t = File.ReadAllText(path, ecn1); // читаем весь файл | ||
project.Variables["res"].Value = t; // ложим в переменную |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не помешает ссылка на форум, откуда это было взято
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
И код отформатировать не помешает, там какая-то каша местами (где фигурные скобки особенно)