| 11 | const unsigned* INVALID_LINES_END = INVALID_LINES + sizeof(INVALID_LINES)/sizeof(unsigned); |
| 12 | |
| 13 | int main(int argc, char** argv) |
| 14 | { |
| 15 | string test_file_path; |
| 16 | if (argc == 2) |
| 17 | test_file_path = argv[1]; |
| 18 | else { |
| 19 | cout << "Wrong number of arguments" << endl; |
| 20 | return 1; |
| 21 | } |
| 22 | // Open the test file |
| 23 | ifstream fs8(test_file_path.c_str()); |
| 24 | if (!fs8.is_open()) { |
| 25 | cout << "Could not open " << test_file_path << endl; |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | // Read it line by line |
| 30 | unsigned int line_count = 0; |
| 31 | char byte; |
| 32 | while (!fs8.eof()) { |
| 33 | string line; |
| 34 | while ((byte = static_cast<char>(fs8.get())) != '\n' && !fs8.eof()) |
| 35 | line.push_back(byte); |
| 36 | |
| 37 | line_count++; |
| 38 | bool expected_valid = (find(INVALID_LINES, INVALID_LINES_END, line_count) == INVALID_LINES_END); |
| 39 | // Print out lines that contain unexpected invalid UTF-8 |
| 40 | if (!is_valid(line.begin(), line.end())) { |
| 41 | if (expected_valid) { |
| 42 | cout << "Unexpected invalid utf-8 at line " << line_count << '\n'; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | // try fixing it: |
| 47 | string fixed_line; |
| 48 | replace_invalid(line.begin(), line.end(), back_inserter(fixed_line)); |
| 49 | if (!is_valid(fixed_line.begin(), fixed_line.end())) { |
| 50 | cout << "replace_invalid() resulted in an invalid utf-8 at line " << line_count << '\n'; |
| 51 | return 1; |
| 52 | } |
| 53 | } |
| 54 | else if (!expected_valid) { |
| 55 | cout << "Invalid utf-8 NOT detected at line " << line_count << '\n'; |
| 56 | return 1; |
| 57 | } |
| 58 | } |
| 59 | } |