aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: f2a63c6db4022d42e7b34493fdcae27ee1ea61df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#if 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <locale.h>

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 <stdio.h>      /* printf */
#include <stdlib.h>     /* wcstombs, wchar_t(C) */
#include <locale.h>

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