1616
1717#include < vector>
1818
19- #include < io/core/console.hpp>
2019#include < io/core/files.hpp>
20+ #include < io/console/console.hpp>
2121
2222#include < io/textapi/charset_detector.hpp>
2323#include < io/textapi/charset_converter.hpp>
@@ -27,47 +27,47 @@ void file_to_console(std::error_code& ec,io::file&& f, io::console_out_writer& o
2727
2828int main (int argc, const char ** argv)
2929{
30- int ret = ERROR_SUCCESS;
31-
32- io::console cons;
33- io::console_error_stream cerr (cons);
34-
35- io::console_out_writer out (cons);
36-
37- if (argc < 2 ) {
38- cerr << io::cclr::light_red << " No files to display." << io::cclr::reset << std::endl << std::endl;
39- cerr << " \t Command usage : " ;
40- cerr << io::cclr::navy_green << " cat_ext " << io::cclr::reset;
41- cerr << ' [' << io::cclr::magenta << " file..." << io::cclr::reset << ' ]' << std::endl;
42- ret = EINVAL;
43- }
44-
45- std::error_code ec;
46- for (std::size_t i =1 ; i < static_cast <std::size_t >(argc); i++) {
47- io::file src_file (argv[i]);
48- if ( !src_file.exist () ) {
49- ret = ENOENT;
50- print_error (cerr," Requested" ,argv[i]," not found" );
51- break ;
52- }
53- file_to_console (ec,std::move (src_file), out);
54- if (ec) {
55- ret = ec.value ();
56- print_error (cerr," Issue with" ,argv[i], ec.message ().data () );
57- break ;
58- }
59- }
60-
61- return ret;
30+ int ret = ERROR_SUCCESS;
31+
32+ io::console cons;
33+ io::console_error_stream cerr (cons);
34+
35+ io::console_out_writer out (cons);
36+
37+ if (argc < 2 ) {
38+ cerr << io::cclr::light_red << " No files to display." << io::cclr::reset << std::endl << std::endl;
39+ cerr << " \t Command usage : " ;
40+ cerr << io::cclr::navy_green << " cat_ext " << io::cclr::reset;
41+ cerr << ' [' << io::cclr::magenta << " file..." << io::cclr::reset << ' ]' << std::endl;
42+ ret = EINVAL;
43+ }
44+
45+ std::error_code ec;
46+ for (std::size_t i =1 ; i < static_cast <std::size_t >(argc); i++) {
47+ io::file src_file (argv[i]);
48+ if ( !src_file.exist () ) {
49+ ret = ENOENT;
50+ print_error (cerr," Requested" ,argv[i]," not found" );
51+ break ;
52+ }
53+ file_to_console (ec,std::move (src_file), out);
54+ if (ec) {
55+ ret = ec.value ();
56+ print_error (cerr," Issue with" ,argv[i], ec.message ().data () );
57+ break ;
58+ }
59+ }
60+
61+ return ret;
6262}
6363
6464
6565void print_error (io::console_error_stream& err,const char * reason, const char * f, const char * what)
6666{
67- err << io::cclr::light_red << " Error" << io::cclr::reset << " : " ;
68- err << reason << " file " ;
69- err << io::cclr::yellow << f << io::cclr::reset;
70- err << ' ' << what << io::cclr::reset << std::endl;
67+ err << io::cclr::light_red << " Error" << io::cclr::reset << " : " ;
68+ err << reason << " file " ;
69+ err << io::cclr::yellow << f << io::cclr::reset;
70+ err << ' ' << what << io::cclr::reset << std::endl;
7171}
7272
7373#ifdef __IO_WINDOWS_BACKEND__
@@ -76,42 +76,52 @@ static const char* WENDL = "\r\n";
7676static const char * WENDL = " \n " ;
7777#endif // __IO_WINDOWS_BACKEND__
7878
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)
81+ {
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;
88+ }
89+
7990void file_to_console (std::error_code& ec,io::file&& f, io::console_out_writer& out)
8091{
81- io::s_read_channel rch = f.open_for_read (ec);
82- if (!ec) {
83- io::s_charset_detector chdet = io::charset_detector::create (ec);
84- if (!ec) {
85- // detect character set
86- uint8_t buff[1024 ] = {0 };
87- std::size_t read = rch->read (ec, buff, 1024 );
88- if ( !ec && read > 0 ) {
89- auto detect_status = chdet->detect ( ec, buff, read );
90- if (!ec && !detect_status) {
91- ec = std::make_error_code (std::errc::illegal_byte_sequence);
92- }
93- else {
94- out.write (" Detected : " );
95- out.write (detect_status.character_set ()->name ());
96- out.write (WENDL);
97- if ( io::code_pages::utf8 ()->code () != detect_status.character_set ()->code () ) {
98-
99- }
100- else {
101- const char * it = reinterpret_cast <const char *>(buff);
102- if ( io::utf8_bom::is (it) ) {
103- it += io::utf8_bom::len ();
104- read -= io::utf8_bom::len ();
105- }
106- do {
107- out.write (it, read);
108- out.flush ();
109- read = rch->read (ec, buff, 1024 );
110- it = reinterpret_cast <const char *>(buff);
111- } while ( read && !ec);
112- }
113- }
114- }
115- }
116- }
92+ 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+ }
126+ }
117127}
0 commit comments