Last active
August 22, 2023 23:29
-
-
Save Hadaward/0733c1772aaf00f2c3c4b80cc172fd80 to your computer and use it in GitHub Desktop.
A unicode header to allow printing and scanning unicodes from terminal (Emojis may work but you need a terminal who can represent them)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef UNICODE_H | |
| #define UNICODE_H | |
| #include <locale.h> | |
| #include <wchar.h> | |
| #include <stdarg.h> | |
| #include <stdio.h> | |
| #include <wctype.h> | |
| #include <stdlib.h> | |
| #ifdef _WIN32 | |
| #include <fcntl.h> | |
| #include <io.h> | |
| _setmode(_fileno(stdin), _O_U16TEXT); | |
| _setmode(_fileno(stdout), _O_U16TEXT); | |
| #endif | |
| void uprintf(wchar_t *format, ...) { | |
| setlocale(LC_ALL, ""); | |
| va_list args; | |
| va_start(args, format); | |
| vwprintf(format, args); | |
| va_end(args); | |
| } | |
| void uscan(wchar_t *str) { | |
| setlocale(LC_ALL, ""); | |
| while (1) { | |
| wint_t chr = getwchar(); | |
| if (chr == '\n') | |
| break; | |
| // Transform wint_t to wchar_t array | |
| wchar_t buff[2]; | |
| swprintf(buff, sizeof(buff)/sizeof(buff[0]), L"%lc", chr); | |
| // concatenate buff to the end of str | |
| wcscat(str, buff); | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment