/****************************************************************************** * This program simply copies one file to another, line by line. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace Utils; using namespace std; void copy_text_file(wstring from_filespec, wstring to_filespec) { wifstream ifh(filesystem::path(from_filespec).c_str()); wofstream ofh(filesystem::path(to_filespec).c_str()); if (!ifh.good()) { throw ios_base::failure(Utils::wchar_to_UTF8(L"Problem opening input file!")); } if (!ofh.good()) { throw ios_base::failure(Utils::wchar_to_UTF8(L"Problem opening output file!")); } ifh.imbue(utf8loc); ofh.imbue(utf8loc); wstring line; while (getline(ifh, line)) { ofh << line << endl; } ifh.close(); ofh.close(); } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); if (argc != 3) { wcout << L"Syntax: " << Utils::UTF8_to_wstring(argv[0]) << L" {fromFilespec} {toFilespec}" << endl; exit(1); } wstring from_filespec = Utils::UTF8_to_wstring(argv[1]); wstring to_filespec = Utils::UTF8_to_wstring(argv[2]); try { copy_text_file(from_filespec, to_filespec); } catch (ios_base::failure ex) { wcout << L"Error: " << Utils::exceptionMessage(ex) << endl; } return 0; }