-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (88 loc) · 2.44 KB
/
Copy pathmain.cpp
File metadata and controls
105 lines (88 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <qrencode.h>
/// Print left or right margin for enhanced visibility.
static void print_horizontal_margin(){
std::cout << "\033[01;107m \033[00m";
}
/// Print top or bottom margin for enhanced visibility.
static void print_vertical_margin(int width){
int i;
print_horizontal_margin();
print_horizontal_margin();
for (i = 0; i < width; i++ ) {
std::cout << "\033[01;107m \033[00m";
}
std::cout << std::endl;
}
/// Show version
void show_version() {
std::cerr
<< "qrenconsole v0.1.0 - licensed under GPLv2 by Nomura Suzume"
<< std::endl;
}
/// Show usage
void show_usage(){
show_version();
std::cerr
<< "Any words from stdin will be converted to inline QR code:"
<< std::endl;
}
/// Convert string into ASCII QR code lines.
/// For coloring detail, see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
int convert_to_ascii_qr(std::string s){
QRcode *qrcode;
unsigned char* ref;
int i, j, bitshift;
uint64_t img_bit;
if (s.length() == 0) {
std::cerr << "null line detected!";
return 1;
} else if (s == "\n") {
std::cerr << "no description";
show_usage();
return 1;
}
qrcode = QRcode_encodeString8bit(
s.c_str(),
1,
QR_ECLEVEL_M
);
unsigned char* ref_root = qrcode->data;
//top margin
print_vertical_margin(qrcode->width);
print_vertical_margin(qrcode->width);
for (i = 0; i < qrcode->width; i++ ) {
//left margin
print_horizontal_margin();
//body
for (j=0; j < qrcode->width; j++) {
bitshift = (i + j * qrcode->width);
ref = ref_root + bitshift;
img_bit = uint64_t (*ref);
if ( img_bit % 2 == 1) {
std::cout << "\033[01;40m \033[00m";
} else {
std::cout << "\033[01;107m \033[00m";
};
}
//right margin
print_horizontal_margin();
//next
std::cout << std::endl;
}
//bottom margin
print_vertical_margin(qrcode->width);
print_vertical_margin(qrcode->width);
std::cerr << std::endl << "EOF" << std::endl;
QRcode_free(qrcode);
return 0;
}
int main() {
std::string s;
show_usage();
while (std::cin >> s) {
std::cout << "<target> " << s << std::endl;
convert_to_ascii_qr(s);
}
return 0;
}