Skip to content

Instantly share code, notes, and snippets.

@Hadaward
Last active August 22, 2023 23:29
Show Gist options
  • Select an option

  • Save Hadaward/0733c1772aaf00f2c3c4b80cc172fd80 to your computer and use it in GitHub Desktop.

Select an option

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)
#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