aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise3/ThreadModerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week13/Exercise3/ThreadModerator.cpp')
-rw-r--r--week13/Exercise3/ThreadModerator.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/week13/Exercise3/ThreadModerator.cpp b/week13/Exercise3/ThreadModerator.cpp
new file mode 100644
index 0000000..047b0ec
--- /dev/null
+++ b/week13/Exercise3/ThreadModerator.cpp
@@ -0,0 +1,24 @@
+#include "ThreadModerator.h"
+#include <cstring>
+
+void ThreadModerator::RemoveMessage(Thread& thread, unsigned index) {
+ if (index >= thread.size) {
+ throw "Index out of range!";
+ }
+
+ delete[] thread.messages[index];
+ for (unsigned i = index; i < thread.size - 1; i++) {
+ thread.messages[i] = thread.messages[i-1];
+ }
+ thread.size--;
+}
+
+void ThreadModerator::RemoveByUser(Thread& thread, const User& poster) {
+ unsigned nameLen = strlen(poster.GetUsername());
+ for (unsigned i = 0; i < thread.size; i++) {
+ if (strncmp(thread.messages[i], poster.GetUsername(), nameLen) == 0) {
+ RemoveMessage(thread, i);
+ i--;
+ }
+ }
+}