valid-utf8/main.cpp
2024-10-09 11:05:37 +02:00

31 lines
713 B
C++

#include <cstdlib>
#include <libscstring.hpp>
#include <stdexcept>
#include <iostream>
#include <fstream>
using namespace std;
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;
}
}