diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 03:26:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 03:26:42 +0000 |
commit | daf0485ec77463dbaeba3b1b0ffeefc8a89f5399 (patch) | |
tree | 1542cc4a202acd6b3e3b7c1729ede0e94750b691 /tests/src/compare-files.cpp | |
parent | Initial commit. (diff) | |
download | sexpp-daf0485ec77463dbaeba3b1b0ffeefc8a89f5399.tar.xz sexpp-daf0485ec77463dbaeba3b1b0ffeefc8a89f5399.zip |
Adding upstream version 0.8.7.upstream/0.8.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/src/compare-files.cpp')
-rw-r--r-- | tests/src/compare-files.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/tests/src/compare-files.cpp b/tests/src/compare-files.cpp new file mode 100644 index 0000000..49caec5 --- /dev/null +++ b/tests/src/compare-files.cpp @@ -0,0 +1,126 @@ +/** + * + * Copyright 2021-2023 Ribose Inc. (https://www.ribose.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include <algorithm> +#include <fstream> + +#include "sexp-tests.h" + +bool compare_binary_files(const std::string &filename1, const std::string &filename2) +{ + bool res = false; + std::ifstream file1(filename1, std::ifstream::ate | std::ifstream::binary); + std::ifstream file2(filename2, std::ifstream::ate | std::ifstream::binary); + + if (file1.tellg() == file2.tellg()) { // otherwise different file size + file1.seekg(0); + file2.seekg(0); + + std::istreambuf_iterator<char> begin1(file1); + std::istreambuf_iterator<char> begin2(file2); + + res = std::equal(begin1, std::istreambuf_iterator<char>(), begin2); + } + + return res; +} + +bool compare_binary_files(const std::string &filename1, std::istream &file2) +{ + std::ifstream file1(filename1, std::ifstream::binary); + + std::istreambuf_iterator<char> begin1(file1); + std::istreambuf_iterator<char> begin2(file2); + return std::equal(begin1, std::istreambuf_iterator<char>(), begin2); +} + +std::istream &safe_get_line(std::istream &is, std::string &t) +{ + t.clear(); + + // The characters in the stream are read one-by-one using a std::streambuf. + // That is faster than reading them one-by-one using the std::istream. + // Code that uses streambuf this way must be guarded by a sentry object. + // The sentry object performs various tasks, + // such as thread synchronization and updating the stream state. + + std::istream::sentry se(is, true); + std::streambuf * sb = is.rdbuf(); + + for (;;) { + int c = sb->sbumpc(); + switch (c) { + case '\n': + return is; + case '\r': + if (sb->sgetc() == '\n') + sb->sbumpc(); + return is; + case std::streambuf::traits_type::eof(): + // Also handle the case when the last line has no line ending + if (t.empty()) + is.setstate(std::ios::eofbit); + return is; + default: + t += (char) c; + } + } +} + +bool compare_text_files(const std::string &filename1, const std::string &filename2) +{ + bool res = true; + std::ifstream file1(filename1, std::ifstream::binary); + std::ifstream file2(filename2, std::ifstream::binary); + std::string s1, s2; + + while (res) { + if (file1.eof() && file2.eof()) + break; + safe_get_line(file1, s1); + safe_get_line(file2, s2); + if (s1 != s2) + res = false; + } + + return res; +} + +bool compare_text_files(const std::string &filename1, std::istream &file2) +{ + bool res = true; + std::ifstream file1(filename1, std::ifstream::binary); + std::string s1, s2; + file2.seekg(0); + + while (res) { + if (file1.eof() && file2.eof()) + break; + safe_get_line(file1, s1); + safe_get_line(file2, s2); + if (s1 != s2) + res = false; + } + + return res; +} |