This little function allows the script to print a string to the screen
| 488 | |
| 489 | // This little function allows the script to print a string to the screen |
| 490 | void PrintString(const string &str) |
| 491 | { |
| 492 | #ifdef _WIN32 |
| 493 | // Unless the std out has been redirected to file we'll need to allow Windows to convert |
| 494 | // the text to the current locale so that characters will be displayed appropriately. |
| 495 | HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); |
| 496 | DWORD mode = 0; |
| 497 | if( console != INVALID_HANDLE_VALUE && GetConsoleMode(console, &mode) != 0 ) |
| 498 | { |
| 499 | // We're writing to a console window, so convert the UTF8 string to UTF16 and write with |
| 500 | // WriteConsoleW. Windows will then automatically display the characters correctly according |
| 501 | // to the user's settings |
| 502 | // TODO: buffer size needs to be dynamic to handle large strings |
| 503 | // must split the string correctly between UTF8 unicode sequences |
| 504 | wchar_t bufUTF16[100000]; |
| 505 | MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, bufUTF16, 100000); |
| 506 | WriteConsoleW(console, bufUTF16, lstrlenW(bufUTF16), 0, 0); |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | // We're writing to a file, so just write the bytes as-is without any conversion |
| 511 | cout << str; |
| 512 | } |
| 513 | #else |
| 514 | cout << str; |
| 515 | #endif |
| 516 | } |
| 517 | |
| 518 | // Retrieve a line from stdin |
| 519 | string GetInput() |