smntc
an in-memory multimodal graph database
Loading...
Searching...
No Matches
Utf8.c
Go to the documentation of this file.
1#include <wchar.h>
2#include <locale.h>
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include "errors.h"
7
8#include <stdio.h>
9
10struct Utf8 {
11 char *locale;
12 char *oldLocale;
14 wchar_t *wcharBuffer;
15};
16
17struct Utf8 *Utf8_construct(char *locale, int *error)
18{
19 struct Utf8 *this = malloc(sizeof(struct Utf8));
20
21 if (0 == this) {
22 *error = ERROR_NO_MEMORY;
23 return 0;
24 }
25
26 size_t length = strlen(locale) + 1;
27
28 this->locale = malloc(sizeof(char) * length);
29
30 strcpy(this->locale, locale);
31
32 char *oldLocale = setlocale(LC_ALL, locale);
33
34 size_t oldLength = strlen(oldLocale) + 1;
35
36 this->oldLocale = malloc(sizeof(char) * oldLength);
37
38 strcpy(this->oldLocale, oldLocale);
39
40 return this;
41}
42
43struct Utf8 *Utf8_destruct(struct Utf8 *this)
44{
45 if (0 != this) {
46 setlocale(LC_ALL, this->oldLocale);
47 free(this->oldLocale);
48 free(this->locale);
49 free(this);
50 }
51
52 return 0;
53}
54
55char *Utf8_encode(struct Utf8 *this, const wchar_t* wchars, int *error)
56{
57 size_t wcharLength = wcslen(wchars);
58 size_t charsLength = wcharLength * 4; // enough, but more than nessesary
59
60 char *chars = malloc(sizeof(char) * charsLength);
61
62 if (0 == chars) {
63 *error = ERROR_NO_MEMORY;
64 return 0;
65 }
66
67 mbstate_t convertState;
68 memset(&convertState, 0, sizeof(convertState));
69
70 wcsrtombs(chars, &wchars, charsLength, &convertState);
71
72 if (EILSEQ == errno) {
74 return 0;
75 }
76
77 return chars;
78}
79
80wchar_t *Utf8_decode(struct Utf8 *this, const char *chars, int *error)
81{
82 size_t charsLength = strlen(chars) + 1;
83
84 size_t wcharLength = charsLength; // enough, but more than nessesary
85
86 wchar_t *wchars = malloc(sizeof(wchar_t) * wcharLength);
87
88 if (0 == wchars) {
89 *error = ERROR_NO_MEMORY;
90 return 0;
91 }
92
93 mbstate_t convertState;
94 memset(&convertState, 0, sizeof(convertState));
95
96 mbsrtowcs(wchars, &chars, wcharLength, &convertState);
97
98 if (EILSEQ == errno || EINVAL == errno) {
100 return 0;
101 }
102
103 return wchars;
104}
105
106size_t Utf8_countCodes(struct Utf8 *this, const char *chars, int *error)
107{
108 size_t charsLength = strlen(chars) + 1;
109
110 size_t wcharLength = charsLength; // enough, but more than nessesary
111
112 mbstate_t state;
113 memset(&state, 0, sizeof(state));
114
115 wchar_t *wchars = 0;
116
117 size_t count = mbsrtowcs(wchars, &chars, wcharLength, &state);
118
119 if (EILSEQ == errno || EINVAL == errno) {
121 return 0;
122 }
123
124 return count;
125}
struct Utf8 * Utf8_construct(char *locale, int *error)
Definition Utf8.c:17
struct Utf8 * Utf8_destruct(struct Utf8 *this)
Definition Utf8.c:43
size_t Utf8_countCodes(struct Utf8 *this, const char *chars, int *error)
Definition Utf8.c:106
char * Utf8_encode(struct Utf8 *this, const wchar_t *wchars, int *error)
Definition Utf8.c:55
wchar_t * Utf8_decode(struct Utf8 *this, const char *chars, int *error)
Definition Utf8.c:80
#define ERROR_STRING_NOT_CONVERTED
Definition errors.h:12
#define ERROR_NO_MEMORY
Definition errors.h:5
Definition Utf8.c:10
char * oldLocale
Definition Utf8.c:12
wchar_t * wcharBuffer
Definition Utf8.c:14
char * locale
Definition Utf8.c:11
size_t wcharBufferLength
Definition Utf8.c:13