Added reference and const modifiers to improve speed. Added DataBlocks class
This commit is contained in:
parent
77baa030c9
commit
8c464edb2e
7
QRCodeLibrary/DataBlocks.cpp
Normal file
7
QRCodeLibrary/DataBlocks.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include "pch.h"
|
||||||
|
#include "DataBlocks.hpp"
|
||||||
|
|
||||||
|
vector<unsigned char>& DataBlocks::compose_data_and_EC_blocks()
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
24
QRCodeLibrary/DataBlocks.hpp
Normal file
24
QRCodeLibrary/DataBlocks.hpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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<unsigned char>& compose_data_and_EC_blocks();
|
||||||
|
|
||||||
|
private:
|
||||||
|
const BitArray& e_data;
|
||||||
|
CorrectionLevel corr_lvl;
|
||||||
|
unsigned char version;
|
||||||
|
|
||||||
|
vector<unsigned char> data;
|
||||||
|
};
|
||||||
|
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
BitArray Encoder::encode()
|
BitArray& Encoder::encode()
|
||||||
{
|
{
|
||||||
unsigned encoded_bit_num = calculate_encoded_input_size(input.size(), method);
|
unsigned encoded_bit_num = calculate_encoded_input_size(input.size(), method);
|
||||||
unsigned metadata_bit_num = calculate_metadata_size(method, ((version < 0) ? 0 : version));
|
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;
|
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");
|
if (version < 0) throw std::runtime_error("Determite version before getting it");
|
||||||
|
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
BitArray Encoder::get_data()
|
BitArray Encoder::get_data() const
|
||||||
{
|
{
|
||||||
if (e.size == 0) throw std::runtime_error("Data is not calculated yet");
|
if (e.size == 0) throw std::runtime_error("Data is not calculated yet");
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ class Encoder
|
|||||||
public:
|
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_ } {};
|
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);
|
static unsigned char determite_version(unsigned size, CorrectionLevel corr_lvl);
|
||||||
|
|
||||||
@ -44,8 +44,8 @@ public:
|
|||||||
|
|
||||||
static void pad_data(BitArray& arr, unsigned bits_written);
|
static void pad_data(BitArray& arr, unsigned bits_written);
|
||||||
|
|
||||||
unsigned char get_version();
|
unsigned char get_version() const;
|
||||||
BitArray get_data();
|
BitArray get_data() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const string input;
|
const string input;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "Method.hpp"
|
#include "Method.hpp"
|
||||||
#include "Tables.hpp"
|
#include "Tables.hpp"
|
||||||
|
|
||||||
QRCodeMethod Method::determite_method(string input)
|
QRCodeMethod Method::determite_method(string& input)
|
||||||
{
|
{
|
||||||
QRCodeMethod type = QRCodeMethod::Numeric;
|
QRCodeMethod type = QRCodeMethod::Numeric;
|
||||||
|
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
#include "QRCode.hpp"
|
|
||||||
|
|
||||||
QRCode::QRCode(string input_, CorrectionLevel corr_lvl_, QRCodeMethod method_, unsigned char 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_ }
|
input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ }
|
||||||
{
|
{
|
||||||
if (method == QRCodeMethod::Dynamic)
|
if (method == QRCodeMethod::Dynamic)
|
||||||
method = Method::determite_method(input);
|
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<unsigned char>& blocked_data = data_blocks.compose_data_and_EC_blocks();
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ using namespace std;
|
|||||||
class QRCode
|
class QRCode
|
||||||
{
|
{
|
||||||
public:
|
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:
|
protected:
|
||||||
string input;
|
string input;
|
||||||
|
@ -152,6 +152,7 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="BitArray.hpp" />
|
<ClInclude Include="BitArray.hpp" />
|
||||||
|
<ClInclude Include="DataBlocks.hpp" />
|
||||||
<ClInclude Include="Encoder.hpp" />
|
<ClInclude Include="Encoder.hpp" />
|
||||||
<ClInclude Include="framework.h" />
|
<ClInclude Include="framework.h" />
|
||||||
<ClInclude Include="Method.hpp" />
|
<ClInclude Include="Method.hpp" />
|
||||||
@ -161,6 +162,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="BitArray.cpp" />
|
<ClCompile Include="BitArray.cpp" />
|
||||||
|
<ClCompile Include="DataBlocks.cpp" />
|
||||||
<ClCompile Include="Encoder.cpp" />
|
<ClCompile Include="Encoder.cpp" />
|
||||||
<ClCompile Include="Method.cpp" />
|
<ClCompile Include="Method.cpp" />
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
|
@ -36,6 +36,9 @@
|
|||||||
<ClInclude Include="BitArray.hpp">
|
<ClInclude Include="BitArray.hpp">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="DataBlocks.hpp">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
@ -56,5 +59,8 @@
|
|||||||
<ClCompile Include="BitArray.cpp">
|
<ClCompile Include="BitArray.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="DataBlocks.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -20,7 +20,7 @@ enum class CorrectionLevel {
|
|||||||
|
|
||||||
class Method {
|
class Method {
|
||||||
public:
|
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 constexpr bool is_num(char ch) { return ch >= '0' && ch <= '9'; };
|
||||||
static bool is_alphabetic(char ch);
|
static bool is_alphabetic(char ch);
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
#include "../QRCodeLibrary/Method.hpp"
|
#include "../QRCodeLibrary/Method.hpp"
|
||||||
|
|
||||||
TEST(MethodTests, DetermitesStringMethod) {
|
TEST(MethodTests, DetermitesStringMethod) {
|
||||||
EXPECT_EQ(Method::determite_method("123"), QRCodeMethod::Numeric);
|
EXPECT_EQ(Method::determite_method(std::string("123")), QRCodeMethod::Numeric);
|
||||||
EXPECT_EQ(Method::determite_method("ABC"), QRCodeMethod::Alphabetic);
|
EXPECT_EQ(Method::determite_method(std::string("ABC")), QRCodeMethod::Alphabetic);
|
||||||
EXPECT_EQ(Method::determite_method("ghfjghfj gfjhgd"), QRCodeMethod::Byte);
|
EXPECT_EQ(Method::determite_method(std::string("ghfjghfj gfjhgd")), QRCodeMethod::Byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MethodTests, ChecksNumber) {
|
TEST(MethodTests, ChecksNumber) {
|
||||||
|
@ -11,7 +11,7 @@ public:
|
|||||||
QRCodeConstructorMock(Parameters ...params) {};
|
QRCodeConstructorMock(Parameters ...params) {};
|
||||||
};
|
};
|
||||||
TEST(QRCodeTests, ConstructsClass) {
|
TEST(QRCodeTests, ConstructsClass) {
|
||||||
QRCode qr("TEST");
|
QRCode qr(std::string("TEST"));
|
||||||
|
|
||||||
EXPECT_EQ(qr.input, "TEST");
|
EXPECT_EQ(qr.input, "TEST");
|
||||||
EXPECT_EQ(qr.corr_lvl, CorrectionLevel::Q);
|
EXPECT_EQ(qr.corr_lvl, CorrectionLevel::Q);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user