aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise3/ThreadModerator.cpp
blob: 047b0ec23a7cf6759f58678124a131e67cee78e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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--;
		}
	}
}