code quality: more code cleanup

* removed unnecessary enum class `massivedynamic::Type`
* removed some not needed newlines
* cleaned up error handling by utilizing asserts more
pull/1/head
Michael Ochmann 2 years ago
parent 6f3827427b
commit 576bdf8430
  1. 10
      main.cpp
  2. 31
      src/QR.cpp
  3. 9
      src/QR.hpp
  4. 2
      src/renderers/ConsoleRenderer.hpp

@ -3,6 +3,7 @@
#include <iostream>
#include <sstream>
#include "qrcodegen.hpp"
#include "QR.hpp"
#define UNUSED(var) (void) var;
@ -72,7 +73,7 @@ int main(int argc, char* argv[]) {
size_t segmentSize = 0;
bool fromStdin = true;
bool anyParameterSet = false;
massivedynamic::Type type = massivedynamic::Type::MEDIUM;
qrcodegen::QrCode::Ecc type = qrcodegen::QrCode::Ecc::LOW;
massivedynamic::Format format = massivedynamic::Format::CONSOLE;
for(;;) {
@ -98,12 +99,13 @@ int main(int argc, char* argv[]) {
case 't': {
std::string value = optarg;
if (value == "small")
type = massivedynamic::Type::SMALL;
type = qrcodegen::QrCode::Ecc::LOW;
else if (value == "medium")
type = massivedynamic::Type::MEDIUM;
type = qrcodegen::QrCode::Ecc::MEDIUM;
else if (value == "large")
type = massivedynamic::Type::LARGE;
type = qrcodegen::QrCode::Ecc::HIGH;
else {
std::cerr << "ERROR: type (-t, --type) has to be one of 'small', 'medium' or 'large'" << std::endl;
printHelp();
return 1;
}

@ -1,8 +1,8 @@
#include <iostream>
#include <vector>
#include <assert.h>
#include "QR.hpp"
#include "qrcodegen.hpp"
#include "renderers/ConsoleRenderer.hpp"
#include "renderers/PNGRenderer.hpp"
@ -12,24 +12,8 @@
namespace massivedynamic {
QR::QR(const std::string& data, std::string outputFile, size_t size, Type type) : outputFile(std::move(outputFile)) {
qrcodegen::QrCode::Ecc errorCorrectionLevel = qrcodegen::QrCode::Ecc::HIGH;
switch(type) {
case Type::SMALL:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::LOW;
break;
case Type::MEDIUM:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::MEDIUM;
break;
default:
case Type::LARGE:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::HIGH;
break;
}
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.c_str(), errorCorrectionLevel);
QR::QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type) : outputFile(std::move(outputFile)) {
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.c_str(), type);
static_assert(FormatLength == 5, "exhaustive formats: did you miss to add something here?");
this->renderers.insert({Format::CONSOLE, std::make_unique<ConsoleRenderer>(this->pixels, qr.getSize(), size)});
@ -38,8 +22,8 @@ QR::QR(const std::string& data, std::string outputFile, size_t size, Type type)
this->renderers.insert({Format::JPG, std::make_unique<JPGRenderer>(this->pixels, qr.getSize(), size)});
this->renderers.insert({Format::BMP, std::make_unique<BMPRenderer>(this->pixels, qr.getSize(), size)});
// this is inherently stupid, but "qrcodegen" does not give access to the
// `segments` vector
// this is inherently stupid, but "qrcodegen::QrCode" does not give access to the
// `segments` vector member and the class itself is marked final.
for (int y = 0; y < qr.getSize(); y++) {
for (int x = 0; x < qr.getSize(); x++)
this->pixels.push_back(qr.getModule(x, y));
@ -48,10 +32,7 @@ QR::QR(const std::string& data, std::string outputFile, size_t size, Type type)
}
void QR::render(Format format) {
if (this->renderers.find(format) == this->renderers.end()) {
std::cerr << "ERROR: a non-valid renderer has been selected" << std::endl;
exit(1);
}
assert(this->renderers.find(format) != this->renderers.end() && "ERROR: a non-valid renderer has been selected");
this->renderers.at(format)->render(this->outputFile);
}

@ -11,12 +11,6 @@
namespace massivedynamic {
typedef uint32_t Color;
enum class Type {
SMALL,
MEDIUM,
LARGE
};
enum class Format {
CONSOLE,
SVG,
@ -27,14 +21,13 @@ namespace massivedynamic {
};
constexpr size_t FormatLength = static_cast<size_t>(Format::END);
class QR {
private:
std::string outputFile;
std::vector<bool> pixels;
std::unordered_map<Format, std::unique_ptr<Renderer>> renderers;
public:
QR(const std::string& data, std::string outputFile, size_t size, Type type);
QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type);
void render(Format format);
};

@ -21,7 +21,7 @@ class ConsoleRenderer : public Renderer {
std::cout << '\n';
}
std::cout << std::string(border, '\n');
std::cout << std::endl;
std::cout.flush();
}
};

Loading…
Cancel
Save