| 195 | } |
| 196 | |
| 197 | QByteArray DetectCode::uchardetCode(QString filePath) |
| 198 | { |
| 199 | FILE *fp; |
| 200 | QByteArray charset; |
| 201 | |
| 202 | size_t buffer_size = 0x10000; |
| 203 | char *buff = new char[buffer_size]; |
| 204 | memset(buff, 0, buffer_size); |
| 205 | |
| 206 | /* Analyze text encoding through sample characters */ |
| 207 | uchardet_t handle = uchardet_new(); |
| 208 | |
| 209 | /* Open the text file to be detected and read a certain number of sample characters */ |
| 210 | fp = fopen(filePath.toLocal8Bit().data(), "rb"); |
| 211 | |
| 212 | if (fp) { |
| 213 | while (!feof(fp)) { |
| 214 | size_t len = fread(buff, 1, buffer_size, fp); |
| 215 | int retval = uchardet_handle_data(handle, buff, len); |
| 216 | if (retval != 0) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | break; |
| 221 | } |
| 222 | fclose(fp); |
| 223 | |
| 224 | uchardet_data_end(handle); |
| 225 | charset = uchardet_get_charset(handle); |
| 226 | } |
| 227 | |
| 228 | uchardet_delete(handle); |
| 229 | delete[] buff; |
| 230 | buff = nullptr; |
| 231 | |
| 232 | if (charset == "MAC-CENTRALEUROPE") |
| 233 | charset = "MACCENTRALEUROPE"; |
| 234 | if (charset == "MAC-CYRILLIC") |
| 235 | charset = "MACCYRILLIC"; |
| 236 | if (charset.contains("WINDOWS-")) |
| 237 | charset = charset.replace("WINDOWS-", "CP"); |
| 238 | return charset; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @author guoshao |