Skip to content

Commit

Permalink
Finish Release-3
Browse files Browse the repository at this point in the history
  • Loading branch information
Galfurian committed Nov 20, 2018
2 parents bf4a4a9 + 5393006 commit 309b559
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 26 deletions.
24 changes: 24 additions & 0 deletions include/colors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// @file colors.hpp

#pragma once

/* FOREGROUND */
#define KRST "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"

#define FRED(x) KRED x KRST
#define FGRN(x) KGRN x KRST
#define FYEL(x) KYEL x KRST
#define FBLU(x) KBLU x KRST
#define FMAG(x) KMAG x KRST
#define FCYN(x) KCYN x KRST
#define FWHT(x) KWHT x KRST

#define BOLD(x) "\x1B[1m" x KRST
#define UNDL(x) "\x1B[4m" x KRST
126 changes: 126 additions & 0 deletions include/compression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/// @file compression.hpp
/// @brief Functions used to compress a stream of characters.
/// @author Enrico Fraccaroli
/// @date Aug 23 2017

#pragma once
#ifdef COMPRESSION_ENABLED

#include <stdexcept>
#include <sstream>
#include <cstring>
#include <string>
#include <zlib.h>

namespace Compression
{
/// Size of the buffer used for the compression.
#define BUFFER_SIZE 32768

/// @brief Compress a STL string using zlib with given compression level and
/// return the binary data.
std::string compress(std::string const & str, int level = Z_BEST_COMPRESSION)
{
// Variable used to track return value from zlib.
int ret;
// Create the zlib's control structure.
z_stream zs;
std::memset(&zs, 0, sizeof(zs));
// Add 16 to MAX_WBITS to specify gzip format - it gets taken off again
// in defaultInit2.
if ((ret = deflateInit2(&zs, level,
Z_DEFLATED,
16 + MAX_WBITS,
MAX_MEM_LEVEL,
Z_DEFAULT_STRATEGY)) != Z_OK)
{
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
throw (std::runtime_error(oss.str()));
}
zs.next_in = (Bytef *) str.data();
// set the z_stream's input
zs.avail_in = static_cast<uInt>(str.size());
// Create and set the gzip header.
gz_header header;
memset(&header, 0, sizeof(header));
if ((ret = deflateSetHeader(&zs, &header)) != Z_OK)
{
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
throw (std::runtime_error(oss.str()));
}
char outbuffer[BUFFER_SIZE];
std::string outstring;
// retrieve the compressed bytes blockwise
do
{
zs.next_out = reinterpret_cast<Bytef *>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out)
{
// Append the block to the output string
outstring.append(outbuffer, zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END)
{
// An error occurred that was not EOF.
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
throw (std::runtime_error(oss.str()));
}
return outstring;
}

/// @brief Decompress an STL string using zlib and return the original data.
std::string decompress(std::string const & str)
{
// z_stream is zlib's control structure
z_stream zs;
std::memset(&zs, 0, sizeof(zs));

if (inflateInit(&zs) != Z_OK)
throw (std::runtime_error("inflateInit failed while decompressing."));

zs.next_in = (Bytef *) str.data();
zs.avail_in = static_cast<uInt>(str.size());

int ret;
char outbuffer[BUFFER_SIZE];
std::string outstring;

// get the decompressed bytes blockwise using repeated calls to inflate
do
{
zs.next_out = reinterpret_cast<Bytef *>(outbuffer);
zs.avail_out = sizeof(outbuffer);

ret = inflate(&zs, 0);

if (outstring.size() < zs.total_out)
{
outstring.append(outbuffer,
zs.total_out - outstring.size());
}

} while (ret == Z_OK);

inflateEnd(&zs);

if (ret != Z_STREAM_END)
{ // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") "
<< zs.msg;
throw (std::runtime_error(oss.str()));
}

return outstring;
}

} // namespace Compression

#endif
80 changes: 80 additions & 0 deletions include/utilities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// @file utilities.hpp
/// @author Enrico Fraccaroli
/// @date Jul 18 2017
/// @copyright
/// Copyright (c) 2017 Enrico Fraccaroli <enrico.fraccaroli@univr.it>

#pragma once

#include <string>
#include <map>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cassert>
#include <random>
#include <set>

namespace Utility
{

inline void create_dir(std::string const & path)
{
const int dir_err = system(("mkdir -p " + path).c_str());
if (dir_err == -1)
{
printf("Error creating directory %s!\n", path.c_str());
exit(1);
}
}

inline unsigned int elfHash(std::string const & s)
{
unsigned int hash = 0;
unsigned int x = 0;
for (const auto c : s)
{
hash = (hash << 4) + static_cast<unsigned int>(c);
if ((x = hash & 0xF0000000L) != 0)
hash ^= (x >> 24);
hash &= ~x;
}
return hash;
}

template<typename TDone, typename TTotal>
inline double getPercent(TDone done, TTotal total)
{
return (static_cast<double>(done) / static_cast<double>(total)) * 100.0;
}

template<typename TDone, typename TTotal>
inline std::string getPercentString(TDone done, TTotal total)
{
std::stringstream ss;
ss << std::setprecision(0) << std::fixed
<< std::setw(3) << std::right
<< getPercent(done, total) << "%";
return ss.str();
}

inline double to_double(std::string const & s)
{
std::string::size_type sz;
return std::stod(s, &sz);
}

template<typename T>
inline T to_number(std::string const & s)
{
std::string::size_type sz;
return static_cast<T>(std::stol(s, &sz));
}

/// @brief Provides the current date.
std::string getDateTime();

} // namespace Utility
Loading

0 comments on commit 309b559

Please sign in to comment.