valid-utf8/main.cpp

31 lines
713 B
C++
Raw Normal View History

2024-10-09 11:05:37 +02:00
#include <cstdlib>
2020-04-12 22:33:47 +02:00
#include <libscstring.hpp>
2024-10-09 11:05:37 +02:00
#include <stdexcept>
2019-09-21 14:04:05 +02:00
#include <iostream>
2024-10-09 11:05:37 +02:00
#include <fstream>
2019-09-21 14:04:05 +02:00
using namespace std;
2024-10-09 11:05:37 +02:00
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[]) {
2019-09-21 14:04:05 +02:00
try {
2024-10-09 11:05:37 +02:00
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);
}
2019-09-21 14:04:05 +02:00
} catch (const exception& ex) {
2024-10-09 11:05:37 +02:00
cerr << "valid-utf8: " << ex.what() << '\n';
return EXIT_FAILURE;
2019-09-21 14:04:05 +02:00
}
}