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 @@
//
// main.cpp
// valid-utf8
//
// Created by Bob Polis at 2019-09-21
// Copyright (c) 2019 SwiftCoder. All rights reserved.
//
// libcommon
#include <cstdlib>
#include <libscstring.hpp>
// UNIX
#include <unistd.h>
#include <libgen.h>
// C++
#include <stdexcept>
#include <iostream>
#include <fstream>
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<char*>(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;
}