diff options
author | stderr64 <stderr64@null.net> | 2024-11-02 23:11:00 +0200 |
---|---|---|
committer | stderr64 <stderr64@null.net> | 2024-11-02 23:11:00 +0200 |
commit | 23778581f374a3fbd266de4e854cffa3f11a4400 (patch) | |
tree | d57a8d896970abbed1fa24102cdce9b61cb58133 /all_unicode_chars/all_unicode_chars.c | |
parent | 142cc4551ebbb4f826f63cbc40df40e05497cca3 (diff) | |
download | experiments-23778581f374a3fbd266de4e854cffa3f11a4400.tar.gz experiments-23778581f374a3fbd266de4e854cffa3f11a4400.tar.zst |
Added unicode encoding example in C
Diffstat (limited to 'all_unicode_chars/all_unicode_chars.c')
-rw-r--r-- | all_unicode_chars/all_unicode_chars.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/all_unicode_chars/all_unicode_chars.c b/all_unicode_chars/all_unicode_chars.c new file mode 100644 index 0000000..33099b1 --- /dev/null +++ b/all_unicode_chars/all_unicode_chars.c @@ -0,0 +1,54 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <errno.h> +#include <locale.h> +#include <string.h> +#include <uchar.h> +#include <wchar.h> + +int main( int argc, char *args[] ){ + setlocale( LC_ALL, "" ); + fprintf( stdout, "Buffer size: %llu\n", (0xFFFF - (0x00A0 - 0x007F)) * 4 ); + uint16_t char_buffer[(0xFFFF - (0x00A0 - 0x007F)) * 4] = {0}; + memset( (void*)&char_buffer, 0, (0xFFFF - (0x00A0 - 0x007F)) * 4 ); + uint16_t current_char = 0x0020; + size_t char_bytes = 0; + char mbstr[sizeof(wchar_t)] = {0}; + memset( (void*)&mbstr, 0, sizeof(wchar_t) ); + size_t b_size = 0; + mbstate_t *m_state = NULL; + for ( ; current_char < 0xFFFF; current_char++ ){ + if ( current_char > 0x007F && current_char < 0x00A0 ) + current_char = 0x00A0; + if ( (b_size = wcrtomb((char*)&mbstr, (wchar_t)current_char, m_state)) == (size_t)-1 ) + continue; + wmemmove( (void*)&char_buffer[char_bytes], (const void*)&mbstr, b_size ); + memset( (void*)&mbstr, 0, b_size ); + char_bytes += b_size; + } + fprintf( stdout, "Copied %llu bytes into buffer\n", char_bytes ); + b_size = 0; + m_state = NULL; + memset( (void*)&mbstr, 0, sizeof(mbstr) ); + FILE *output = fopen( "unicode_chars.txt", "w" ); + if ( output == NULL ){ + fprintf( stderr, "Error: %s\n", strerror(errno) ); + exit( EXIT_FAILURE ); + } + ssize_t char_written = 0; + if ( (char_written = (ssize_t)fwrite((void*)&char_buffer, sizeof(uint16_t), char_bytes, output)) < 0 ){ + fclose( output ); + fprintf( stderr, "Error writing file: %s\n", strerror(errno) ); + exit( EXIT_FAILURE ); + } + fclose( output ); + fprintf( stdout, "%llu bytes written into unicode_chars.txt\n", char_written ); + memset( (void*)&char_buffer, 0, sizeof(char_buffer) ); + output = NULL; + current_char = 0x0020; + char_written = 0; + char_bytes = 0; + exit( EXIT_SUCCESS ); +} |