Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
diff --git a/допълнително упражнение/Source.cpp b/допълнително упражнение/Source.cpp
new file mode 100644
index 0000000..8845349
--- /dev/null
+++ b/допълнително упражнение/Source.cpp
@@ -0,0 +1,12 @@
+#include<iostream>
+#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/допълнително упражнение/User.cpp b/допълнително упражнение/User.cpp
new file mode 100644
index 0000000..7f5a53d
--- /dev/null
+++ b/допълнително упражнение/User.cpp
@@ -0,0 +1,97 @@
+#include "User.h"
+#include<cstring>
+#include<fstream>
+#include<iostream>
+
+#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/допълнително упражнение/User.h b/допълнително упражнение/User.h
new file mode 100644
index 0000000..ab729cd
--- /dev/null
+++ b/допълнително упражнение/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/допълнително упражнение/chatSystem.cpp b/допълнително упражнение/chatSystem.cpp
new file mode 100644
index 0000000..416b1f8
--- /dev/null
+++ b/допълнително упражнение/chatSystem.cpp
@@ -0,0 +1,80 @@
+#include "chatSystem.h"
+#include<cstring>
+#include<fstream>
+
+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/допълнително упражнение/chatSystem.h b/допълнително упражнение/chatSystem.h
new file mode 100644
index 0000000..cf7332a
--- /dev/null
+++ b/допълнително упражнение/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();
+
+};
+