Total rewrite

This commit is contained in:
Bob Polis 2024-10-09 11:05:37 +02:00
parent 9df8373b94
commit 3ee79437fe

View File

@ -1,35 +1,30 @@
// #include <cstdlib>
// main.cpp
// valid-utf8
//
// Created by Bob Polis at 2019-09-21
// Copyright (c) 2019 SwiftCoder. All rights reserved.
//
// libcommon
#include <libscstring.hpp> #include <libscstring.hpp>
#include <stdexcept>
// UNIX
#include <unistd.h>
#include <libgen.h>
// C++
#include <iostream> #include <iostream>
#include <fstream>
using namespace std; using namespace std;
int main(int argc, const char * argv[]) { static int check_file(istream& file) {
int result = EXIT_SUCCESS; string line;
try { while (getline(file, line)) {
string text; if (!sc::is_valid_utf8(line)) return EXIT_FAILURE;
if (argc == 1) { }
cin >> text; return EXIT_SUCCESS;
} else { }
text = sc::file_get_contents(argv[1]);
} int main(int argc, char* argv[]) {
result = sc::is_valid_utf8(text) ? EXIT_SUCCESS : EXIT_FAILURE; try {
} catch (const exception& ex) { if (argc == 1) {
cerr << basename(const_cast<char*>(argv[0])) << ": " << ex.what() << '\n'; return check_file(cin);
result = EXIT_FAILURE; } 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;
} }