From 4d6185a5d318188d961e8448b9111198674f889d Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 28 Aug 2017 04:03:05 +0000 Subject: [PATCH] Reorganized a chunk of code in worker program of {Java, Python, C, C++} language versions, without changing behavior. --- c/qrcodegen-worker.c | 9 +++--- cpp/QrCodeGeneratorWorker.cpp | 9 ++---- .../qrcodegen/QrCodeGeneratorWorker.java | 30 ++++++++----------- python/qrcodegen-worker.py | 22 ++++++-------- 4 files changed, 29 insertions(+), 41 deletions(-) diff --git a/c/qrcodegen-worker.c b/c/qrcodegen-worker.c index 98e54cd..920198a 100644 --- a/c/qrcodegen-worker.c +++ b/c/qrcodegen-worker.c @@ -99,17 +99,16 @@ int main(void) { free(data); free(tempBuffer); - // Print grid of modules - if (!ok) - printf("-1\n"); - else { + if (ok) { + // Print grid of modules int size = qrcodegen_getSize(qrcode); printf("%d\n", (size - 17) / 4); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) printf("%d\n", qrcodegen_getModule(qrcode, x, y) ? 1 : 0); } - } + } else + printf("-1\n"); free(qrcode); fflush(stdout); } diff --git a/cpp/QrCodeGeneratorWorker.cpp b/cpp/QrCodeGeneratorWorker.cpp index fe89385..8ca58dc 100644 --- a/cpp/QrCodeGeneratorWorker.cpp +++ b/cpp/QrCodeGeneratorWorker.cpp @@ -83,11 +83,9 @@ int main() { } else segs.push_back(QrSegment::makeBytes(data)); - // Try to make QR Code symbol - try { + try { // Try to make QR Code symbol const QrCode qr = QrCode::encodeSegments(segs, *ECC_LEVELS[errCorLvl], minVersion, maxVersion, mask, boostEcl == 1); - // Print grid of modules std::cout << qr.version << std::endl; for (int y = 0; y < qr.size; y++) { @@ -96,12 +94,11 @@ int main() { } } catch (const char *msg) { - if (strcmp(msg, "Data too long") == 0) - std::cout << -1 << std::endl; - else { + if (strcmp(msg, "Data too long") != 0) { std::cerr << msg << std::endl; return EXIT_FAILURE; } + std::cout << -1 << std::endl; } std::cout << std::flush; } diff --git a/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java b/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java index 818ff18..ba61335 100644 --- a/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java +++ b/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java @@ -77,24 +77,20 @@ public final class QrCodeGeneratorWorker { else segs = Arrays.asList(QrSegment.makeBytes(data)); - // Try to make QR Code symbol - QrCode qr; - try { - qr = QrCode.encodeSegments(segs, QrCode.Ecc.values()[errCorLvl], minVersion, maxVersion, mask, boostEcl != 0); - } catch (IllegalArgumentException e) { - if (e.getMessage().equals("Data too long")) { - System.out.println(-1); - System.out.flush(); - continue; - } else - throw e; - } - // Print grid of modules - System.out.println(qr.version); - for (int y = 0; y < qr.size; y++) { - for (int x = 0; x < qr.size; x++) - System.out.println(qr.getModule(x, y)); + try { // Try to make QR Code symbol + QrCode qr = QrCode.encodeSegments(segs, QrCode.Ecc.values()[errCorLvl], minVersion, maxVersion, mask, boostEcl != 0); + // Print grid of modules + System.out.println(qr.version); + for (int y = 0; y < qr.size; y++) { + for (int x = 0; x < qr.size; x++) + System.out.println(qr.getModule(x, y)); + } + + } catch (IllegalArgumentException e) { + if (!e.getMessage().equals("Data too long")) + throw e; + System.out.println(-1); } System.out.flush(); } diff --git a/python/qrcodegen-worker.py b/python/qrcodegen-worker.py index 4f798fc..5262e1e 100644 --- a/python/qrcodegen-worker.py +++ b/python/qrcodegen-worker.py @@ -60,22 +60,18 @@ def main(): else: segs = [qrcodegen.QrSegment.make_bytes("".join(chr(b) for b in data))] - # Try to make QR Code symbol - try: + try: # Try to make QR Code symbol qr = qrcodegen.QrCode.encode_segments(segs, ECC_LEVELS[errcorlvl], minversion, maxversion, mask, boostecl != 0) + # Print grid of modules + print(qr.get_version()) + for y in range(qr.get_size()): + for x in range(qr.get_size()): + print(qr.get_module(x, y)) + except ValueError as e: - if e.args[0] == "Data too long": - print(-1) - sys.stdout.flush() - continue - else: + if e.args[0] != "Data too long": raise - - # Print grid of modules - print(qr.get_version()) - for y in range(qr.get_size()): - for x in range(qr.get_size()): - print(qr.get_module(x, y)) + print(-1) sys.stdout.flush()