• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * QR Code generator test worker (C++)
3  *
4  * This program reads data and encoding parameters from standard input and writes
5  * QR Code bitmaps to standard output. The I/O format is one integer per line.
6  * Run with no command line arguments. The program is intended for automated
7  * batch testing of end-to-end functionality of this QR Code generator library.
8  *
9  * Copyright (c) Project Nayuki. (MIT License)
10  * https://www.nayuki.io/page/qr-code-generator-library
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
13  * this software and associated documentation files (the "Software"), to deal in
14  * the Software without restriction, including without limitation the rights to
15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16  * the Software, and to permit persons to whom the Software is furnished to do so,
17  * subject to the following conditions:
18  * - The above copyright notice and this permission notice shall be included in
19  *   all copies or substantial portions of the Software.
20  * - The Software is provided "as is", without warranty of any kind, express or
21  *   implied, including but not limited to the warranties of merchantability,
22  *   fitness for a particular purpose and noninfringement. In no event shall the
23  *   authors or copyright holders be liable for any claim, damages or other
24  *   liability, whether in an action of contract, tort or otherwise, arising from,
25  *   out of or in connection with the Software or the use or other dealings in the
26  *   Software.
27  */
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <cstdlib>
32 #include <cstring>
33 #include <iostream>
34 #include <vector>
35 #include "QrCode.hpp"
36 
37 using qrcodegen::QrCode;
38 using qrcodegen::QrSegment;
39 
40 
41 static const std::vector<QrCode::Ecc> ECC_LEVELS{
42 	QrCode::Ecc::LOW,
43 	QrCode::Ecc::MEDIUM,
44 	QrCode::Ecc::QUARTILE,
45 	QrCode::Ecc::HIGH,
46 };
47 
48 
main()49 int main() {
50 	while (true) {
51 
52 		// Read data length or exit
53 		int length;
54 		std::cin >> length;
55 		if (length == -1)
56 			break;
57 
58 		// Read data bytes
59 		bool isAscii = true;
60 		std::vector<uint8_t> data;
61 		for (int i = 0; i < length; i++) {
62 			int b;
63 			std::cin >> b;
64 			data.push_back(static_cast<uint8_t>(b));
65 			isAscii &= 0 < b && b < 128;
66 		}
67 
68 		// Read encoding parameters
69 		int errCorLvl, minVersion, maxVersion, mask, boostEcl;
70 		std::cin >> errCorLvl;
71 		std::cin >> minVersion;
72 		std::cin >> maxVersion;
73 		std::cin >> mask;
74 		std::cin >> boostEcl;
75 
76 		// Make list of segments
77 		std::vector<QrSegment> segs;
78 		if (isAscii) {
79 			std::vector<char> text(data.cbegin(), data.cend());
80 			text.push_back('\0');
81 			segs = QrSegment::makeSegments(text.data());
82 		} else
83 			segs.push_back(QrSegment::makeBytes(data));
84 
85 		try {  // Try to make QR Code symbol
86 			const QrCode qr = QrCode::encodeSegments(segs,
87 				ECC_LEVELS.at(static_cast<std::size_t>(errCorLvl)), minVersion, maxVersion, mask, boostEcl == 1);
88 			// Print grid of modules
89 			std::cout << qr.getVersion() << std::endl;
90 			for (int y = 0; y < qr.getSize(); y++) {
91 				for (int x = 0; x < qr.getSize(); x++)
92 					std::cout << (qr.getModule(x, y) ? 1 : 0) << std::endl;
93 			}
94 
95 		} catch (const qrcodegen::data_too_long &ex) {
96 			std::cout << -1 << std::endl;
97 		}
98 		std::cout << std::flush;
99 	}
100 	return EXIT_SUCCESS;
101 }
102