• R/O
  • SSH
  • HTTPS

ktx: Commit


Commit MetaInfo

Revision30 (tree)
Zeit2018-05-26 21:56:20
Autorbananajinn

Log Message

ファイルの文字コード判別機能を追加

Ändern Zusammenfassung

Diff

--- trunk/ktx/Utils/FileEncodingDetector.cs (nonexistent)
+++ trunk/ktx/Utils/FileEncodingDetector.cs (revision 30)
@@ -0,0 +1,132 @@
1+/*
2+ * Copyright(C) Dreamgate co.,ltd. All rights reserved.
3+ */
4+using System;
5+using System.IO;
6+using System.Text;
7+
8+namespace ktox.Utils
9+{
10+ /// <summary>
11+ /// ファイルのエンコーディングを確認します。
12+ /// </summary>
13+ public static class FileEncodingDetector
14+ {
15+ public static Encoding GetEncoding(string path)
16+ {
17+ using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
18+ var buffer = new byte[1024];
19+ var len = fs.Read(buffer, 0, 4);
20+ Encoding result = GetEncodingFromBOM(buffer, len);
21+ if (result != null) {
22+ return result;
23+ }
24+ fs.Seek(0, SeekOrigin.Begin);
25+ for (var count = 0; count < 10; count++) {
26+ len = fs.Read(buffer, 0, buffer.Length);
27+ if (IsUTF8(buffer, len))
28+ return Encoding.UTF8;
29+ if (IsSJIS(buffer, len))
30+ return Encoding.GetEncoding(932);
31+ }
32+ }
33+ return null;
34+ }
35+
36+ private static Encoding GetEncodingFromBOM(byte[] buffer, int length)
37+ {
38+ var bytes = new byte[length];
39+ Array.Copy(buffer, bytes, length);
40+ if (EqualsBytes(bytes, 0xfe, 0xff))
41+ return Encoding.BigEndianUnicode;
42+ if (EqualsBytes(bytes, 0xff, 0xfe))
43+ return Encoding.Unicode;
44+ if (EqualsBytes(bytes, 0xef, 0xbb, 0xbf))
45+ return Encoding.UTF8;
46+ if (EqualsBytes(bytes, 0xff, 0xfe, 0x00, 0x00))
47+ return Encoding.UTF32;
48+ return null;
49+ }
50+
51+ private static bool EqualsBytes(byte[] a, params byte[] b)
52+ {
53+ if (a.Length == b.Length) {
54+ for (int i = 0; i < a.Length; i++) {
55+ if (a[i] != b[i])
56+ return false;
57+ }
58+ return true;
59+ }
60+ return false;
61+ }
62+
63+ private static bool IsUTF8(byte[] buffer, int length)
64+ {
65+ bool multibyte = false;
66+ for (int i = 0; i < length; i++) {
67+ var b = buffer[i];
68+ int charLen = 0;
69+ if ((b & 0x80) == 0) {
70+ /* 1 byte */
71+ continue;
72+ }
73+ if ((b & 0xe0) == 0xc0) {
74+ /* 2 bytes */
75+ charLen = 2;
76+ } else if ((b & 0xf0) == 0xe0) {
77+ /* 3 bytes */
78+ charLen = 3;
79+ continue;
80+ }
81+ if ((b & 0xf8) == 0xf0) {
82+ /* 4 bytes */
83+ charLen = 4;
84+ continue;
85+ }
86+ if (charLen == 0)
87+ return false;
88+ for (int j = 0; j < charLen && i + j < length; j++) {
89+ if (!Is2ndByteOfUTF8(buffer[i + j]))
90+ return false;
91+ }
92+ multibyte = true;
93+ i += charLen - 1;
94+ }
95+ return multibyte;
96+ }
97+
98+ private static bool IsSJIS(byte[] buffer, int length)
99+ {
100+ bool multibyte = false;
101+ for (int i = 0; i < length; i++) {
102+ var b = buffer[i];
103+ if ((b & 0x80) == 0) {
104+ /* 1 byte */
105+ continue;
106+ }
107+ if (!IsLeadByteOfSJIS(b))
108+ return false;
109+ if (i + 1 < length && !IsTrailByteOfSJIS(buffer[i + 1]))
110+ return false;
111+ multibyte = true;
112+ i++;
113+ }
114+ return multibyte;
115+ }
116+
117+ private static bool Is2ndByteOfUTF8(byte b)
118+ {
119+ return (b & 0xc0) == 0x80;
120+ }
121+
122+ private static bool IsLeadByteOfSJIS(byte b)
123+ {
124+ return (b >= 0x81 && b <= 0x9f) || (b >= 0xe0 && b <= 0xfc);
125+ }
126+
127+ private static bool IsTrailByteOfSJIS(byte b)
128+ {
129+ return (b >= 0x40 && b <= 0x7e) || (b >= 0x80 && b <= 0xfc);
130+ }
131+ }
132+}
--- trunk/ktx/Views/MainForm.cs (revision 29)
+++ trunk/ktx/Views/MainForm.cs (revision 30)
@@ -12,6 +12,7 @@
1212 using System.Text;
1313 using System.Windows.Forms;
1414 using ktox.Targets;
15+using ktox.Utils;
1516 using ktox.Views;
1617
1718 namespace ktox
@@ -269,6 +270,8 @@
269270
270271 void NewFileTail(string file, Encoding encoding = null)
271272 {
273+ if(encoding == null)
274+ encoding = FileEncodingDetector.GetEncoding(file) ?? Encoding.UTF8;
272275 var form = GetNewForm();
273276 form.FileEncoding = encoding;
274277 form.Path = file;
@@ -286,7 +289,7 @@
286289 bool shiftKey = (e.KeyState & 0x04) != 0;
287290 var files = (string[])e.Data.GetData(DataFormats.FileDrop);
288291 foreach(var file in files){
289- NewFileTail(file, shiftKey ? Encoding.GetEncoding(932) : Encoding.UTF8);
292+ NewFileTail(file, shiftKey ? Encoding.GetEncoding(932) : null);
290293 }
291294 }
292295 }
Show on old repository browser