diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/endian/example/conversion_use_case.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/endian/example/conversion_use_case.cpp')
-rw-r--r-- | src/boost/libs/endian/example/conversion_use_case.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/boost/libs/endian/example/conversion_use_case.cpp b/src/boost/libs/endian/example/conversion_use_case.cpp new file mode 100644 index 00000000..544b0c0b --- /dev/null +++ b/src/boost/libs/endian/example/conversion_use_case.cpp @@ -0,0 +1,51 @@ +// endian/example/conversion_use_case.cpp + +// Copyright Beman Dawes 2014 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// This program reads a binary file of fixed length records, with a format defined +// in a header file supplied by a third-party. The records inserted into a vector, +// and the vector is sorted. The sorted records are then written to an output file. + +// Full I/O error testing omitted for brevity, So don't try this at home. + +#include "third_party_format.hpp" +#include <boost/endian/conversion.hpp> +#include <vector> +#include <fstream> +#include <algorithm> +#include <iostream> + +using third_party::record; + +int main() +{ + std::ifstream in("data.bin", std::ios::binary); + if (!in) { std::cout << "Could not open data.bin\n"; return 1; } + + std::ofstream out("sorted-data.bin", std::ios::binary); + if (!out) { std::cout << "Could not open sorted-data.bin\n"; return 1; } + + record rec; + std::vector<record> recs; + + while (!in.eof()) // read each record + { + in.read((char*)&rec, sizeof(rec)); + rec.balance = boost::endian::big_to_native(rec.balance); // reverse if needed + recs.push_back(rec); + } + + std::sort(recs.begin(), recs.end(), // decending sort by balance + [](const record& lhs, const record& rhs) -> bool + { return lhs.balance > rhs.balance; }); + + for (auto &out_rec : recs) // write each record + { + out_rec.balance = boost::endian::native_to_big(out_rec.balance); // reverse if needed + out.write((const char*)&out_rec, sizeof(out_rec)); + } + +} |