Skip to content

Commit

Permalink
qrcode example
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Sep 29, 2022
1 parent 6bd4691 commit f269b5b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/qbk/6.0.examples.qbk
Expand Up @@ -12,6 +12,10 @@
[section Examples]
[block'''<?dbhtml stop-chunking?>''']

[section Qr Code]
[example_qrcode]
[endsect]

[section Magnet Link]
[example_magnet]
[endsect]
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Expand Up @@ -7,5 +7,6 @@
# Official repository: https://github.com/boostorg/url
#

add_subdirectory(qrcode)
add_subdirectory(magnet)
add_subdirectory(route)
1 change: 1 addition & 0 deletions example/Jamfile
Expand Up @@ -7,5 +7,6 @@
# Official repository: https://github.com/boostorg/url
#

build-project qrcode ;
build-project magnet ;
build-project route ;
19 changes: 19 additions & 0 deletions example/qrcode/CMakeLists.txt
@@ -0,0 +1,19 @@
#
# Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/url
#

source_group("" FILES
qrcode.cpp
)

add_executable(qrcode
qrcode.cpp
)

set_property(TARGET qrcode PROPERTY FOLDER "Examples")
target_link_libraries(qrcode PRIVATE Boost::url)
17 changes: 17 additions & 0 deletions example/qrcode/Jamfile
@@ -0,0 +1,17 @@
#
# Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/url
#

project : requirements ;

project
: requirements
<library>/boost/url//boost_url
;

exe qrcode : qrcode.cpp ;
87 changes: 87 additions & 0 deletions example/qrcode/qrcode.cpp
@@ -0,0 +1,87 @@
//
// Copyright (c) 2022 alandefreitas (alandefreitas@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
//

//[example_qrcode

/*
This example shows how to compose and mutate
URLs to consume a third party API to
generate QR Codes.
https://developers.google.com/chart/infographics/docs/qr_codes
*/

#include <boost/url/url.hpp>
#include <boost/url/parse.hpp>
#include <iostream>

namespace urls = boost::urls;

int main(int argc, char** argv)
{
if (argc < 2) {
std::cout << argv[0] << "\n";
std::cout << "Usage: qrcode <data> <width> <height> <output encoding> <error correction> <border>\n"
"options:\n"
" <data>: The data to encode (required)\n"
" <width>: Image width (default: 100)\n"
" <height>: Image height (default: width)\n"
" <output encoding>: UTF-8, Shift_JIS, ISO-8859-1 (default: utf8)\n"
" <error correction>: percentage of error correction (default: 7)\n"
" <margin>: border width (default: 4)\n"
"examples:\n"
"qrcode \"Hello world\"\n";
return EXIT_FAILURE;
}

urls::url u =
urls::parse_uri(
"https://chart.googleapis.com/chart?cht=qr").value();
auto ps = u.params();

// Data
ps.append({"chl", argv[1]});

// Size
std::size_t width = argc < 3 ? 100 : std::stoll(argv[2]);
std::size_t height = argc < 4 ? width : std::stoll(argv[3]);
ps.append({"chs", std::to_string(width) + "x" + std::to_string(height)});

// Encoding
if (argc >= 5)
{
urls::string_view output_encoding =
urls::string_view(argv[3]) == "Shift_JIS" ||
urls::string_view(argv[3]) == "ISO-8859-1" ?
argv[4] : "UTF-8";
ps.append({"choe", output_encoding});
}

// Error
if (argc >= 6)
{
std::size_t err = std::stoll(argv[5]);
std::string chld;
if (err < 11)
chld = "L";
else if (err < 20)
chld = "M";
else if (err < 27)
chld = "Q";
else
chld = "H";
std::size_t margin = argc < 7 ? 4 : std::stoll(argv[6]);
chld += "|";
chld += std::to_string(margin);
ps.append({"chld", chld});
}

std::cout << u << '\n';

return EXIT_SUCCESS;
}

//]

0 comments on commit f269b5b

Please sign in to comment.