icinga2/samples/docsample.cpp
Alexander A. Klimov 499abec779 Squashed 'third-party/utf8cpp/' changes from 82344d605..79835a5fa
79835a5fa Remove -Wsign-conversion from test builds.
53398ca70 Restructure tests.
e3e57c811 Fix for issue #92: Clang 10 Compiler warnings
d736c29d3 Fix -Wconversion warnings in tests when -funsigned-char option is used.
780bd57d6 Merge pull request #96 from Finkman/provide-ns-on-installed
73b347f31 Adds the following advantages:  - utf8::cpp is available, even when installed  - cmake is aware of the library version  - compatible with previous usage: target utf8cpp available when installed
3ca904b21 Version 3.2.2
aa3b12c52 Added a couple of statis_casts to eliminate signed conversion warnings.
ee90a4a52 Added compiler options to enable warnings.
d8b92208f Merge pull request #87 from cschreib/patch-1
a92f85526 Add missing export() command
b85efd66a Test string literals
4e1149766 Merge pull request #80 from RT222/master
ec6747005 Fix the inclusion of both cpp11 and cpp17 headers on C++17 compilation
ddd38b304 Version 3.2
2af93bd67 Documentation: C++ 17 support.
b6cd18f92 test_utf32to8 tests string_view, not string
0991356b7 find_invalid(string_view) returns string_view::npos, not string::npos
4bed367c1 Update test docker image
a7d530de5 C++ 17 support: add tests, remove duplicate utf8::append()
50361edbf Merge pull request #77 from RT222/master
fe79d14ae Switching from gtest to ftest for unit-testing.
7d17aed95 Merge pull request #79 from fu7mu4/it-is-code
957e30052 Does it have syntax hilight
d40e4948f Support for C++17
e39907b38 Version 3.1.2
3c85cd48f Add prefix to NOEXCEPT and OVERRIDE macros.
944ef0561 Merge pull request #55 from Flamefire/disable_tests_for_subproject
dc2442c2e Merge pull request #61 from xenia-project/master
2b1521b48 Fix deprecated std::iterator usage (STL4015).
bee3b1349 Enable tests, docu, install only when building as root project
c3f9261eb Merge pull request #51 from AkiraMiyakoda/msvc_warning
7fb121130 Fix an MSVC warning about narrowing conversion.
170e2d11f Merge pull request #44 from tvanriper/patch-1
32ccbfb74 Removed utf8 path from #include.
2370b95a8 Compile negative test with C++ 98
89cfc3e38 Replace throw() with noexcept when using C++11 or later
687a59d88 Add override keyword when compiled with C++11 or later
f26d39e8a Move C++ version detection to core.h
b1002fd19 Introduce UTF_CPP_CPLUSPLUS macro.
088dd3ad7 Merge pull request #40 from xantares/patch-1
afc32b4c0 Always use GNU install layout except MSVC
82c674b7a Version 3.1
33516ff07 Test -fno-exceptions with unchecked api
f16bdde67 Merge pull request #37 from nemtrif/unchecked_replace_invalid
c52319302 unchecked::replace_invalid()
a3fa4ee84 Merge pull request #36 from nemtrif/gtesturl
8fd51836a Submodule url for googletest is https
ad27c7d5e Merge pull request #33 from nemtrif/optionalinstall
293f85d97 Installation is optional
a540297fd Version 3.0.3
04d29d92a Add inline declarations to non-template functions in cpp11.h
741259277 Merge pull request #30 from nemtrif/ci
335a3ee6a Update docker to 3.0.1
d5e847d0a Update project version
e6da1a36a Set up CircleCI
0b45681b6 Add Dockerfile
66804081d Introduce GTest
a0a2d9d1e Fix for the cpp11.h header guard
7db72814a Merge pull request #24 from nemtrif/release_3_0
007f8e32c advance() can work in backward direction.
92158c9ce Minor whitespace fixes
236d61942 Update documentation for replace_invalid
d8b09b301 Merge pull request #21 from glebm/fix-remove-invalid
3793703e7 Remove unused test data
080f1e6a3 Remove utf8reader sample
00d73e4a0 Remove deprecated functions
2b8596149 Support for C++11
f637e70c1 Merge pull request #22 from AkiraMiyakoda/fix-msvc2019-warning
cfc351c86 Fix MSVC 2017-2019 warning about negative unsigned value.
026aed63d replace_invalid: Replace final incomplete sequence

git-subtree-dir: third-party/utf8cpp
git-subtree-split: 79835a5fa57271f07a90ed36123e30ae9741178e
2023-07-03 17:20:34 +02:00

65 lines
2.1 KiB
C++

#include "../source/utf8.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2) {
cout << "\nUsage: docsample filename\n";
return 0;
}
const char* test_file_path = argv[1];
// Open the test file (must be UTF-8 encoded)
ifstream fs8(test_file_path);
if (!fs8.is_open()) {
cout << "Could not open " << test_file_path << endl;
return 0;
}
unsigned line_count = 1;
string line;
// Play with all the lines in the file
while (getline(fs8, line)) {
// check for invalid utf-8 (for a simple yes/no check, there is also utf8::is_valid function)
#if __cplusplus >= 201103L // C++ 11 or later
auto end_it = utf8::find_invalid(line.begin(), line.end());
#else
string::iterator end_it = utf8::find_invalid(line.begin(), line.end());
#endif // C++ 11
if (end_it != line.end()) {
cout << "Invalid UTF-8 encoding detected at line " << line_count << "\n";
cout << "This part is fine: " << string(line.begin(), end_it) << "\n";
}
// Get the line length (at least for the valid part)
ptrdiff_t length = utf8::distance(line.begin(), end_it);
cout << "Length of line " << line_count << " is " << length << "\n";
// Convert it to utf-16
#if __cplusplus >= 201103L // C++ 11 or later
u16string utf16line = utf8::utf8to16(line);
#else
vector<unsigned short> utf16line;
utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line));
#endif // C++ 11
// And back to utf-8;
#if __cplusplus >= 201103L // C++ 11 or later
string utf8line = utf8::utf16to8(utf16line);
#else
string utf8line;
utf8::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line));
#endif // C++ 11
// Confirm that the conversion went OK:
if (utf8line != string(line.begin(), end_it))
cout << "Error in UTF-16 conversion at line: " << line_count << "\n";
line_count++;
}
return 0;
}