|
1 | 1 | /* |
2 | | - * |
3 | | - * Copyright (c) 2016-2023 |
| 2 | + * Copyright (c) 2016-2026 |
4 | 3 | * Viktor Gubin |
5 | 4 | * |
6 | 5 | * Use, modification and distribution are subject to the |
7 | 6 | * Boost Software License, Version 1.0. (See accompanying file |
8 | 7 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | 8 | */ |
10 | 9 |
|
11 | | -/* |
12 | | - * This example demonstrate IO library text API possibilities |
13 | | - * It works as POSIX utility cat https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/cat.html |
14 | | - * and in the same time auto-detect input file character set and convert it to system UNCODE before printing to console |
15 | | - */ |
16 | | - |
17 | 10 | #include <vector> |
18 | | - |
19 | 11 | #include <io/core/files.hpp> |
20 | 12 | #include <io/console/console.hpp> |
21 | | - |
22 | 13 | #include <io/textapi/charset_detector.hpp> |
23 | 14 | #include <io/textapi/charset_converter.hpp> |
24 | 15 |
|
25 | | -void print_error(io::console_error_stream& err,const char* reason, const char* f, const char* what); |
26 | | -void file_to_console(std::error_code& ec,io::file&& f, io::console_out_writer& out); |
| 16 | +/// Prints an error into console if any |
| 17 | +void print_error(io::console_error_stream& err, const char* reason, const char* f, const char* what); |
| 18 | +/// Prints a text file into console, detect character set automatically and reconvert into system UNICODE |
| 19 | +void file_to_console(std::error_code& ec, io::file&& f, io::console_out_writer& out); |
27 | 20 |
|
28 | 21 | int main(int argc, const char** argv) |
29 | 22 | { |
30 | 23 | int ret = ERROR_SUCCESS; |
31 | 24 |
|
32 | 25 | io::console cons; |
33 | 26 | io::console_error_stream cerr(cons); |
34 | | - |
35 | 27 | io::console_out_writer out(cons); |
36 | 28 |
|
37 | | - if(argc < 2) { |
38 | | - cerr << io::cclr::light_red << "No files to display." << io::cclr::reset << std::endl << std::endl; |
| 29 | + if (argc < 2) { |
| 30 | + cerr << io::cclr::light_red << "No files to display." << io::cclr::reset << std::endl << std::endl; |
39 | 31 | cerr << "\tCommand usage : "; |
40 | | - cerr << io::cclr::navy_green << "cat_ext " << io::cclr::reset; |
| 32 | + cerr << io::cclr::navy_green << "cat_ext " << io::cclr::reset; |
41 | 33 | cerr << '[' << io::cclr::magenta << "file..." << io::cclr::reset << ']' << std::endl; |
42 | | - ret = EINVAL; |
| 34 | + return EINVAL; |
43 | 35 | } |
44 | 36 |
|
45 | 37 | std::error_code ec; |
46 | | - for(std::size_t i =1; i < static_cast<std::size_t>(argc); i++) { |
| 38 | + for (std::size_t i = 1; i < static_cast<std::size_t>(argc); i++) { |
47 | 39 | io::file src_file(argv[i]); |
48 | | - if( !src_file.exist() ) { |
| 40 | + if (!src_file.exist()) { |
49 | 41 | ret = ENOENT; |
50 | | - print_error(cerr,"Requested",argv[i],"not found"); |
| 42 | + print_error(cerr, "Requested", argv[i], "not found"); |
51 | 43 | break; |
52 | 44 | } |
53 | | - file_to_console(ec,std::move(src_file), out); |
54 | | - if(ec) { |
| 45 | + |
| 46 | + file_to_console(ec, std::move(src_file), out); |
| 47 | + if (ec) { |
55 | 48 | ret = ec.value(); |
56 | | - print_error(cerr,"Issue with",argv[i], ec.message().data() ); |
| 49 | + print_error(cerr, "Issue with", argv[i], ec.message().data()); |
57 | 50 | break; |
58 | 51 | } |
59 | 52 | } |
60 | 53 |
|
61 | 54 | return ret; |
62 | 55 | } |
63 | 56 |
|
64 | | - |
65 | | -void print_error(io::console_error_stream& err,const char* reason, const char* f, const char* what) |
| 57 | +void print_error(io::console_error_stream& err, const char* reason, const char* f, const char* what) |
66 | 58 | { |
67 | | - err << io::cclr::light_red << "Error" << io::cclr::reset << ": "; |
| 59 | + err << io::cclr::light_red << "Error" << io::cclr::reset << ": "; |
68 | 60 | err << reason << " file "; |
69 | 61 | err << io::cclr::yellow << f << io::cclr::reset; |
70 | 62 | err << ' ' << what << io::cclr::reset << std::endl; |
71 | 63 | } |
72 | 64 |
|
73 | | -#ifdef __IO_WINDOWS_BACKEND__ |
74 | | -static const char* WENDL = "\r\n"; |
75 | | -#else |
76 | | -static const char* WENDL = "\n"; |
77 | | -#endif // __IO_WINDOWS_BACKEND__ |
78 | | - |
79 | | - |
80 | | -io::s_pump data_pump_for_charset(std::error_code& ec,const io::s_read_channel& rch, const io::charset* chst, io::byte_buffer&& buff) |
| 65 | +io::s_pump data_pump_for_charset(std::error_code& ec, const io::s_read_channel& rch, const io::charset* chst, io::byte_buffer&& buff) |
81 | 66 | { |
82 | | - io::s_pump ret; |
83 | | - if(io::code_pages::utf8() == chst) |
84 | | - ret = io::buffered_channel_pump::create(ec, rch, std::forward<io::byte_buffer>(buff) ); |
85 | | - else |
86 | | - ret = io::charset_converting_channel_pump::create(ec, rch, chst, io::code_pages::utf8(), std::forward<io::byte_buffer>(buff) ); |
87 | | - return ret; |
| 67 | + if (io::code_pages::utf8() == chst) { |
| 68 | + return io::buffered_channel_pump::create(ec, rch, std::move(buff)); |
| 69 | + } |
| 70 | + return io::charset_converting_channel_pump::create(ec, rch, chst, io::code_pages::utf8(), std::move(buff)); |
88 | 71 | } |
89 | 72 |
|
90 | | -void file_to_console(std::error_code& ec,io::file&& f, io::console_out_writer& out) |
| 73 | +void file_to_console(std::error_code& ec, io::file&& f, io::console_out_writer& out) |
91 | 74 | { |
92 | 75 | io::s_read_channel rch = f.open_for_read(ec); |
93 | | - if(!ec) { |
94 | | - // detect character set |
95 | | - io::byte_buffer buff = io::byte_buffer::allocate(ec, 1024); |
96 | | - if(!ec) { |
97 | | - std::size_t read = rch->read(ec, const_cast<uint8_t*>(buff.position().get()), buff.capacity()); |
98 | | - if(!ec && read > 0 ) { |
99 | | - io::s_charset_detector chdet = io::charset_detector::create(ec); |
100 | | - buff.move(read); |
101 | | - buff.flip(); |
102 | | - auto detect_status = chdet->detect( ec, buff.position().get(), read ); |
103 | | - if(!ec) { |
104 | | - if(!detect_status) { |
105 | | - ec = std::make_error_code(std::errc::illegal_byte_sequence); |
106 | | - } |
107 | | - else { |
108 | | - out.write("Detected : "); |
109 | | - out.write(detect_status.character_set()->name()); |
110 | | - out.write(WENDL); |
111 | | - io::s_pump pmp = data_pump_for_charset(ec, rch, detect_status.character_set(), std::move(buff) ); |
112 | | - if(!ec) { |
113 | | - io::reader in( std::move(pmp) ); |
114 | | - char tmp[1024] = {'\0'}; |
115 | | - std::size_t read = 0; |
116 | | - do { |
117 | | - read = in.read(ec, tmp, 1024); |
118 | | - if(read > 0 && !ec) |
119 | | - out.write(tmp, read); |
120 | | - } while(read > 0 && !ec); |
121 | | - } |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - } |
| 76 | + if (ec) |
| 77 | + return; |
| 78 | + |
| 79 | + io::byte_buffer buff = io::byte_buffer::allocate(ec, 1024); |
| 80 | + if (ec) |
| 81 | + return; |
| 82 | + |
| 83 | + // Read initial block to detect character set |
| 84 | + std::size_t bytes_read = rch->read(ec, const_cast<uint8_t*>(buff.position().get()), buff.capacity()); |
| 85 | + if (ec || bytes_read == 0) |
| 86 | + return; |
| 87 | + |
| 88 | + io::s_charset_detector chdet = io::charset_detector::create(ec); |
| 89 | + if (ec) |
| 90 | + return; |
| 91 | + |
| 92 | + buff.move(bytes_read); |
| 93 | + buff.flip(); |
| 94 | + |
| 95 | + auto detect_status = chdet->detect(ec, buff.position().get(), bytes_read); |
| 96 | + if (ec) |
| 97 | + return; |
| 98 | + |
| 99 | + if (!detect_status) { |
| 100 | + ec = std::make_error_code(std::errc::illegal_byte_sequence); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Print detected charset |
| 105 | + out.write("Detected : "); |
| 106 | + out.writeln(detect_status.character_set()->name()); |
| 107 | + |
| 108 | + io::s_pump pmp = data_pump_for_charset(ec, rch, detect_status.character_set(), std::move(buff)); |
| 109 | + if (ec) |
| 110 | + return; |
| 111 | + |
| 112 | + // Stream the converted content directly to the console |
| 113 | + io::reader in(std::move(pmp)); |
| 114 | + char tmp[1024]; |
| 115 | + while ((bytes_read = in.read(ec, tmp, sizeof(tmp))) > 0 && !ec) { |
| 116 | + out.write(tmp, bytes_read); |
126 | 117 | } |
127 | 118 | } |
0 commit comments