Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
File renamed without changes.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
cmake-build-*/
.idea/
build/
.DS_Store
File renamed without changes.
23 changes: 18 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.16)
set (CMAKE_CXX_STANDARD 17)
project(messenger)

configure_file(CMakeLists.txt.in
Expand All @@ -11,8 +12,8 @@ execute_process(COMMAND ${CMAKE_COMMAND} --build .
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build)

set(CMAKE_C_FLAGS "-pedantic -fprofile-arcs -ftest-coverage -Wall -Werror -Wpedantic")
set(CMAKE_CXX_FLAGS "-pedantic -fprofile-arcs -ftest-coverage -Wall -Werror -Wpedantic")
set(CMAKE_C_FLAGS "-pedantic -Wall")
set(CMAKE_CXX_FLAGS "-pedantic -Wall")

enable_testing()

Expand All @@ -24,14 +25,26 @@ set(INCLUDE ${PROJECT_SOURCE_DIR}/project/include)
set(SOURCE ${PROJECT_SOURCE_DIR}/project/src)

# adding libs

add_library(Models STATIC
${INCLUDE}/Models.hpp
${SOURCE}/Models.cpp)

add_library(Auth STATIC
${PROJECT_SOURCE_DIR}/hash/md5.h
${PROJECT_SOURCE_DIR}/hash/md5.cpp
${INCLUDE}/Auth.hpp
${SOURCE}/Auth.cpp)
add_library(InMemoryDb STATIC
${INCLUDE}/InMemoryDb.hpp
${SOURCE}/InMemoryDb.cpp)
# end libs

file(GLOB prod_sources
"${INCLUDE}/*.hpp"
"${SOURCE}/main.cpp")

add_executable(main.out ${SOURCE}/main.cpp)
target_link_libraries(main.out Auth InMemoryDb Models)

file(GLOB tests "${PROJECT_SOURCE_DIR}/project/tests/*.cpp")
list(REMOVE_ITEM tests "${PROJECT_SOURCE_DIR}/project/tests/main.cpp")
Expand All @@ -42,7 +55,7 @@ foreach(file ${tests})
add_executable("${name}_tests"
${SOURCE}/${name}.cpp
${file}
"${PROJECT_SOURCE_DIR}/project/tests/main.cpp")
target_link_libraries("${name}_tests" gtest_main)
"${PROJECT_SOURCE_DIR}/project/tests/main.cpp" hash/md5.cpp)
target_link_libraries("${name}_tests" gtest_main gmock_main ${name} Models )
add_test(NAME ${name} COMMAND "${name}_tests")
endforeach()
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@

# SnippetChat
[![Build Status](https://travis-ci.com/tr0llex/SnippetChat.svg?branch=main)](https://travis-ci.com/tr0llex/SnippetChat)
[![codecov](https://codecov.io/gh/tr0llex/SnippetChat/branch/main/graph/badge.svg)](https://codecov.io/gh/tr0llex/SnippetChat)

#### Chat for programmers, the main feature of which is sending code snippets and launching them
Technopark Mail.ru 1st semester C++ project


Загрузить турунтул:
curl -L https://tarantool.io/VBBRNxO/release/2.7/installer.sh | bash
sudo apt-get -y install tarantool

После этого в терминале:

mkdir tarantool_sandbox
cd tarantool_sandbox

tarantool

box.cfg{listen = 3301}

s = box.schema.space.create('keys')

s = box.schema.space.create('unames', {id = 600})


s:format({{name = 'UserName', type = 'string'},{name = 'token', type = 'string'},{name = 'status', type = 'integer'}})

s:create_index('primary', {type = 'hash', parts = {'UserName'}})
s:create_index('secondary', {type = 'hash', parts = {'token'}})

s:insert{1, 'admin', 'AAA', 0}
s:insert{2, 'test_1', 'BBA', 1}
s:insert{3, 'test_2', 'BBQ', 1}

=иногда турунтул засыпает, чтобы фиксить надо:

tarantool

box.cfg{listen = 3301}

Замечание:
sudo lsof -i :3301
проверить что порт свободен, нет - kill по занимающим
37 changes: 37 additions & 0 deletions hash/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
override CXXFLAGS += -g -Wall -O2 -pipe -Wextra -Wno-sign-compare
INC += -I.
LDFLAGS :=
CC := cc
CXX := c++

DEPS := hashids.o
BINARIES := hashidsxx

all: $(BINARIES)

%.o: %.cpp %.h
$(CXX) -std=c++11 $(CXXFLAGS) $(INC) -c $< -o $@

hashidsxx: hashidsxx.cpp $(DEPS)
$(CXX) $(CXXFLAGS) $(INC) $< -o $@ $(DEPS)

test: test.out
./test.out

test.out: test.cpp libgtest.a $(DEPS)
$(CXX) -std=c++11 $(CXXFLAGS) $(INC) -Igtest-1.7.0/include -o $@ $< libgtest.a $(DEPS) -pthread

gtest-1.7.0:
wget -q https://github.com/google/googletest/archive/release-1.7.0.zip
unzip -qq release-1.7.0.zip
rm release-1.7.0.zip
mv googletest-release-1.7.0 gtest-1.7.0

libgtest.a: gtest-1.7.0
$(CXX) -std=c++11 -I gtest-1.7.0/include -I gtest-1.7.0 -c gtest-1.7.0/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o

.PHONY: clean

clean:
rm -rf $(BINARIES) $(DEPS) test.out
235 changes: 235 additions & 0 deletions hash/hashids.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/**
* Hashidsxx (c) 2014 Toon Schoenmakers
*
* https://github.com/schoentoon/hashidsxx
* hashidsxx may be freely distributed under the MIT license.
*/

#include "hashids.h"

#include <algorithm>

#define RATIO_SEPARATORS 3.5
#define RATIO_GUARDS 12

#include <iterator>
#include <iostream>
#include <sstream>
#include <numeric>

namespace hashidsxx {

const static std::string separators("cfhistuCFHISTU");

Hashids::Hashids(const std::string &salt, unsigned int min_length,
const std::string &alphabet)
: _salt(salt), _alphabet(alphabet), _min_length(min_length), _separators(),
_guards() {
std::for_each(separators.begin(), separators.end(), [this](char c) {
if (_alphabet.find(c) != std::string::npos)
_separators.push_back(c);
});
_alphabet.erase(
std::remove_if(_alphabet.begin(), _alphabet.end(), [this](char c) {
return _separators.find(c) != std::string::npos;
}), _alphabet.end());
if (_alphabet.size() + _separators.size() < 16)
throw std::runtime_error(
"Alphabet must contain at least 16 unique characters");

_separators = _reorder(_separators, _salt);

std::size_t min_separators =
(int)std::ceil((float)_alphabet.length() / RATIO_SEPARATORS);

if (_separators.empty() || _separators.length() < min_separators) {
if (min_separators == 1)
min_separators = 2;
if (min_separators > _separators.length()) {
int split_at = min_separators - _separators.length();
_separators.append(_alphabet.substr(0, split_at));
_alphabet = _alphabet.substr(split_at);
};
};

_alphabet = _reorder(_alphabet, _salt);
int num_guards = (int)std::ceil((float)_alphabet.length() / RATIO_GUARDS);

if (_alphabet.length() < 3) {
_guards = _separators.substr(0, num_guards);
_separators = _separators.substr(num_guards);
} else {
_guards = _alphabet.substr(0, num_guards);
_alphabet = _alphabet.substr(num_guards);
};
}

Hashids::Hashids(const Hashids &that)
: _salt(that._salt), _alphabet(that._alphabet),
_min_length(that._min_length), _separators(that._separators),
_guards(that._guards) {}

#if __cplusplus >= 201103
Hashids::Hashids(Hashids &&that)
: _salt(std::move(that._salt)), _alphabet(std::move(that._alphabet)),
_min_length(that._min_length), _separators(std::move(that._separators)),
_guards(std::move(that._guards)) {}
#endif

Hashids::~Hashids() {}

std::string &Hashids::_reorder(std::string &input,
const std::string &salt) const {
if (salt.empty())
return input;

int i = input.length() - 1;
int index = 0;
int integer_sum = 0;

while (i > 0) {
index %= salt.length();
int integer = salt[index];
integer_sum += integer;
unsigned int j = (integer + index + integer_sum) % i;

std::swap(input[i], input[j]);

--i;
++index;
};

return input;
}

std::string Hashids::_reorder_norewrite(const std::string &input,
const std::string &salt) const {
std::string output(input);
return _reorder(output, salt);
}

std::string Hashids::_hash(uint64_t number, const std::string &alphabet) const {
std::string output;
do {
output.push_back(alphabet[number % alphabet.size()]);
number /= alphabet.size();
} while(number != 0);
return std::string(output.rbegin(), output.rend());
}

std::string Hashids::encodeHex(const std::string &input) const {
std::vector<uint64_t> numbers;
std::string buffer;
std::string hex("0123456789abcdefABCDEF");

for (char c : input) {
if (hex.find_first_of(c) != std::string::npos)
buffer.push_back(c);
if (buffer.size() == 12) {
numbers.push_back(std::stoull("1" + buffer, nullptr, 16));
buffer.clear();
}
}
if (!buffer.empty())
numbers.push_back(std::stoull("1" + buffer, nullptr, 16));

return encode(numbers.begin(), numbers.end());
}

std::string Hashids::decodeHex(const std::string &input) const {
std::stringstream output;
std::stringstream hexbuf;
for (uint64_t number : decode(input)) {
hexbuf << std::hex << number;
output << hexbuf.str().substr(1);
hexbuf.str(std::string());
}
return output.str();
}

uint64_t Hashids::_unhash(const std::string &input,
const std::string &alphabet) const {
return std::accumulate(
input.begin(),
input.end(),
static_cast<uint64_t>(0),
[&alphabet](const uint64_t &carry, const char &item){
return carry * alphabet.size() + alphabet.find(item);
}
);
}

void Hashids::_ensure_length(std::string &output, std::string &alphabet,
int values_hash) const {
int guard_index = (values_hash + output[0]) % _guards.size();
output.insert(output.begin(), _guards[guard_index]);

if (output.size() < _min_length) {
guard_index = (values_hash + output[2]) % _guards.size();
output.push_back(_guards[guard_index]);
};

int split_at = alphabet.size() / 2;
while (output.size() < _min_length) {
alphabet = _reorder_norewrite(alphabet, alphabet);

output = alphabet.substr(split_at) + output + alphabet.substr(0, split_at);

int excess = output.size() - _min_length;
if (excess > 0) {
int from_index = excess / 2;
output = output.substr(from_index, _min_length);
};
};
}

std::vector<std::string> Hashids::_split(const std::string &input,
const std::string &splitters) const {
std::vector<std::string> parts;
std::string tmp;

for (char c : input) {
if (splitters.find(c) != std::string::npos) {
parts.push_back(tmp);
tmp.clear();
} else
tmp.push_back(c);
};
if (!tmp.empty())
parts.push_back(tmp);

return parts;
}

std::vector<uint64_t> Hashids::decode(const std::string &input) const {
std::vector<uint64_t> output;

std::vector<std::string> parts = _split(input, _guards);

std::string hashid = parts[0];
if (parts.size() >= 2)
hashid = parts[1];

if (hashid.empty())
return output;

output.reserve(parts.size());

char lottery = hashid[0];
std::string alphabet(_alphabet);

hashid.erase(hashid.begin());

std::vector<std::string> hash_parts = _split(hashid, _separators);
for (const std::string &part : hash_parts) {
std::string alphabet_salt = (lottery + _salt + alphabet);
alphabet_salt = alphabet_salt.substr(0, alphabet.size());

alphabet = _reorder(alphabet, alphabet_salt);

output.push_back(_unhash(part, alphabet));
};

return output;
}
};
Loading