From 817aa55011d9d685930fd59a42df807d8ba9b2c2 Mon Sep 17 00:00:00 2001 From: Clecio Jung Date: Sun, 16 Apr 2023 10:05:54 -0300 Subject: Simplifying the usage of the API --- main.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..f2a63c6 --- /dev/null +++ b/main.c @@ -0,0 +1,114 @@ +#if 0 +#include +#include +#include + +size_t strlen_utf8(const char *str) { + size_t len = 0; + for (; *str; str++) { + if ((*str & 0xc0) != 0x80) { + len++; + } + } + return len; +} + +int main(void) { + // Read a encoded string from a file + FILE *fp = fopen("text.txt", "r,ccs=utf16le"); + if (fp == NULL) { + perror("Error opening file"); + return EXIT_FAILURE; + } + char str[4096]; + if (fgets(str, sizeof(str), fp) == NULL) { + perror("Error in gets"); + //return EXIT_FAILURE; + } + printf("strlen=%ld, strlen_utf8=%ld, str=%s\n", strlen(str), strlen_utf8(str), str); + return EXIT_SUCCESS; +} +#elif 1 + +#include +#include +#include +#include +#include + +int main() { + // Set locale to support UTF-16LE encoding + char *local = setlocale(LC_ALL, "en_US.UTF-16"); + if (local == NULL) { + perror("setlocale"); + return EXIT_FAILURE; + } + printf("locale: %s\n", local); + + FILE* file = fopen("text.txt", "rb"); + if (file == NULL) { + perror("fopen"); + return EXIT_FAILURE; + } + fseek(file, 0, SEEK_END); + int file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + // Allocate buffer for file contents + wchar_t* file_contents = malloc(file_size); + if (file_contents == NULL) { + perror("malloc"); + return EXIT_FAILURE; + } + + printf("file_size=%d\n", file_size); + // Read file contents + size_t bytes_read = fread(file_contents, 1, file_size, file); + printf("bytes_read=%ld\n", bytes_read); + if (bytes_read != file_size) { + printf("Error: Failed to read file\n"); + return 1; + } + + // Close file + fclose(file); + + // Convert wide string to multibyte string in UTF-8 encoding + int buffer_size = wcstombs(NULL, file_contents, 0) + 1; + printf("buffer_size=%d\n", buffer_size); + char* buffer = malloc(buffer_size); + + // Print multibyte string to console + printf("%s", buffer); + + // Free buffers + free(file_contents); + free(buffer); + + return 0; +} + +#else + +/* wcstombs example */ +#include /* printf */ +#include /* wcstombs, wchar_t(C) */ +#include + +int main() { + const wchar_t str[] = L"ç ~^ âãáà êẽéè õôóò"; + char buffer[32]; + int ret; + + setlocale(LC_ALL, ""); + + printf ("wchar_t string: %ls \n",str); + + ret = wcstombs ( buffer, str, sizeof(buffer) ); + if (ret==32) buffer[31]='\0'; + if (ret) printf ("multibyte string: %s \n",buffer); + + return 0; +} + +#endif \ No newline at end of file -- cgit v1.2.3