From 67386adad2f664ca721bae448828e99844507499 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Wed, 15 Dec 2021 22:29:36 +0300 Subject: [PATCH] Added library demo --- Demo/Demo.vcxproj | 152 ++++++++++++++++++++++++++++++++++++++ Demo/Demo.vcxproj.filters | 22 ++++++ Demo/main.cpp | 101 +++++++++++++++++++++++++ QRCodeLibrary.sln | 10 +++ QRCodeLibrary/Encoder.hpp | 2 +- tests/Encoder_test.cpp | 18 ++--- 6 files changed, 295 insertions(+), 10 deletions(-) create mode 100644 Demo/Demo.vcxproj create mode 100644 Demo/Demo.vcxproj.filters create mode 100644 Demo/main.cpp diff --git a/Demo/Demo.vcxproj b/Demo/Demo.vcxproj new file mode 100644 index 0000000..a7a6de1 --- /dev/null +++ b/Demo/Demo.vcxproj @@ -0,0 +1,152 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {b2ae1ac2-8d6a-4990-a6a7-e96e3ae6e6ea} + Demo + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + {b1126767-cdf7-4831-8b08-85bf457db4e2} + + + + + + + + + \ No newline at end of file diff --git a/Demo/Demo.vcxproj.filters b/Demo/Demo.vcxproj.filters new file mode 100644 index 0000000..ce0c35c --- /dev/null +++ b/Demo/Demo.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/Demo/main.cpp b/Demo/main.cpp new file mode 100644 index 0000000..00a8ad8 --- /dev/null +++ b/Demo/main.cpp @@ -0,0 +1,101 @@ +#include +#include +#include + +#include "../QRCodeLibrary/QRCode.hpp" + +using namespace std; + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +std::string cp1251_to_utf8(const char* str) +{ + std::string res; + WCHAR* ures = NULL; + char* cres = NULL; + + int result_u = MultiByteToWideChar(1251, 0, str, -1, 0, 0); + if (result_u != 0) + { + ures = new WCHAR[result_u]; + if (MultiByteToWideChar(1251, 0, str, -1, ures, result_u)) + { + int result_c = WideCharToMultiByte(CP_UTF8, 0, ures, -1, 0, 0, 0, 0); + if (result_c != 0) + { + cres = new char[result_c]; + if (WideCharToMultiByte(CP_UTF8, 0, ures, -1, cres, result_c, 0, 0)) + { + res = cres; + } + } + } + } + + delete[] ures; + delete[] cres; + + return res; +} +#else +string str_of(unsigned k, const string input) { + string res; + for (;k > 0; k--) + res += input; + return res; +} +#endif + +int main() { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + SetConsoleCP(1251); +#endif + + string input; + + getline(cin, input); + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + input = cp1251_to_utf8(input.c_str()); +#endif + + QRCode qr(input); + + const auto& res = qr.to_vector(); + + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + const string long_sep = string((res.size() + 8) * 2, 219), + short_sep = string(4 * 2, 219), + black = string(2, 219), + white = " "; + + SetConsoleCP(855); +#else + const string long_sep = str_of((res.size() + 8) * 2, "█"), + short_sep = str_of(4 * 2, "█"), + black = str_of(2, "█"), + white = " "; +#endif + + for (int i = 0; i < 4; i++) + cout << long_sep << endl; + + for (unsigned i = 0; i < res.size(); i++) { + cout << short_sep; + for (auto cell : res[i]) + switch (cell) { + case Trit::T: + cout << white; + break; + case Trit::F: + cout << black; + break; + default: + throw std::runtime_error("Empty cell is not allowed. Your QR code is corrupted."); + } + cout << short_sep << endl; + } + + for (int i = 0; i < 4; i++) + cout << long_sep << endl; +} \ No newline at end of file diff --git a/QRCodeLibrary.sln b/QRCodeLibrary.sln index 7598357..8c97003 100644 --- a/QRCodeLibrary.sln +++ b/QRCodeLibrary.sln @@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "QRCodeLibrary", "QRCodeLibr EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests\tests.vcxproj", "{96CDAF28-007A-4900-B24E-E21676713543}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Demo", "Demo\Demo.vcxproj", "{B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -31,6 +33,14 @@ Global {96CDAF28-007A-4900-B24E-E21676713543}.Release|x64.Build.0 = Release|x64 {96CDAF28-007A-4900-B24E-E21676713543}.Release|x86.ActiveCfg = Release|Win32 {96CDAF28-007A-4900-B24E-E21676713543}.Release|x86.Build.0 = Release|Win32 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Debug|x64.ActiveCfg = Debug|x64 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Debug|x64.Build.0 = Debug|x64 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Debug|x86.ActiveCfg = Debug|Win32 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Debug|x86.Build.0 = Debug|Win32 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Release|x64.ActiveCfg = Release|x64 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Release|x64.Build.0 = Release|x64 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Release|x86.ActiveCfg = Release|Win32 + {B2AE1AC2-8D6A-4990-A6A7-E96E3AE6E6EA}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/QRCodeLibrary/Encoder.hpp b/QRCodeLibrary/Encoder.hpp index cf359dd..44c59c2 100644 --- a/QRCodeLibrary/Encoder.hpp +++ b/QRCodeLibrary/Encoder.hpp @@ -57,7 +57,7 @@ private: }; template -constexpr unsigned char upper_index(const array arr, T val) { +constexpr unsigned upper_index(const array arr, T val) { unsigned count = arr.size(), s = 0, e, step; while (count > 0) { diff --git a/tests/Encoder_test.cpp b/tests/Encoder_test.cpp index a366c5e..2d8f70d 100644 --- a/tests/Encoder_test.cpp +++ b/tests/Encoder_test.cpp @@ -7,29 +7,29 @@ /* upper_index function */ TEST(UpperIndexTests, FindsExactMatch) { - array arr1{ 1, 2, 3, 4, 5 }; + array arr1{ 1, 2, 3, 4, 5 }; - for (int i = 0; i < 5; i++) + for (unsigned i = 0; i < 5; i++) EXPECT_EQ(upper_index(arr1, i+1), i); - array arr2{ 1, 2, 3, 4 }; + array arr2{ 1, 2, 3, 4 }; - for (int i = 0; i < 4; i++) + for (unsigned i = 0; i < 4; i++) EXPECT_EQ(upper_index(arr2, i + 1), i); } TEST(UpperIndexTests, FindsClosestUpper) { - array arr{ 1, 3, 5, 7, 9 }; + array arr{ 1, 3, 5, 7, 9 }; - for (int i = 0; i < 5; i++) + for (unsigned i = 0; i < 5; i++) EXPECT_EQ(upper_index(arr, i * 2), i); } TEST(UpperIndexTests, IfNothingFoundReturnsArrSize) { - array arr{ 0, 1, 2, 3, 4 }; + array arr{ 0, 1, 2, 3, 4 }; - EXPECT_EQ(upper_index(arr, 5), 5); - EXPECT_EQ(upper_index(arr, 10), 5); + EXPECT_EQ(upper_index(arr, 5u), 5); + EXPECT_EQ(upper_index(arr, 10u), 5); } /* Encoder class */