From 3ee79437fe1bf54999ebf97b13735bbe2e1497f9 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 9 Oct 2024 11:05:37 +0200 Subject: [PATCH] Total rewrite --- main.cpp | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/main.cpp b/main.cpp index 925420f..7c907d9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,35 +1,30 @@ -// -// main.cpp -// valid-utf8 -// -// Created by Bob Polis at 2019-09-21 -// Copyright (c) 2019 SwiftCoder. All rights reserved. -// - -// libcommon +#include #include - -// UNIX -#include -#include - -// C++ +#include #include +#include + using namespace std; -int main(int argc, const char * argv[]) { - int result = EXIT_SUCCESS; - try { - string text; - if (argc == 1) { - cin >> text; - } else { - text = sc::file_get_contents(argv[1]); - } - result = sc::is_valid_utf8(text) ? EXIT_SUCCESS : EXIT_FAILURE; - } catch (const exception& ex) { - cerr << basename(const_cast(argv[0])) << ": " << ex.what() << '\n'; - result = EXIT_FAILURE; +static int check_file(istream& file) { + string line; + while (getline(file, line)) { + if (!sc::is_valid_utf8(line)) return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +int main(int argc, char* argv[]) { + try { + if (argc == 1) { + return check_file(cin); + } else { + ifstream file {argv[1]}; + if (!file) throw runtime_error {"could not open file"}; + return check_file(file); + } + } catch (const exception& ex) { + cerr << "valid-utf8: " << ex.what() << '\n'; + return EXIT_FAILURE; } - return result; }