|
| 1 | +// ============================================================================ |
| 2 | +// |
| 3 | +// ztd.cuneicode |
| 4 | +// Copyright © JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC |
| 5 | + |
| 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(®istry, 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 | +} |
0 commit comments