diff --git a/QRCodeLibrary/DataBlocks.cpp b/QRCodeLibrary/DataBlocks.cpp new file mode 100644 index 0000000..e420543 --- /dev/null +++ b/QRCodeLibrary/DataBlocks.cpp @@ -0,0 +1,7 @@ +#include "pch.h" +#include "DataBlocks.hpp" + +vector& DataBlocks::compose_data_and_EC_blocks() +{ + return data; +} diff --git a/QRCodeLibrary/DataBlocks.hpp b/QRCodeLibrary/DataBlocks.hpp new file mode 100644 index 0000000..d9111f8 --- /dev/null +++ b/QRCodeLibrary/DataBlocks.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "BitArray.hpp" +#include "Method.hpp" + +using namespace std; + +class DataBlocks +{ +public: + DataBlocks(const BitArray& e_data_, CorrectionLevel corr_lvl_, unsigned char version_) : e_data{ e_data_ }, corr_lvl{ corr_lvl_ }, version{ version_ } {}; + + vector& compose_data_and_EC_blocks(); + +private: + const BitArray& e_data; + CorrectionLevel corr_lvl; + unsigned char version; + + vector data; +}; + diff --git a/QRCodeLibrary/Encoder.cpp b/QRCodeLibrary/Encoder.cpp index 1b9d3d2..e686e4e 100644 --- a/QRCodeLibrary/Encoder.cpp +++ b/QRCodeLibrary/Encoder.cpp @@ -5,7 +5,7 @@ #include -BitArray Encoder::encode() +BitArray& Encoder::encode() { unsigned encoded_bit_num = calculate_encoded_input_size(input.size(), method); unsigned metadata_bit_num = calculate_metadata_size(method, ((version < 0) ? 0 : version)); @@ -138,14 +138,14 @@ void Encoder::pad_data(BitArray& arr, unsigned bits_written) arr.v[i] = ((i - encoded_bytes) % 2 == 0) ? 0b11101100 : 0b00010001; } -unsigned char Encoder::get_version() +unsigned char Encoder::get_version() const { if (version < 0) throw std::runtime_error("Determite version before getting it"); return version; } -BitArray Encoder::get_data() +BitArray Encoder::get_data() const { if (e.size == 0) throw std::runtime_error("Data is not calculated yet"); diff --git a/QRCodeLibrary/Encoder.hpp b/QRCodeLibrary/Encoder.hpp index fca397a..79f3926 100644 --- a/QRCodeLibrary/Encoder.hpp +++ b/QRCodeLibrary/Encoder.hpp @@ -13,7 +13,7 @@ class Encoder public: Encoder(const string& input_, CorrectionLevel corr_lvl_ = CorrectionLevel::M, QRCodeMethod method_ = QRCodeMethod::Dynamic, char version_ = -1) : input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ } {}; - BitArray encode(); + BitArray& encode(); static unsigned char determite_version(unsigned size, CorrectionLevel corr_lvl); @@ -44,8 +44,8 @@ public: static void pad_data(BitArray& arr, unsigned bits_written); - unsigned char get_version(); - BitArray get_data(); + unsigned char get_version() const; + BitArray get_data() const; private: const string input; diff --git a/QRCodeLibrary/Method.cpp b/QRCodeLibrary/Method.cpp index 22da80f..14fbc96 100644 --- a/QRCodeLibrary/Method.cpp +++ b/QRCodeLibrary/Method.cpp @@ -3,7 +3,7 @@ #include "Method.hpp" #include "Tables.hpp" -QRCodeMethod Method::determite_method(string input) +QRCodeMethod Method::determite_method(string& input) { QRCodeMethod type = QRCodeMethod::Numeric; diff --git a/QRCodeLibrary/QRCode.cpp b/QRCodeLibrary/QRCode.cpp index 7206c7c..675c75d 100644 --- a/QRCodeLibrary/QRCode.cpp +++ b/QRCodeLibrary/QRCode.cpp @@ -1,9 +1,19 @@ #include "pch.h" -#include "QRCode.hpp" -QRCode::QRCode(string input_, CorrectionLevel corr_lvl_, QRCodeMethod method_, unsigned char version_) : - input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ } +#include "QRCode.hpp" +#include "Encoder.hpp" +#include "DataBlocks.hpp" + +QRCode::QRCode(string& input_, CorrectionLevel corr_lvl_, QRCodeMethod method_, unsigned char version_) : + input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ } { if (method == QRCodeMethod::Dynamic) method = Method::determite_method(input); + + Encoder encoder(input, corr_lvl, method, version); + version = encoder.get_version(); + const BitArray& encoded_data = encoder.encode(); + + DataBlocks data_blocks(encoded_data, corr_lvl, version); + vector& blocked_data = data_blocks.compose_data_and_EC_blocks(); } diff --git a/QRCodeLibrary/QRCode.hpp b/QRCodeLibrary/QRCode.hpp index 5bccf87..0cb9562 100644 --- a/QRCodeLibrary/QRCode.hpp +++ b/QRCodeLibrary/QRCode.hpp @@ -9,7 +9,7 @@ using namespace std; class QRCode { public: - QRCode(string input_, CorrectionLevel corr_lvl_ = CorrectionLevel::Q, QRCodeMethod method_ = QRCodeMethod::Dynamic, unsigned char version_ = 0); + QRCode(string& input_, CorrectionLevel corr_lvl_ = CorrectionLevel::Q, QRCodeMethod method_ = QRCodeMethod::Dynamic, unsigned char version_ = 0); protected: string input; diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj b/QRCodeLibrary/QRCodeLibrary.vcxproj index 7462e57..bca060a 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj @@ -152,6 +152,7 @@ + @@ -161,6 +162,7 @@ + diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters index f504f0f..0e0799f 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters @@ -36,6 +36,9 @@ Header Files + + Header Files + @@ -56,5 +59,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/QRCodeLibrary/method.hpp b/QRCodeLibrary/method.hpp index 6e7a3a0..8c2e41a 100644 --- a/QRCodeLibrary/method.hpp +++ b/QRCodeLibrary/method.hpp @@ -20,7 +20,7 @@ enum class CorrectionLevel { class Method { public: - static QRCodeMethod determite_method(string input); + static QRCodeMethod determite_method(string& input); static constexpr bool is_num(char ch) { return ch >= '0' && ch <= '9'; }; static bool is_alphabetic(char ch); diff --git a/tests/Method_test.cpp b/tests/Method_test.cpp index d9b6374..385f620 100644 --- a/tests/Method_test.cpp +++ b/tests/Method_test.cpp @@ -6,9 +6,9 @@ #include "../QRCodeLibrary/Method.hpp" TEST(MethodTests, DetermitesStringMethod) { - EXPECT_EQ(Method::determite_method("123"), QRCodeMethod::Numeric); - EXPECT_EQ(Method::determite_method("ABC"), QRCodeMethod::Alphabetic); - EXPECT_EQ(Method::determite_method("ghfjghfj gfjhgd"), QRCodeMethod::Byte); + EXPECT_EQ(Method::determite_method(std::string("123")), QRCodeMethod::Numeric); + EXPECT_EQ(Method::determite_method(std::string("ABC")), QRCodeMethod::Alphabetic); + EXPECT_EQ(Method::determite_method(std::string("ghfjghfj gfjhgd")), QRCodeMethod::Byte); } TEST(MethodTests, ChecksNumber) { diff --git a/tests/QRCode_test.cpp b/tests/QRCode_test.cpp index fc961d3..839524c 100644 --- a/tests/QRCode_test.cpp +++ b/tests/QRCode_test.cpp @@ -11,7 +11,7 @@ public: QRCodeConstructorMock(Parameters ...params) {}; }; TEST(QRCodeTests, ConstructsClass) { - QRCode qr("TEST"); + QRCode qr(std::string("TEST")); EXPECT_EQ(qr.input, "TEST"); EXPECT_EQ(qr.corr_lvl, CorrectionLevel::Q);