Total rewrite
This commit is contained in:
parent
9df8373b94
commit
3ee79437fe
43
main.cpp
43
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 <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;
|
||||
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 {
|
||||
string text;
|
||||
if (argc == 1) {
|
||||
cin >> text;
|
||||
return check_file(cin);
|
||||
} else {
|
||||
text = sc::file_get_contents(argv[1]);
|
||||
ifstream file {argv[1]};
|
||||
if (!file) throw runtime_error {"could not open file"};
|
||||
return check_file(file);
|
||||
}
|
||||
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;
|
||||
cerr << "valid-utf8: " << ex.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user