Skip to content

Commit

Permalink
Update C API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Feb 1, 2024
1 parent bd3f150 commit 2f758c0
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 135 deletions.
2 changes: 1 addition & 1 deletion .bumpversion-capi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ current_version = "0.2.5"
filename = "crates/capi/examples/meson.build"

[[tool.bumpversion.files]]
filename = "crates/capi/examples/version.cpp"
filename = "crates/capi/examples/version.hpp"

[[tool.bumpversion.files]]
filename = "crates/capi/src/lib.rs"
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install libfmt-dev meson
sudo apt-get install libcli11-dev libfmt-dev meson
meson -v
- name: Setup just
uses: extractions/setup-just@v1
Expand Down
6 changes: 6 additions & 0 deletions crates/capi/CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ All notable changes to this project will be documented in this file.
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
project adheres to https://semver.org/[Semantic Versioning].

== {compare-url}/abcrypt-capi-v0.2.5\...HEAD[Unreleased]

=== Changed

* Update C API examples ({pull-request-url}/257[#257])

== {compare-url}/abcrypt-capi-v0.2.4\...abcrypt-capi-v0.2.5[0.2.5] - 2024-01-18

=== Changed
Expand Down
43 changes: 9 additions & 34 deletions crates/capi/examples/decrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <termios.h>
#include <unistd.h>

#include <CLI/CLI.hpp>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
Expand All @@ -21,41 +22,15 @@
#include "abcrypt.h"
#include "version.hpp"

static void print_help(void) {
std::cout << "Usage: decrypt <INFILE> <OUTFILE>\n\n";
std::cout << "Arguments:\n";
std::cout << " <INFILE> File to decrypt\n";
std::cout << " <OUTFILE> File to write the result to\n\n";
std::cout << "Options:\n";
std::cout << " -h Print help\n";
std::cout << " -V Print version" << std::endl;
}

int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "hV")) != -1) {
switch (opt) {
case 'h':
print_help();
return EXIT_SUCCESS;
case 'V':
print_version();
return EXIT_SUCCESS;
default:
print_help();
return EXIT_FAILURE;
}
}

char *input_filename;
char *output_filename;
if ((argc - optind) == 2) {
input_filename = argv[optind];
output_filename = argv[optind + 1];
} else {
print_help();
return EXIT_FAILURE;
}
CLI::App app{
"An example of decrypting from the abcrypt encrypted data format"};
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<INFILE>", input_filename, "Input file")->required();
std::string output_filename;
app.add_option("<OUTFILE>", output_filename, "Output file")->required();
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
if (!input_file) {
Expand Down
68 changes: 18 additions & 50 deletions crates/capi/examples/encrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <termios.h>
#include <unistd.h>

#include <CLI/CLI.hpp>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
Expand All @@ -21,57 +22,24 @@
#include "abcrypt.h"
#include "version.hpp"

static void print_help(void) {
std::cout << "Usage: encrypt [OPTIONS] <INFILE> <OUTFILE>\n\n";
std::cout << "Arguments:\n";
std::cout << " <INFILE> File to encrypt\n";
std::cout << " <OUTFILE> File to write the result to\n\n";
std::cout << "Options:\n";
std::cout << " -m <NUM> Set the memory size in KiB [default: 19456]\n";
std::cout << " -t <NUM> Set the number of iterations [default: 2]\n";
std::cout << " -p <NUM> Set the degree of parallelism [default: 1]\n";
std::cout << " -h Print help\n";
std::cout << " -V Print version" << std::endl;
}

int main(int argc, char *argv[]) {
std::uint32_t memory_size = 19456;
std::uint32_t iterations = 2;
std::uint32_t parallelism = 1;

int opt;
while ((opt = getopt(argc, argv, "m:t:p:hV")) != -1) {
switch (opt) {
case 'm':
memory_size = std::atoi(optarg);
break;
case 't':
iterations = std::atoi(optarg);
break;
case 'p':
parallelism = std::atoi(optarg);
break;
case 'h':
print_help();
return EXIT_SUCCESS;
case 'V':
print_version();
return EXIT_SUCCESS;
default:
print_help();
return EXIT_FAILURE;
}
}

char *input_filename;
char *output_filename;
if ((argc - optind) == 2) {
input_filename = argv[optind];
output_filename = argv[optind + 1];
} else {
print_help();
return EXIT_FAILURE;
}
CLI::App app{"An example of encrypting to the abcrypt encrypted data format"};
std::uint32_t memory_size{19456};
app.add_option("-m,--memory-size", memory_size, "Set the memory size in KiB")
->capture_default_str();
std::uint32_t iterations{2};
app.add_option("-t,--iterations", iterations, "Set the number of iterations")
->capture_default_str();
std::uint32_t parallelism{1};
app.add_option("-p,--parallelism", parallelism,
"Set the degree of parallelism")
->capture_default_str();
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<INFILE>", input_filename, "Input file")->required();
std::string output_filename;
app.add_option("<OUTFILE>", output_filename, "Output file")->required();
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
if (!input_file) {
Expand Down
38 changes: 6 additions & 32 deletions crates/capi/examples/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// An example of reading the Argon2 parameters from a file.

#include <fmt/core.h>
#include <unistd.h>

#include <CLI/CLI.hpp>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
Expand All @@ -20,38 +20,12 @@
#include "abcrypt.h"
#include "version.hpp"

static void print_help(void) {
std::cout << "Usage: info <FILE>\n\n";
std::cout << "Arguments:\n";
std::cout << " <FILE> File to print the Argon2 parameters\n\n";
std::cout << "Options:\n";
std::cout << " -h Print help\n";
std::cout << " -V Print version" << std::endl;
}

int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "hV")) != -1) {
switch (opt) {
case 'h':
print_help();
return EXIT_SUCCESS;
case 'V':
print_version();
return EXIT_SUCCESS;
default:
print_help();
return EXIT_FAILURE;
}
}

char *input_filename;
if ((argc - optind) == 1) {
input_filename = argv[optind];
} else {
print_help();
return EXIT_FAILURE;
}
CLI::App app{"An example of reading the Argon2 parameters"};
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<FILE>", input_filename, "Input file")->required();
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
if (!input_file) {
Expand Down
5 changes: 2 additions & 3 deletions crates/capi/examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cpp = meson.get_compiler('cpp')
cpp.check_header('termios.h', required: true)
cpp.check_header('unistd.h', required: true)

fmtdep = dependency('fmt')
deps = [dependency('CLI11'), dependency('fmt')]

libdir = meson.current_source_dir() / '../../../target'
if fs.exists(libdir / 'release')
Expand All @@ -31,8 +31,7 @@ example_names = ['encrypt', 'decrypt', 'info']
foreach example_name : example_names
executable(example_name,
example_name + '.cpp',
'version.cpp',
dependencies: [fmtdep, libabcrypt_capi],
dependencies: [deps, libabcrypt_capi],
include_directories: incdir,
)
endforeach
13 changes: 0 additions & 13 deletions crates/capi/examples/version.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion crates/capi/examples/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

#pragma once

void print_version(void);
inline constexpr auto VERSION = "0.2.5";

0 comments on commit 2f758c0

Please sign in to comment.