From 8c11fe93cd21fe2a7586959f85d79dcf9812968c Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com> Date: Sat, 2 Apr 2022 18:54:56 +0300 Subject: add solutions to task2 --- .../Source.cpp" | 12 +++ .../User.cpp" | 97 ++++++++++++++++++++++ .../User.h" | 26 ++++++ .../chatSystem.cpp" | 80 ++++++++++++++++++ .../chatSystem.h" | 31 +++++++ 5 files changed, 246 insertions(+) create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" new file mode 100644 index 0000000..8845349 --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" @@ -0,0 +1,12 @@ +#include +#include"chatSystem.h" + +int main() { + chatSystem data; + + data.addUser({ "Ivan", "1234", "iv@abv.bg" }); + data.addUser({ "Qvor", "4321", "qv@abv.bg" }); + + data.sendMessage("last example", "Ivan", "Qvor"); + data.viewMessages("Qvor", "Ivan"); +} \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" new file mode 100644 index 0000000..7f5a53d --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" @@ -0,0 +1,97 @@ +#include "User.h" +#include +#include +#include + +#pragma warning(disable : 4996) + +void generatePath(char* path, const char* bigger, const char* smaller) { + strcpy(path, bigger); + strcat(path, "-"); + strcat(path, smaller); + strcat(path, ".bat"); +} + +void User::createFileName(char* fileName, const User& other) const { + if (strcmp(name, other.name) == 1) { + generatePath(fileName, name, other.name); + } + else { + generatePath(fileName, other.name, name); + } +} + +User::User() : name(""), password(""), email(""){} + +User::User(const char* n, const char* pass, const char* mail) { + if (strlen(n) > NAME_LEN || strlen(pass) > PASS_LEN || strlen(mail) > MAIL_LEN) { + strcpy(name, ""); + strcpy(password, ""); + strcpy(email, ""); + + return; + } + + strcpy(name, n); + strcpy(password, pass); + strcpy(email, mail); +} + +void User::sendMessage(const char* message, const User& other) const { + char filePath[50]; + createFileName(filePath, other); + + std::ofstream outFile(filePath, std::ios::binary | std::ios::app); + + if (!outFile.is_open()) + return; + + outFile.write(name, sizeof(name)); + + int length = strlen(message); + + outFile.write((const char*)&length, sizeof(length)); + + outFile.write(message, length); + + outFile.close(); +} + +void User::viewMessage(const User& other) const { + char filePath[50]; + createFileName(filePath, other); + + std::ifstream inFile(filePath, std::ios::binary); + + if (!inFile.is_open()) + return; + + char buff[NAME_LEN + 1]; + + while (!inFile.eof()) { + inFile.read(buff, sizeof(buff)); + + if (inFile.eof()) + return; + + int length = 0; + + inFile.read((char*)&length, sizeof(length)); + + char* msg = new char[length + 1]; + + inFile.read(msg, length); + + msg[length] = '\0'; + + std::cout << buff << ": " << msg << std::endl; + + delete[] msg; + } + + inFile.close(); +} + +const char* User::getName() const { + return name; +} diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" new file mode 100644 index 0000000..ab729cd --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" @@ -0,0 +1,26 @@ +#pragma once + +const int NAME_LEN = 20; +const int MAIL_LEN = 12; +const int PASS_LEN = 8; + +class User { +private: + // N символа за име +1 за терминираща нула! + char name[NAME_LEN + 1]; + char password[PASS_LEN + 1]; + char email[MAIL_LEN + 1]; + + void createFileName(char* fileName, const User& other) const; +public: + User(); + + User(const char* n, const char* pass, const char* email); + + void sendMessage(const char* message, const User& u) const; + + void viewMessage(const User& u) const; + + const char* getName() const; +}; + diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" new file mode 100644 index 0000000..416b1f8 --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" @@ -0,0 +1,80 @@ +#include "chatSystem.h" +#include +#include + +void chatSystem::save() const { + std::ofstream outFile(FILE_PATH, std::ios::binary); + + outFile.write((const char*)this, sizeof(chatSystem)); +} + +void chatSystem::read() { + std::ifstream inFile(FILE_PATH, std::ios::binary); + + inFile.read((char*)this, sizeof(chatSystem)); +} + +void chatSystem::searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const { + for (int i = 0; i < size; i++) { + if (strcmp(fstName, data[i].getName()) == 0) { + firstIndex = i; + } + + if (strcmp(sndName, data[i].getName()) == 0) { + secondIndex = i; + } + + if (firstIndex != -1 && secondIndex != -1) + break; + } +} + +chatSystem::chatSystem() { + size = 0; + read(); +} + +void chatSystem::addUser(const User& arg) { + if (size > 99) + return; + + for (int i = 0; i < size; i++) { + if (strcmp(arg.getName(), data[i].getName()) == 0) { + return; + } + } + + data[size++] = arg; +} + +void chatSystem::sendMessage(const char* msg, const char* fstName, const char* sndName) const { + int firstIndex = -1; + int secondIndex = -1; + + searchIndexes(firstIndex, secondIndex, fstName, sndName); + + if (firstIndex == -1 || secondIndex == -1) + return; + + data[firstIndex].sendMessage(msg, data[secondIndex]); +} + +void chatSystem::viewMessages(const char* fstName, const char* sndName) const { + int firstIndex = -1; + int secondIndex = -1; + + searchIndexes(firstIndex, secondIndex, fstName, sndName); + + if (firstIndex == -1 || secondIndex == -1) + return; + + data[firstIndex].viewMessage(data[secondIndex]); +} + +int chatSystem::getSize() const { + return size; +} + +chatSystem::~chatSystem() { + save(); +} diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" new file mode 100644 index 0000000..cf7332a --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" @@ -0,0 +1,31 @@ +#pragma once +#include"User.h" + +const char FILE_PATH[] = "database.bat"; + +class chatSystem { +private: + User data[100]; + size_t size; + + void save() const; + + void read(); + + void searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const; + +public: + chatSystem(); + + void addUser(const User& arg); + + void sendMessage(const char* msg, const char* fstName, const char* sndName) const; + + void viewMessages(const char* sender, const char* reciever) const; + + int getSize() const; + + ~chatSystem(); + +}; + -- cgit v1.3.1-23-gd82d