-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.cpp
More file actions
69 lines (55 loc) · 2.27 KB
/
Message.cpp
File metadata and controls
69 lines (55 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by matin on 12/30/25.
//
#include "Message.h"
#include "ConsoleStyle.h"
#include <sstream>
#include <iostream>
Message::Message(int sId) : senderId(sId), senderUsername(""), timestamp() {}
Message::Message(int sId, const string& sUsername) : senderId(sId), senderUsername(sUsername), timestamp() {}
Message::Message(int sId, const DateTime& time) : senderId(sId), senderUsername(""), timestamp(time) {}
Message::Message(int sId, const string& sUsername, const DateTime& time) : senderId(sId), senderUsername(sUsername), timestamp(time) {}
Message* Message::deserialize(const string& line) {
istringstream iss(line);
string token;
vector<string> tokens;
while (getline(iss, token, ';')) {
tokens.push_back(token);
}
if (tokens.size() < 4) {
return nullptr;
}
string type = tokens[0];
int sId = stoi(tokens[1]);
DateTime time = DateTime::fromString(tokens[2]);
if (type == "TEXT") {
return new TextMessage(sId, time, tokens[3]);
} else if (type == "VOICE") {
return new VoiceMessage(sId, time, stoi(tokens[3]));
}
return nullptr;
}
TextMessage::TextMessage(int sId, const string& sUsername, const string& content)
: Message(sId, sUsername), text(content) {}
TextMessage::TextMessage(int sId, const DateTime& time, const string& content)
: Message(sId, time), text(content) {}
string TextMessage::serialize() const {
return "TEXT;" + to_string(senderId) + ";" + timestamp.toString() + ";" + text;
}
void TextMessage::display() const {
cout << CYAN << "[" << timestamp.toString() << "] " << RESET;
cout << BOLD << senderUsername << ": " << RESET;
cout << text << endl;
}
VoiceMessage::VoiceMessage(int sId, const string& sUsername, int dur)
: Message(sId, sUsername), duration(dur) {}
VoiceMessage::VoiceMessage(int sId, const DateTime& time, int dur)
: Message(sId, time), duration(dur) {}
string VoiceMessage::serialize() const {
return "VOICE;" + to_string(senderId) + ";" + timestamp.toString() + ";" + to_string(duration);
}
void VoiceMessage::display() const {
cout << CYAN << "[" << timestamp.toString() << "] " << RESET;
cout << BOLD << senderUsername << ": " << RESET;
cout << MAGENTA << "[Voice message - " << duration << " seconds]" << RESET << endl;
}