diff --git a/Makefile b/Makefile index 5195723..d61fd67 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ PARSER_PREFIXS = Command User Pass Nick Quit PrivateMessage Join Part Invite Mod PARSER_FILES = $(addsuffix Parser, $(PARSER_PREFIXS)) PARSER_SRCS = $(addprefix $(PARSER_DIR), $(PARSER_FILES)) -FILES = main Server User Channel libsUtils Logger +FILES = main Server User Channel Utils Logger SRCS_PATHS = $(addprefix $(SRC_DIR)/, $(FILES)) $(CMD_SRCS) $(PARSER_SRCS) SRCS = $(addsuffix .cpp, $(SRCS_PATHS)) diff --git a/includes/Server.hpp b/includes/Server.hpp index e5db835..5c69dd0 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -14,7 +14,7 @@ # include "Channel.hpp" # include "User.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" # define MIN_PORT 1 # define MAX_PORT 65535 diff --git a/includes/libsUtils.hpp b/includes/Utils.hpp similarity index 94% rename from includes/libsUtils.hpp rename to includes/Utils.hpp index a96fd7a..38d324f 100644 --- a/includes/libsUtils.hpp +++ b/includes/Utils.hpp @@ -1,5 +1,5 @@ -#ifndef LIBS_UTILS_HPP -# define LIBS_UTILS_HPP +#ifndef UTILS_HPP +# define UTILS_HPP /** * A file that contains utility consts and libraries that are used along the project. @@ -81,8 +81,15 @@ # define INVITE_MSG(nickname, username, hostname, invitedUser, channel) USER_ID(nickname, username, hostname) + " INVITE " + (invitedUser) + " " + (channel) # define MODE_MSG(nickname, username, hostname, channel, flag, modeParams) USER_ID(nickname, username, hostname) + " MODE " + (channel) + " " + (flag) + " " + (modeParams) -std::vector split(const std::string &s, char delim); -bool isNumber(const std::string& s); -std::string getCurrentDate(); +class Utils { + private: + Utils(); + ~Utils(); + + public: + static std::vector split(const std::string &s, char delim); + static bool isNumber(const std::string& s); + static std::string getCurrentDate(); +}; #endif \ No newline at end of file diff --git a/includes/parser/CommandParser.hpp b/includes/parser/CommandParser.hpp index 5b388ff..2036fea 100644 --- a/includes/parser/CommandParser.hpp +++ b/includes/parser/CommandParser.hpp @@ -17,7 +17,7 @@ # include "ModeParser.hpp" # include "KickParser.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * Enumerates the commands that the server can receive. diff --git a/includes/parser/IParser.hpp b/includes/parser/IParser.hpp index e40c034..555a3b6 100644 --- a/includes/parser/IParser.hpp +++ b/includes/parser/IParser.hpp @@ -3,7 +3,7 @@ # include "ACommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" class ACommand; diff --git a/includes/parser/InviteParser.hpp b/includes/parser/InviteParser.hpp index 0f2b06e..5458f5a 100644 --- a/includes/parser/InviteParser.hpp +++ b/includes/parser/InviteParser.hpp @@ -1,7 +1,7 @@ #ifndef INVITE_PARSER_HPP # define INVITE_PARSER_HPP -# include "libsUtils.hpp" +# include "Utils.hpp" # include "IParser.hpp" # include "InviteCommand.hpp" diff --git a/includes/parser/JoinParser.hpp b/includes/parser/JoinParser.hpp index dda67f8..1129831 100644 --- a/includes/parser/JoinParser.hpp +++ b/includes/parser/JoinParser.hpp @@ -5,7 +5,7 @@ # include "JoinCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the JOIN command. diff --git a/includes/parser/KickParser.hpp b/includes/parser/KickParser.hpp index 5d36681..fdca150 100644 --- a/includes/parser/KickParser.hpp +++ b/includes/parser/KickParser.hpp @@ -5,7 +5,7 @@ # include "KickCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the KICK command. diff --git a/includes/parser/NickParser.hpp b/includes/parser/NickParser.hpp index 63ea340..76b5502 100644 --- a/includes/parser/NickParser.hpp +++ b/includes/parser/NickParser.hpp @@ -5,7 +5,7 @@ # include "NickCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the NICK command. diff --git a/includes/parser/PartParser.hpp b/includes/parser/PartParser.hpp index c7fb83a..5ebe20f 100644 --- a/includes/parser/PartParser.hpp +++ b/includes/parser/PartParser.hpp @@ -5,7 +5,7 @@ # include "PartCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the PART command. diff --git a/includes/parser/PassParser.hpp b/includes/parser/PassParser.hpp index 763d191..d8c64a0 100644 --- a/includes/parser/PassParser.hpp +++ b/includes/parser/PassParser.hpp @@ -5,7 +5,7 @@ # include "PassCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the PASS command. diff --git a/includes/parser/PrivateMessageParser.hpp b/includes/parser/PrivateMessageParser.hpp index 4e90e09..3a5fa63 100644 --- a/includes/parser/PrivateMessageParser.hpp +++ b/includes/parser/PrivateMessageParser.hpp @@ -4,7 +4,7 @@ # include "IParser.hpp" # include "PrivateMessageCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * A class that represents the parser for the private message command. diff --git a/includes/parser/QuitParser.hpp b/includes/parser/QuitParser.hpp index 726abaa..5d5b56e 100644 --- a/includes/parser/QuitParser.hpp +++ b/includes/parser/QuitParser.hpp @@ -5,7 +5,7 @@ # include "QuitCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * A class that is responsible for parsing the QUIT command. diff --git a/includes/parser/TopicParser.hpp b/includes/parser/TopicParser.hpp index 64e699b..da95205 100644 --- a/includes/parser/TopicParser.hpp +++ b/includes/parser/TopicParser.hpp @@ -7,7 +7,7 @@ # include "Channel.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the TOPIC command. diff --git a/includes/parser/UserParser.hpp b/includes/parser/UserParser.hpp index 3799beb..3c2cfd6 100644 --- a/includes/parser/UserParser.hpp +++ b/includes/parser/UserParser.hpp @@ -5,7 +5,7 @@ # include "UserCommand.hpp" -# include "libsUtils.hpp" +# include "Utils.hpp" /** * An IParser implementation that is responsible for parsing the USER command. diff --git a/src/User.cpp b/src/User.cpp index e30a709..6c4eb57 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -187,7 +187,7 @@ void User::makeRegistration() { throw PasswordMismatchException(); this->_registered = true; - std::string date = getCurrentDate(); + std::string date = Utils::getCurrentDate(); std::string channelModes = "iklot"; Server& server = Server::getInstance(); diff --git a/src/libsUtils.cpp b/src/Utils.cpp similarity index 81% rename from src/libsUtils.cpp rename to src/Utils.cpp index f085ea0..cce24c1 100644 --- a/src/libsUtils.cpp +++ b/src/Utils.cpp @@ -1,4 +1,8 @@ -#include "libsUtils.hpp" +#include "Utils.hpp" + +Utils::Utils() {} + +Utils::~Utils() {} /** * Splits the string by the delimiter. @@ -8,10 +12,11 @@ * * @return The vector of strings. */ -std::vector split(const std::string &s, char delim) { +std::vector Utils::split(const std::string &s, char delim) { std::vector elems; std::stringstream ss(s); std::string item; + while (std::getline(ss, item, delim)) if (!item.empty()) elems.push_back(item); @@ -25,7 +30,7 @@ std::vector split(const std::string &s, char delim) { * * @return `true` if the string is a number, `false` otherwise. */ -bool isNumber(const std::string& s) { +bool Utils::isNumber(const std::string& s) { std::string::const_iterator it = s.begin(); while (it != s.end() && std::isdigit(*it)) @@ -38,7 +43,7 @@ bool isNumber(const std::string& s) { * * @return The string with the current date. */ -std::string getCurrentDate() { +std::string Utils::getCurrentDate() { std::time_t t = std::time(0); std::tm* now = std::localtime(&t); char buffer[100]; diff --git a/src/commands/ModeCommand.cpp b/src/commands/ModeCommand.cpp index 215016f..c5c71a9 100644 --- a/src/commands/ModeCommand.cpp +++ b/src/commands/ModeCommand.cpp @@ -128,7 +128,7 @@ void ModeCommand::channelOperator() { void ModeCommand::userLimit() { if (_modeParams == NONE && _plus) throw NeedMoreParamsException("MODE"); - if (!isNumber(_modeParams)) + if (!Utils::isNumber(_modeParams)) return ; int numUsers = std::atoi(_modeParams.c_str()); diff --git a/src/parser/CommandParser.cpp b/src/parser/CommandParser.cpp index 67c91ab..a647273 100644 --- a/src/parser/CommandParser.cpp +++ b/src/parser/CommandParser.cpp @@ -71,7 +71,7 @@ IParser* CommandParser::getParser(std::string command) { * @return The tokens of the command. */ std::vector CommandParser::tokenize(const std::string& command) { - std::vector tokens(split(command, ' ')); + std::vector tokens(Utils::split(command, ' ')); std::string token; for (std::vector::iterator it = tokens.begin(); it != tokens.end(); it++) diff --git a/src/parser/JoinParser.cpp b/src/parser/JoinParser.cpp index 1be32ea..e71e95c 100644 --- a/src/parser/JoinParser.cpp +++ b/src/parser/JoinParser.cpp @@ -19,11 +19,11 @@ ACommand *JoinParser::parse(const std::vector& tokens) { throw NeedMoreParamsException("JOIN"); std::map channels; - std::vector channelsVec = split(tokens[1], ','); + std::vector channelsVec = Utils::split(tokens[1], ','); std::vector keysVec; if (tokens.size() == 3) - keysVec = split(tokens[2], ','); + keysVec = Utils::split(tokens[2], ','); for (size_t i = 0; i < channelsVec.size(); i++) { if (channelsVec[i].empty()) diff --git a/src/parser/KickParser.cpp b/src/parser/KickParser.cpp index 324f825..4f0579f 100644 --- a/src/parser/KickParser.cpp +++ b/src/parser/KickParser.cpp @@ -18,8 +18,8 @@ ACommand *KickParser::parse(const std::vector& tokens) { if (tokens.size() < 3 || tokens[1].empty() || tokens[2].empty()) throw NeedMoreParamsException("KICK"); - std::vector channels = split(tokens[1], ','); - std::vector usersList = split(tokens[2], ','); + std::vector channels = Utils::split(tokens[1], ','); + std::vector usersList = Utils::split(tokens[2], ','); std::vector users; for (size_t i = 0; i < channels.size(); i++) { diff --git a/src/parser/PartParser.cpp b/src/parser/PartParser.cpp index 11835f3..ac780e7 100644 --- a/src/parser/PartParser.cpp +++ b/src/parser/PartParser.cpp @@ -18,7 +18,7 @@ ACommand *PartParser::parse(const std::vector& tokens) { if (tokens.size() < 2) throw NeedMoreParamsException("PART"); - std::vector channels = split(tokens[1], ','); + std::vector channels = Utils::split(tokens[1], ','); for (size_t i = 0; i < channels.size(); i++) { if (channels[i][0] != '#' && channels[i][0] != '&') diff --git a/src/parser/PrivateMessageParser.cpp b/src/parser/PrivateMessageParser.cpp index 622b49e..6a6dc95 100644 --- a/src/parser/PrivateMessageParser.cpp +++ b/src/parser/PrivateMessageParser.cpp @@ -19,7 +19,7 @@ ACommand* PrivateMessageParser::parse(const std::vector& tokens) { if (tokens.size() < 2) throw NoRecipientGivenException("PRIVMSG"); - receivers = split(tokens[1], ','); + receivers = Utils::split(tokens[1], ','); if (tokens.size() < 3) throw NoTextToSendException();