Skip to content

Commit 9dd06e5

Browse files
committed
✨ proper error examples for both the registry and the normal functions
1 parent e719ef1 commit 9dd06e5

File tree

5 files changed

+320
-27
lines changed

5 files changed

+320
-27
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// ============================================================================
2+
//
3+
// ztd.cuneicode
4+
// Copyright © JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
5+
// Contact: [email protected]
6+
//
7+
// Commercial License Usage
8+
// Licensees holding valid commercial ztd.cuneicode licenses may use this file
9+
// in accordance with the commercial license agreement provided with the
10+
// Software or, alternatively, in accordance with the terms contained in
11+
// a written agreement between you and Shepherd's Oasis, LLC.
12+
// For licensing terms and conditions see your agreement. For
13+
// further information contact [email protected].
14+
//
15+
// Apache License Version 2 Usage
16+
// Alternatively, this file may be used under the terms of Apache License
17+
// Version 2.0 (the "License"); you may not use this file except in compliance
18+
// with the License. You may obtain a copy of the License at
19+
//
20+
// https://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
//
28+
// ========================================================================= //
29+
30+
#include <ztd/cuneicode.h>
31+
#include <ztd/cuneicode/io.h>
32+
33+
#include <ztd/idk/size.h>
34+
35+
#include <stddef.h>
36+
#include <stdio.h>
37+
#include <stdbool.h>
38+
#include <string.h>
39+
40+
int main(int argc, char* argv[]) {
41+
(void)argc;
42+
(void)argv;
43+
44+
const char input_data[]
45+
= "\x61\x6c\x6c\x20\x61\x63\x63\x6f\x72\x64\x69\x6e\x67\x20\x74\x6f\x20"
46+
"\x82\xAF\x82\xA2\x82\xA9\x82\xAD\x2c"
47+
// errors
48+
"\xFF\xFF"
49+
// normal text again
50+
"\x20\x75\x66\x75\x66\x75\x66\x75\x21";
51+
// Manually inserted "\xFF" are errors in Shift-JIS!
52+
const size_t error_location_index = 25;
53+
ztd_char32_t output_data[ztdc_c_array_size(input_data)] = { 0 };
54+
55+
const size_t starting_input_size = ztdc_c_string_array_byte_size(input_data);
56+
size_t input_size = starting_input_size;
57+
const char* input = &input_data[0];
58+
const size_t starting_output_size = ztdc_c_array_byte_size(output_data);
59+
size_t output_size = starting_output_size;
60+
ztd_char32_t* output = &output_data[0];
61+
cnc_mcerr err = cnc_mcsntoc32sn_shift_jis_x0208(
62+
&output_size, &output, &input_size, &input);
63+
const bool has_err = err != cnc_mcerr_ok;
64+
const size_t input_read = starting_input_size - input_size;
65+
const size_t output_written = starting_output_size - output_size;
66+
const char* const conversion_result_title_str = (has_err
67+
? "Conversion failed!!! \xF0\x9F\x8E\x89 (expected)" // UTF-8 bytes
68+
// for 😭
69+
: "Conversion succeeded \xF0\x9F\x98\xAD" // UTF-8 bytes for 🎉
70+
"(NOT expected: this is bad!!)");
71+
const size_t conversion_result_title_str_size
72+
= strlen(conversion_result_title_str);
73+
// Use fwrite to prevent conversions / locale-sensitive-probing from
74+
// fprintf family of functions
75+
fwrite(conversion_result_title_str, sizeof(*conversion_result_title_str),
76+
conversion_result_title_str_size, has_err ? stdout : stderr);
77+
fprintf(has_err ? stdout : stderr,
78+
"\n\tRead: %zu %zu-bit elements"
79+
"\n\tWrote: %zu %zu-bit elements"
80+
"\n\tError happened at: data index %zu (expected %zu %s)"
81+
"\n\tThe bad byte was: %x index\n",
82+
(size_t)(input_read), (size_t)(sizeof(*input) * CHAR_BIT),
83+
(size_t)(output_written), (size_t)(sizeof(*output) * CHAR_BIT),
84+
(size_t)(input_read - 1), (size_t)(error_location_index),
85+
(error_location_index == input_read - 1 ? "\xe2\x9c\x85"
86+
: "\xe2\x9d\x8e"),
87+
(unsigned int)((unsigned char)input_data[input_read]));
88+
fprintf(stdout, "%s Conversion Result:\n", has_err ? "Partial" : "Complete");
89+
cnc_print_str_c32n(output_written, output_data);
90+
// the stream (may be) line-buffered, so make sure an extra "\n" is written
91+
// out this is actually critical for some forms of stdout/stderr mirrors; they
92+
// won't show the last line even if you manually call fflush(…) !
93+
fprintf(stdout, "\n");
94+
95+
return has_err ? 0 : 1;
96+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// ============================================================================
2+
//
3+
// ztd.cuneicode
4+
// Copyright © JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
5+
// Contact: [email protected]
6+
//
7+
// Commercial License Usage
8+
// Licensees holding valid commercial ztd.cuneicode licenses may use this file
9+
// in accordance with the commercial license agreement provided with the
10+
// Software or, alternatively, in accordance with the terms contained in
11+
// a written agreement between you and Shepherd's Oasis, LLC.
12+
// For licensing terms and conditions see your agreement. For
13+
// further information contact [email protected].
14+
//
15+
// Apache License Version 2 Usage
16+
// Alternatively, this file may be used under the terms of Apache License
17+
// Version 2.0 (the "License"); you may not use this file except in compliance
18+
// with the License. You may obtain a copy of the License at
19+
//
20+
// https://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
//
28+
// ========================================================================= //
29+
30+
#include <ztd/cuneicode.h>
31+
#include <ztd/cuneicode/io.h>
32+
33+
#include <ztd/idk/size.h>
34+
35+
#include <stddef.h>
36+
#include <stdio.h>
37+
#include <stdbool.h>
38+
#include <string.h>
39+
40+
int main(int argc, char* argv[]) {
41+
(void)argc;
42+
(void)argv;
43+
44+
cnc_conversion_registry* registry = NULL;
45+
{
46+
cnc_open_err err
47+
= cnc_registry_new(&registry, cnc_registry_options_default);
48+
if (err != cnc_open_err_ok) {
49+
fprintf(stderr, "[error] could not open a new registry conversion");
50+
return 1;
51+
}
52+
}
53+
54+
cnc_conversion* conversion = NULL;
55+
cnc_conversion_info conversion_info = { 0 };
56+
{
57+
cnc_open_err err = cnc_conv_new(
58+
registry, "shift-jis", "utf-8", &conversion, &conversion_info);
59+
if (err != cnc_open_err_ok) {
60+
fprintf(stderr, "[error] could not open a new registry conversion");
61+
cnc_registry_delete(registry);
62+
return 2;
63+
}
64+
}
65+
66+
fprintf(stdout, "Opened a conversion from \"");
67+
cnc_fprint_str_c8n(
68+
stdout, conversion_info.from_code_size, conversion_info.from_code_data);
69+
fprintf(stdout, "\" to \"");
70+
cnc_fprint_str_c8n(
71+
stdout, conversion_info.to_code_size, conversion_info.to_code_data);
72+
if (conversion_info.is_indirect) {
73+
fprintf(stdout, "\" (through \"");
74+
cnc_fprint_str_c8n(stdout, conversion_info.indirect_code_size,
75+
conversion_info.indirect_code_data);
76+
fprintf(stdout, "\")");
77+
}
78+
else {
79+
fprintf(stdout, "\"");
80+
}
81+
fprintf(stdout, "\n");
82+
83+
const char input_data[]
84+
= "\x61\x6c\x6c\x20\x61\x63\x63\x6f\x72\x64\x69\x6e\x67\x20\x74\x6f\x20"
85+
"\x82\xAF\x82\xA2\x82\xA9\x82\xAD\x2c"
86+
// errors
87+
"\xFF\xFF"
88+
// normal text again
89+
"\x20\x75\x66\x75\x66\x75\x66\x75\x21";
90+
// Manually inserted "\xFF" are errors in Shift-JIS!
91+
const size_t error_location_index = 25;
92+
unsigned char output_data[ztdc_c_array_size(input_data) * 2] = { 0 };
93+
94+
const size_t starting_input_size = ztdc_c_string_array_byte_size(input_data);
95+
size_t input_size = starting_input_size;
96+
const unsigned char* input = (const unsigned char*)&input_data[0];
97+
const size_t starting_output_size = ztdc_c_array_byte_size(output_data);
98+
size_t output_size = starting_output_size;
99+
unsigned char* output = (unsigned char*)&output_data[0];
100+
cnc_mcerr err
101+
= cnc_conv(conversion, &output_size, &output, &input_size, &input);
102+
const bool has_err = err != cnc_mcerr_ok;
103+
const size_t input_read = starting_input_size - input_size;
104+
const size_t output_written = starting_output_size - output_size;
105+
const char* const conversion_result_title_str = (has_err
106+
? "Conversion failed!!! \xF0\x9F\x8E\x89 (expected)" // UTF-8 bytes
107+
// for 😭
108+
: "Conversion succeeded \xF0\x9F\x98\xAD" // UTF-8 bytes for 🎉
109+
"(NOT expected: this is bad!!)");
110+
const size_t conversion_result_title_str_size
111+
= strlen(conversion_result_title_str);
112+
// Use fwrite to prevent conversions / locale-sensitive-probing from
113+
// fprintf family of functions
114+
fwrite(conversion_result_title_str, sizeof(*conversion_result_title_str),
115+
conversion_result_title_str_size, has_err ? stdout : stderr);
116+
fprintf(has_err ? stdout : stderr,
117+
"\n\tRead: %zu %zu-bit elements"
118+
"\n\tWrote: %zu %zu-bit elements"
119+
"\n\tError happened at: data index %zu (expected %zu %s)"
120+
"\n\tThe bad byte was: %x index\n",
121+
(size_t)(input_read), (size_t)(sizeof(*input) * CHAR_BIT),
122+
(size_t)(output_written), (size_t)(sizeof(*output) * CHAR_BIT),
123+
(size_t)(input_read - 1), (size_t)(error_location_index),
124+
(error_location_index == input_read - 1 ? "\xe2\x9c\x85"
125+
: "\xe2\x9d\x8e"),
126+
(unsigned int)((unsigned char)input_data[input_read]));
127+
fprintf(stdout, "%s Conversion Result:\n", has_err ? "Partial" : "Complete");
128+
cnc_print_str_c8n(output_written, output_data);
129+
// the stream (may be) line-buffered, so make sure an extra "\n" is written
130+
// out this is actually critical for some forms of stdout/stderr mirrors; they
131+
// won't show the last line even if you manually call fflush(…) !
132+
fprintf(stdout, "\n");
133+
134+
// clean up resources
135+
cnc_conv_delete(conversion);
136+
cnc_registry_delete(registry);
137+
return has_err ? 0 : 1;
138+
}

examples/basic/source/registry_windows_code_page.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ int main(int argc, char* argv[]) {
106106
const size_t failed_conversion_result_title_str_size
107107
= strlen(failed_conversion_result_title_str);
108108

109-
const ztd_char8_t input_data[]
110-
= u8"ଜାତି, ଭାଷା ଓ ସାହିତ୍ୟର ପରିଚୟ ଏବଂ ସ୍ୱାଭିମାନ ଏକ ସଂଗେ ଯୋଡା";
109+
const ztd_char32_t input_data[] = U"ଜତି, ଭଷ ଓ ସାହିତ୍ୟର ପରିଚୟ ଏବଂ ଏକ ସଂଗେ ଯୋଡ";
111110

112111
char intermediate_data[ztdc_c_array_size(input_data) * CNC_MC_MAX] = { 0 };
113112
const size_t starting_input_byte_size = sizeof(input_data);
@@ -206,7 +205,7 @@ int main(int argc, char* argv[]) {
206205
printf("Output conversion result:\n");
207206
cnc_print_str_c8n(output_bytes_written / sizeof(*output_data), output_data);
208207
printf("\nOriginal Input:\n");
209-
cnc_print_str_c8n(ztdc_c_array_size(input_data), input_data);
208+
cnc_print_str_c8n(ztdc_c_string_array_size(input_data), input_data);
210209
printf("\n\n");
211210

212211
cnc_conv_delete(conversion);

examples/basic/source/windows_code_page.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ int main(int argc, char* argv[]) {
5353
= strlen(failed_conversion_result_title_str);
5454
(void)argc;
5555
(void)argv;
56-
const ztd_char32_t input_data[]
57-
= U"ଜାତି, ଭାଷା ଓ ସାହିତ୍ୟର ପରିଚୟ ଏବଂ ସ୍ୱାଭିମାନ ଏକ ସଂଗେ ଯୋଡା";
58-
const uint32_t win32_odia_code_page = 57007u;
59-
char intermediate_data[ztdc_c_array_size(input_data) * CNC_MC_MAX] = { 0 };
60-
cnc_mcstate_t intermediate_state = { 0 };
56+
const ztd_char32_t input_data[] = U"ଜତି, ଭଷ ଓ ସାହିତ୍ୟର ପରିଚୟ ଏବଂ ଏକ ସଂଗେ ଯୋଡ";
57+
const uint32_t win32_odia_code_page = 57007u;
6158

59+
cnc_mcstate_t intermediate_state = { 0 };
6260
cnc_mcstate_set_win32_code_page(&intermediate_state, win32_odia_code_page);
61+
6362
const size_t starting_input_size = ztdc_c_string_array_size(input_data);
6463
size_t input_size = starting_input_size;
6564
const ztd_char32_t* input = input_data;
65+
66+
char intermediate_data[ztdc_c_array_size(input_data) * CNC_MC_MAX] = { 0 };
6667
const size_t starting_intermediate_size
6768
= ztdc_c_array_size(intermediate_data);
6869
size_t intermediate_size = starting_intermediate_size;
@@ -146,7 +147,7 @@ int main(int argc, char* argv[]) {
146147
printf("Output conversion result:\n");
147148
cnc_print_str_c32n(output_written, output_data);
148149
printf("\nOriginal Input:\n");
149-
cnc_print_str_c32n(ztdc_c_array_size(input_data), input_data);
150+
cnc_print_str_c32n(ztdc_c_string_array_size(input_data), input_data);
150151
printf("\n\n");
151152

152153
return 0;

0 commit comments

Comments
 (0)