Skip to content

Commit

Permalink
improve messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
smortezah committed Apr 16, 2020
1 parent a3998be commit 64b582e
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 128 deletions.
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
.vscode/*
build
cryfa
keygen
keygen

origin
*.fa
*.fq
in
fa
fq
mori
origfa
origfq
6 changes: 2 additions & 4 deletions src/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ class application {
void application::exe_compress_encrypt() {
switch (par.format) {
case 'A':
std::cerr << "Compacting...\n";
fa.compress();
break;
case 'Q':
// std::cerr << "Compacting...\n";
fq.compress();
break;
case 'n':
Expand All @@ -85,11 +83,11 @@ void application::exe_decrypt_decompress() {
std::ifstream in(DEC_FNAME);
switch (in.peek()) {
case (char)127:
std::cerr << "Decompressing...\n";
// std::cerr << "Decompressing...\n";
fa.decompress();
break;
case (char)126:
std::cerr << "Decompressing...\n";
// std::cerr << "Decompressing...\n";
fq.decompress();
break;
case (char)125:
Expand Down
2 changes: 1 addition & 1 deletion src/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ inline void assert_file_good(const std::string& fname,
std::ifstream in(fname);
if (!in.good() || in.peek() == EOF) {
in.close();
assert_dual(msg.empty(), "failed opening the file \"" + fname + "\".", msg);
assert_dual(msg.empty(), "failed opening \"" + fname + "\".", msg);
}
in.close();
}
Expand Down
46 changes: 22 additions & 24 deletions src/endecrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <thread>

#include "assert.hpp"
#include "file.hpp"
#include "time.hpp"
using namespace cryfa;

std::mutex mutxEnDe; /**< @brief Mutex */
Expand Down Expand Up @@ -236,8 +238,7 @@ void EnDecrypto::build_unpack_tbl(std::vector<std::string>& unpack,
*/
byte EnDecrypto::dna_pack_idx(const std::string& key) {
const auto found = DNA_MAP.find(key);
if (found == DNA_MAP.end())
error("key \"" + key + "\" not found!");
if (found == DNA_MAP.end()) error("key \"" + key + "\" not found!");

return (byte)found->second;
}
Expand All @@ -250,8 +251,7 @@ byte EnDecrypto::dna_pack_idx(const std::string& key) {
*/
u16 EnDecrypto::large_pack_idx(const std::string& key, const htbl_t& map) {
const auto found = map.find(key);
if (found == map.end())
error("key \"" + key + "\" not found!");
if (found == map.end()) error("key \"" + key + "\" not found!");

return (u16)found->second;
}
Expand Down Expand Up @@ -872,11 +872,11 @@ void EnDecrypto::unpack_seq(std::string& out, std::string::iterator& i) {
* @brief Shuffle a file (not FASTA/FASTQ)
*/
void EnDecrypto::shuffle_file() {
std::cerr << "This is not a FASTA/FASTQ file and we just encrypt it.\n";
std::cerr << "\"" << file_name(in_file)
<< "\" isn't FASTA/FASTQ. We just encrypt it.\n";

if (!stop_shuffle) {
const auto start =
std::chrono::high_resolution_clock::now(); // Start timer
const auto start = now(); // Start timer
std::thread arrThread[n_threads];

// Distribute file among threads, for shuffling
Expand All @@ -888,12 +888,9 @@ void EnDecrypto::shuffle_file() {
// Join partially shuffled files
join_shuffled_files();

const auto finish =
std::chrono::high_resolution_clock::now(); // Stop timer
std::chrono::duration<double> elapsed = finish - start; // sec

std::cerr << (verbose ? "Shuffling done" : "Done") << ", in " << std::fixed
<< std::setprecision(4) << elapsed.count() << " seconds.\n";
const auto finish = now(); // Stop timer
std::cerr << "\r" << bold("[+]") << " Shuffling done in "
<< hms(finish - start);
} else {
std::ifstream inFile(in_file);
std::ofstream pckdFile(PCKD_FNAME);
Expand Down Expand Up @@ -927,7 +924,10 @@ void EnDecrypto::shuffle_block(byte threadID) {
// Shuffle
if (!stop_shuffle) {
mutxEnDe.lock(); //--------------------------------------------------
if (shuffInProg) std::cerr << "Shuffling...\n";
if (shuffInProg) {
std::cerr << bold("[+]") << " Shuffling ...";
shuffle_timer = now();
}
shuffInProg = false;
mutxEnDe.unlock(); //------------------------------------------------

Expand Down Expand Up @@ -955,8 +955,7 @@ void EnDecrypto::unshuffle_file() {
if (c == (char)128) {
in.close();

const auto start =
std::chrono::high_resolution_clock::now(); // Start timer
const auto start = now(); // Start timer
std::thread arrThread[n_threads];

// Distribute file among threads, for unshuffling
Expand All @@ -971,13 +970,9 @@ void EnDecrypto::unshuffle_file() {
// Join partially unshuffled files
join_unshuffled_files();

const auto finish =
std::chrono::high_resolution_clock::now(); // Stop timer
std::chrono::duration<double> elapsed = finish - start; // sec

std::cerr << (verbose ? "Unshuffling done" : "Done") << ", in "
<< std::fixed << std::setprecision(4) << elapsed.count()
<< " seconds.\n";
const auto finish = now(); // Stop timer
std::cerr << "\r" << bold("[+]") << " Unshuffling done in "
<< hms(finish - start);
} else if (c == (char)129) {
std::cout << in.rdbuf();

Expand Down Expand Up @@ -1011,7 +1006,10 @@ void EnDecrypto::unshuffle_block(byte threadID) {
// Unshuffle
if (shuffled) {
mutxEnDe.lock(); //--------------------------------------------------
if (shuffInProg) std::cerr << "Unshuffling...\n";
if (shuffInProg) {
std::cerr << bold("[+]") << " Unshuffling ...";
shuffle_timer = now();
}
shuffInProg = false;
mutxEnDe.unlock(); //------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions src/endecrypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef CRYFA_ENDECRYPTO_H
#define CRYFA_ENDECRYPTO_H

#include <chrono>

#include "security.hpp"

namespace cryfa {
Expand Down Expand Up @@ -50,6 +52,7 @@ class EnDecrypto : public Security {
htbl_t HdrMap; /**< @brief Hdrs hash table */
htbl_t QsMap; /**< @brief QSs hash table */
u32 BlockLine; /**< @brief Max block lines */
std::chrono::time_point<std::chrono::high_resolution_clock> shuffle_timer;

void build_hash_tbl(htbl_t&, const std::string&, short);
void build_unpack_tbl(std::vector<std::string>&, const std::string&, u16);
Expand Down
67 changes: 44 additions & 23 deletions src/fasta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <iomanip> // setw, std::setprecision
#include <mutex>
#include <thread>

#include "string.hpp"
#include "time.hpp"
using namespace cryfa;

std::mutex mutxFA; /**< @brief Mutex */
Expand All @@ -21,17 +24,20 @@ std::mutex mutxFA; /**< @brief Mutex */
* @brief Compress
*/
void Fasta::compress() {
const auto start = std::chrono::high_resolution_clock::now(); // Start timer
if (!verbose) std::cerr << bold("[+]") << " Compacting ...";
const auto start = now(); // Start timer
std::thread arrThr[n_threads];
std::string headers;
packfa_s pkStruct; // Collection of inputs to pass to pack...

if (verbose) std::cerr << "Calculating number of different characters...\n";
if (verbose)
std::cerr << bold("[+]") << " Calculating no. unique characters ...";
// Gather different chars in all headers and max length in all bases
gather_h_bs(headers);
// Show number of different chars in headers -- ignore '>'=62
if (verbose)
std::cerr << "In headers, they are " << headers.length() << ".\n";
std::cerr << "\r" << bold("[+]") << " No. unique characters: headers => "
<< headers.length() << " \n";

// Set Hash table and pack function
set_hashTbl_packFn(pkStruct, headers);
Expand All @@ -42,16 +48,18 @@ void Fasta::compress() {
for (auto& thr : arrThr)
if (thr.joinable()) thr.join();

if (verbose) std::cerr << "Shuffling done!\n";
if (verbose){
std::cerr << "\r" << bold("[+]") << " Shuffling done in "
<< hms(now() - shuffle_timer);
std::cerr << bold("[+]") << " Compacting ...";
}

// Join partially packed and/or shuffled files
join_packed_files(headers, "", 'A', false);

const auto finish = std::chrono::high_resolution_clock::now(); // Stop timer
std::chrono::duration<double> elapsed = finish - start; // Dur. (sec)

std::cerr << (verbose ? "Compaction done" : "Done") << ", in " << std::fixed
<< std::setprecision(4) << elapsed.count() << " seconds.\n";
const auto finish = now(); // Stop timer
std::cerr << "\r" << bold("[+]") << " Compacting done in "
<< hms(finish - start);

// Cout encrypted content
encrypt();
Expand Down Expand Up @@ -157,7 +165,10 @@ void Fasta::pack(const packfa_s& pkStruct, byte threadID) {
// Shuffle
if (!stop_shuffle) {
mutxFA.lock(); //----------------------------------------------------
if (verbose && shuffInProg) std::cerr << "Shuffling...\n";
if (verbose && shuffInProg) {
std::cerr << bold("[+]") << " Shuffling ...";
shuffle_timer = now();
}
shuffInProg = false;
mutxFA.unlock(); //--------------------------------------------------

Expand Down Expand Up @@ -218,7 +229,9 @@ void Fasta::gather_h_bs(std::string& headers) {
* @brief Decompress
*/
void Fasta::decompress() {
const auto start = std::chrono::high_resolution_clock::now(); // Start timer
if (!verbose) std::cerr << bold("[+]") << " Decompressing ...";
const auto start = now(); // Start timer

char c; // Chars in file
std::string headers;
unpackfa_s upkStruct; // Collection of inputs to pass to unpack...
Expand All @@ -228,11 +241,12 @@ void Fasta::decompress() {
in.ignore(1); // Jump over decText[0]==(char) 127
in.get(c);
shuffled = (c == (char)128); // Check if file had been shuffled
if (verbose)
std::cerr << bold("[+]") << " Extracting no. unique characters ...";
while (in.get(c) && c != (char)254) headers += c;

if (verbose) // Show number of different chars in headers -- Ignore '>'=62
std::cerr << headers.length()
<< " different characters are included in headers.\n";
std::cerr << "\r" << bold("[+]") << " No. unique characters: headers => "
<< headers.length() << " \n";

// Header -- Set unpack table and unpack function
set_unpackTbl_unpackFn(upkStruct, headers);
Expand Down Expand Up @@ -265,7 +279,11 @@ void Fasta::decompress() {
for (auto& thr : arrThread)
if (thr.joinable()) thr.join();

if (verbose) std::cerr << "Unshuffling done!\n";
if (verbose) {
std::cerr << "\r" << bold("[+]") << " Unshuffling done in "
<< hms(now() - shuffle_timer);
std::cerr << bold("[+]") << " Decompressing ...";
}

// Close/delete decrypted file
in.close();
Expand All @@ -275,12 +293,9 @@ void Fasta::decompress() {
// Join partially unpacked files
join_unpacked_files();

const auto finish = std::chrono::high_resolution_clock::now(); // Stop timer
std::chrono::duration<double> elapsed = finish - start; // Dur. (sec)

std::cerr << (verbose ? "Decompression done" : "Done") << ", in "
<< std::fixed << std::setprecision(4) << elapsed.count()
<< " seconds.\n";
const auto finish = now(); // Stop timer
std::cerr << "\r" << bold("[+]") << " Decompressing done in "
<< hms(finish - start);
}

/**
Expand Down Expand Up @@ -356,7 +371,10 @@ void Fasta::unpack_hS(const unpackfa_s& upkStruct, byte threadID) {
// Unshuffle
if (shuffled) {
mutxFA.lock(); //----------------------------------------------------
if (verbose && shuffInProg) std::cerr << "Unshuffling...\n";
if (verbose && shuffInProg) {
std::cerr << bold("[+]") << " Unshuffling ...";
shuffle_timer = now();
}
shuffInProg = false;
mutxFA.unlock(); //--------------------------------------------------

Expand Down Expand Up @@ -421,7 +439,10 @@ void Fasta::unpack_hL(const unpackfa_s& upkStruct, byte threadID) {
// Unshuffle
if (shuffled) {
mutxFA.lock(); //----------------------------------------------------
if (verbose && shuffInProg) std::cerr << "Unshuffling...\n";
if (verbose && shuffInProg) {
std::cerr << bold("[+]") << " Unshuffling ...";
shuffle_timer = now();
}
shuffInProg = false;
mutxFA.unlock(); //--------------------------------------------------

Expand Down
Loading

0 comments on commit 64b582e

Please sign in to comment.