Skip to content

Commit

Permalink
Merge pull request #140 from jdomingu98/94-utils-enh-aplicar-patron-s…
Browse files Browse the repository at this point in the history
…ingleton

ENH singleton utils
  • Loading branch information
Atrujillo02 authored Apr 27, 2024
2 parents 9a330cd + ae2db62 commit f558f3d
Show file tree
Hide file tree
Showing 23 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion includes/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# include "Channel.hpp"
# include "User.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

# define MIN_PORT 1
# define MAX_PORT 65535
Expand Down
17 changes: 12 additions & 5 deletions includes/libsUtils.hpp → includes/Utils.hpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<std::string> 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<std::string> split(const std::string &s, char delim);
static bool isNumber(const std::string& s);
static std::string getCurrentDate();
};

#endif
2 changes: 1 addition & 1 deletion includes/parser/CommandParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# include "ModeParser.hpp"
# include "KickParser.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* Enumerates the commands that the server can receive.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/IParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# include "ACommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

class ACommand;

Expand Down
2 changes: 1 addition & 1 deletion includes/parser/InviteParser.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef INVITE_PARSER_HPP
# define INVITE_PARSER_HPP

# include "libsUtils.hpp"
# include "Utils.hpp"
# include "IParser.hpp"
# include "InviteCommand.hpp"

Expand Down
2 changes: 1 addition & 1 deletion includes/parser/JoinParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "JoinCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the JOIN command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/KickParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "KickCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the KICK command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/NickParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "NickCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the NICK command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/PartParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "PartCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the PART command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/PassParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "PassCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the PASS command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/PrivateMessageParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/QuitParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "QuitCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* A class that is responsible for parsing the QUIT command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/TopicParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# include "Channel.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the TOPIC command.
Expand Down
2 changes: 1 addition & 1 deletion includes/parser/UserParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# include "UserCommand.hpp"

# include "libsUtils.hpp"
# include "Utils.hpp"

/**
* An IParser implementation that is responsible for parsing the USER command.
Expand Down
2 changes: 1 addition & 1 deletion src/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 9 additions & 4 deletions src/libsUtils.cpp → src/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "libsUtils.hpp"
#include "Utils.hpp"

Utils::Utils() {}

Utils::~Utils() {}

/**
* Splits the string by the delimiter.
Expand All @@ -8,10 +12,11 @@
*
* @return The vector of strings.
*/
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> Utils::split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;

while (std::getline(ss, item, delim))
if (!item.empty())
elems.push_back(item);
Expand All @@ -25,7 +30,7 @@ std::vector<std::string> 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))
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ModeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/parser/CommandParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ IParser* CommandParser::getParser(std::string command) {
* @return The tokens of the command.
*/
std::vector<std::string> CommandParser::tokenize(const std::string& command) {
std::vector<std::string> tokens(split(command, ' '));
std::vector<std::string> tokens(Utils::split(command, ' '));
std::string token;

for (std::vector<std::string>::iterator it = tokens.begin(); it != tokens.end(); it++)
Expand Down
4 changes: 2 additions & 2 deletions src/parser/JoinParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ ACommand *JoinParser::parse(const std::vector<std::string>& tokens) {
throw NeedMoreParamsException("JOIN");

std::map<std::string, std::string> channels;
std::vector<std::string> channelsVec = split(tokens[1], ',');
std::vector<std::string> channelsVec = Utils::split(tokens[1], ',');
std::vector<std::string> 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())
Expand Down
4 changes: 2 additions & 2 deletions src/parser/KickParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ ACommand *KickParser::parse(const std::vector<std::string>& tokens) {
if (tokens.size() < 3 || tokens[1].empty() || tokens[2].empty())
throw NeedMoreParamsException("KICK");

std::vector<std::string> channels = split(tokens[1], ',');
std::vector<std::string> usersList = split(tokens[2], ',');
std::vector<std::string> channels = Utils::split(tokens[1], ',');
std::vector<std::string> usersList = Utils::split(tokens[2], ',');
std::vector<User> users;

for (size_t i = 0; i < channels.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/PartParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ACommand *PartParser::parse(const std::vector<std::string>& tokens) {
if (tokens.size() < 2)
throw NeedMoreParamsException("PART");

std::vector<std::string> channels = split(tokens[1], ',');
std::vector<std::string> channels = Utils::split(tokens[1], ',');

for (size_t i = 0; i < channels.size(); i++) {
if (channels[i][0] != '#' && channels[i][0] != '&')
Expand Down
2 changes: 1 addition & 1 deletion src/parser/PrivateMessageParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ACommand* PrivateMessageParser::parse(const std::vector<std::string>& tokens) {

if (tokens.size() < 2)
throw NoRecipientGivenException("PRIVMSG");
receivers = split(tokens[1], ',');
receivers = Utils::split(tokens[1], ',');
if (tokens.size() < 3)
throw NoTextToSendException();

Expand Down

0 comments on commit f558f3d

Please sign in to comment.