From 439207b388d07cb331f5d1b94e3637ee2ce08944 Mon Sep 17 00:00:00 2001 From: Beman Date: Fri, 5 Dec 2014 09:01:59 -0500 Subject: [PATCH] Add conversion_use_case --- example/conversion_use_case.cpp | 51 +++++++++++ example/third_party_format.hpp | 19 ++++ .../conversion_use_case.vcxproj | 88 +++++++++++++++++++ test/msvc/endian.sln | 8 ++ 4 files changed, 166 insertions(+) create mode 100644 example/conversion_use_case.cpp create mode 100644 example/third_party_format.hpp create mode 100644 test/msvc/conversion_use_case/conversion_use_case.vcxproj diff --git a/example/conversion_use_case.cpp b/example/conversion_use_case.cpp new file mode 100644 index 0000000..76329bb --- /dev/null +++ b/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 +#include +#include +#include +#include + +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 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(rec.balance); // reverse if needed + out.write((const char*)&out_rec, sizeof(out_rec)); + } + +} diff --git a/example/third_party_format.hpp b/example/third_party_format.hpp new file mode 100644 index 0000000..d647283 --- /dev/null +++ b/example/third_party_format.hpp @@ -0,0 +1,19 @@ +// endian/example/third_party_format.hpp + +// Copyright Beman Dawes 2014 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +#include + +namespace third_party +{ + struct record + { + uint32_t id; // big endian + int32_t balance; // big endian + + // ... data members whose endianness is not a concern + }; +} diff --git a/test/msvc/conversion_use_case/conversion_use_case.vcxproj b/test/msvc/conversion_use_case/conversion_use_case.vcxproj new file mode 100644 index 0000000..d7d8cea --- /dev/null +++ b/test/msvc/conversion_use_case/conversion_use_case.vcxproj @@ -0,0 +1,88 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {1139E765-DE0F-497A-A7D9-EB2683521DF1} + Win32Proj + conversion_use_case + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/test/msvc/endian.sln b/test/msvc/endian.sln index d311805..b38a560 100644 --- a/test/msvc/endian.sln +++ b/test/msvc/endian.sln @@ -27,6 +27,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "buffer_test", "buffer_test\ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uses_cases", "uses_cases\uses_cases.vcxproj", "{36BF451A-EAEF-4140-92E4-6EA461A26107}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "conversion_use_case", "conversion_use_case\conversion_use_case.vcxproj", "{1139E765-DE0F-497A-A7D9-EB2683521DF1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -122,6 +124,12 @@ Global {36BF451A-EAEF-4140-92E4-6EA461A26107}.Release|Win32.ActiveCfg = Release|Win32 {36BF451A-EAEF-4140-92E4-6EA461A26107}.Release|Win32.Build.0 = Release|Win32 {36BF451A-EAEF-4140-92E4-6EA461A26107}.Release|x64.ActiveCfg = Release|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Debug|Win32.ActiveCfg = Debug|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Debug|Win32.Build.0 = Debug|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Debug|x64.ActiveCfg = Debug|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Release|Win32.ActiveCfg = Release|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Release|Win32.Build.0 = Release|Win32 + {1139E765-DE0F-497A-A7D9-EB2683521DF1}.Release|x64.ActiveCfg = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE