OSDN Git Service

Ver0.26
[gefu/Gefu.git] / global.cpp
1 #include "global.h"
2
3 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief appendActionShortcut
5 /// \param action   アクションオブジェクト
6 /// \param ks       キーシーケンス文字列
7 ///
8 /// アクションにショートカットキーを追加します。
9 ///
10 void appendActionShortcut(QAction *action, const QString &ks)
11 {
12     QList<QKeySequence> shortcuts = action->shortcuts();
13     shortcuts << QKeySequence(ks);
14     action->setShortcuts(shortcuts);
15 }
16
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \brief detectCode
19 /// \param bytes    バイト列
20 /// \return エンコード文字列を返します。
21 ///
22 std::string detectCode(const QByteArray &bytes)
23 {
24     const quint8 bEscape = 0x1B;
25     const quint8 bAt = 0x40;
26     const quint8 bDollar = 0x24;
27     const quint8 bAnd = 0x26;
28     const quint8 bOpen = 0x28;    //'('
29     const quint8 bB = 0x42;
30     const quint8 bD = 0x44;
31     const quint8 bJ = 0x4A;
32     const quint8 bI = 0x49;
33
34     int len = bytes.size();
35     quint8 b1, b2, b3, b4;
36
37     for (int i = 0; i < len; i++) {
38         b1 = bytes[i];
39         if (b1 <= 0x06 || b1 == 0x7F || b1 == 0xFF) {
40             //'binary'
41             return "Binary";
42         }
43     }
44
45     bool notJapanese = true;
46     for (int i = 0; i < len; i++) {
47         b1 = bytes[i];
48         if (b1 == bEscape || 0x80 <= b1) {
49             notJapanese = false;
50             break;
51         }
52     }
53     if (notJapanese) {
54         return "UTF-8";
55     }
56
57     for (int i = 0; i < len - 2; i++) {
58         b1 = bytes[i];
59         b2 = bytes[i + 1];
60         b3 = bytes[i + 2];
61
62         if (b1 == bEscape){
63             if ((b2 == bDollar && b3 == bAt) ||
64                 (b2 == bDollar && b3 == bB) ||
65                 (b2 == bOpen && (b3 == bB || b3 == bJ)) ||
66                 (b2 == bOpen && b3 == bI))
67             {
68                 return "ISO 2022-JP";
69             }
70             if (i < len - 3) {
71                 b4 = bytes[i + 3];
72                 if (b2 == bDollar && b3 == bOpen && b4 == bD) {
73                     return "ISO 2022-JP";
74                 }
75                 if (i < len - 5 &&
76                     b2 == bAnd && b3 == bAt && b4 == bEscape &&
77                     bytes[i + 4] == bDollar && bytes[i + 5] == bB)
78                 {
79                     return "ISO 2022-JP";
80                 }
81             }
82         }
83     }
84
85     int sjis = 0;
86     int euc = 0;
87     int utf8 = 0;
88     for (int i = 0; i < len - 1; i++) {
89         b1 = bytes[i];
90         b2 = bytes[i + 1];
91         if (((0x81 <= b1 && b1 <= 0x9F) || (0xE0 <= b1 && b1 <= 0xFC)) &&
92             ((0x40 <= b2 && b2 <= 0x7E) || (0x80 <= b2 && b2 <= 0xFC)))
93         {
94             sjis += 2;
95             i++;
96         }
97     }
98     for (int i = 0; i < len - 1; i++) {
99         b1 = bytes[i];
100         b2 = bytes[i + 1];
101         if (((0xA1 <= b1 && b1 <= 0xFE) && (0xA1 <= b2 && b2 <= 0xFE)) ||
102             (b1 == 0x8E && (0xA1 <= b2 && b2 <= 0xDF)))
103         {
104             euc += 2;
105             i++;
106         }
107         else if (i < len - 2) {
108             b3 = bytes[i + 2];
109             if (b1 == 0x8F && (0xA1 <= b2 && b2 <= 0xFE) &&
110                 (0xA1 <= b3 && b3 <= 0xFE))
111             {
112                 euc += 3;
113                 i += 2;
114             }
115         }
116     }
117     for (int i = 0; i < len - 1; i++) {
118         b1 = bytes[i];
119         b2 = bytes[i + 1];
120         if ((0xC0 <= b1 && b1 <= 0xDF) && (0x80 <= b2 && b2 <= 0xBF)) {
121             utf8 += 2;
122             i++;
123         }
124         else if (i < len - 2) {
125             b3 = bytes[i + 2];
126             if ((0xE0 <= b1 && b1 <= 0xEF) && (0x80 <= b2 && b2 <= 0xBF) &&
127                 (0x80 <= b3 && b3 <= 0xBF))
128             {
129                 utf8 += 3;
130                 i += 2;
131             }
132         }
133     }
134
135     if (euc > sjis && euc > utf8) {
136         return "EUC-JP";
137     }
138     else if (sjis > euc && sjis > utf8) {
139         return "Shift-JIS";
140     }
141     else if (utf8 > euc && utf8 > sjis) {
142         return "UTF-8";
143     }
144
145 #ifdef Q_OS_WIN
146     return "Shift-JIS";
147 #else
148     return "UTF-8";
149 #endif
150
151 }
152
153 ///////////////////////////////////////////////////////////////////////////////
154 /// \brief fileSizeToString
155 /// \param size サイズ
156 /// \return サイズの文字列表記を返します。
157 ///
158 QString fileSizeToString(qint64 size)
159 {
160     if (size >= 1024 * 1024 * 1024) {
161         return QString("%1GB").arg(int(10 * size / (1024 * 1024 * 1024)) / 10);
162     }
163     if (size >= 1024 * 1024) {
164         return QString("%1MB").arg(int(10 * size / (1024 * 1024)) / 10);
165     }
166     if (size >= 1024) {
167         return QString("%1KB").arg(int(10 * size / (1024)) / 10);
168     }
169     return QString("%1B").arg(size);
170 }
171
172 ///////////////////////////////////////////////////////////////////////////////
173 /// \brief reconnectAction
174 /// \param sender   送信元アクション
175 /// \param signal   シグナル
176 /// \param reciever 送信先オブジェクト
177 /// \param slot     スロット
178 ///
179 /// シグナルにスロットを再接続します。
180 ///
181 void reconnectAction(QAction *sender, const char *signal, QObject *reciever, const char *slot)
182 {
183     sender->setEnabled(true);
184     sender->disconnect();
185     QObject::connect(sender, signal, reciever, slot);
186 }