Added parser class
This commit is contained in:
parent
86531035a8
commit
53a83308de
6 changed files with 105 additions and 17 deletions
7
Makefile
7
Makefile
|
|
@ -3,10 +3,10 @@
|
||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: omoudni <omoudni@student.42.fr> +#+ +:+ +#+ #
|
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
||||||
# Updated: 2025/05/14 23:33:58 by omoudni ### ########.fr #
|
# Updated: 2025/05/19 15:13:05 by omoudni ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
@ -26,7 +26,8 @@ SESSION = test-irc
|
||||||
# Sources
|
# Sources
|
||||||
SRC = sources/core/main.cpp \
|
SRC = sources/core/main.cpp \
|
||||||
sources/core/server.cpp \
|
sources/core/server.cpp \
|
||||||
sources/core/check.cpp
|
sources/core/check.cpp \
|
||||||
|
sources/core/parser.cpp \
|
||||||
|
|
||||||
|
|
||||||
INC_DIR = include/core \
|
INC_DIR = include/core \
|
||||||
|
|
|
||||||
17
diagram.puml
17
diagram.puml
|
|
@ -69,6 +69,22 @@ class Commandes {
|
||||||
+ USERNAME() : void
|
+ USERNAME() : void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
' ========================
|
||||||
|
' CLASS: Parser
|
||||||
|
' ========================
|
||||||
|
class Parser {
|
||||||
|
- _port : int
|
||||||
|
- _password : string
|
||||||
|
- _valid : bool
|
||||||
|
- _errorMsg : string
|
||||||
|
|
||||||
|
+ Parser(argc : int, argv : char**)
|
||||||
|
+ isValid() : bool
|
||||||
|
+ getPort() : int
|
||||||
|
+ getPassword() : string
|
||||||
|
+ getErrorMsg() : string
|
||||||
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' RELATIONS
|
' RELATIONS
|
||||||
' ========================
|
' ========================
|
||||||
|
|
@ -77,5 +93,6 @@ Server "1" o-- "*" Client : _clients
|
||||||
Client "1" o-- "*" Channel : _channels
|
Client "1" o-- "*" Channel : _channels
|
||||||
Channel "1" *-- "1" Client : _owner
|
Channel "1" *-- "1" Client : _owner
|
||||||
Channel "1" o-- "*" Client : _operators
|
Channel "1" o-- "*" Client : _operators
|
||||||
|
main ..> Parser : uses
|
||||||
|
|
||||||
@enduml
|
@enduml
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* core.hpp :+: :+: :+: */
|
/* core.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */
|
/* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/13 12:45:24 by rparodi ### ########.fr */
|
/* Updated: 2025/05/19 15:07:26 by omoudni ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -23,5 +23,6 @@
|
||||||
|
|
||||||
#include "color.hpp"
|
#include "color.hpp"
|
||||||
#include "server.hpp"
|
#include "server.hpp"
|
||||||
|
#include "parser.hpp"
|
||||||
|
|
||||||
unsigned short int valid_port(char *input);
|
unsigned short int valid_port(char *input);
|
||||||
|
|
|
||||||
30
include/core/parser.hpp
Normal file
30
include/core/parser.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/19 14:47:46 by omoudni #+# #+# */
|
||||||
|
/* Updated: 2025/05/19 15:14:31 by omoudni ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "core.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Parser {
|
||||||
|
private:
|
||||||
|
unsigned short int _port;
|
||||||
|
std::string _password;
|
||||||
|
bool _valid;
|
||||||
|
std::string _errorMsg;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Parser(int argc, char* argv[]);
|
||||||
|
bool isValid() const;
|
||||||
|
unsigned short int getPort() const;
|
||||||
|
const std::string& getPassword() const;
|
||||||
|
const std::string& getErrorMsg() const;
|
||||||
|
};
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.cpp :+: :+: :+: */
|
/* main.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42.fr> +#+ +:+ +#+ */
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:03:13 by rparodi #+# #+# */
|
/* Created: 2025/05/13 11:03:13 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/14 23:17:03 by omoudni ### ########.fr */
|
/* Updated: 2025/05/19 15:02:41 by omoudni ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,16 +14,12 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 3) {
|
Parser parser(argc, argv);
|
||||||
std::cerr << CLR_RED << "Usage: " << argv[0] << " <port> <password>" << CLR_RESET << std::endl;
|
if (!parser.isValid()) {
|
||||||
return 1;
|
std::cerr << CLR_RED << parser.getErrorMsg() << CLR_RESET << std::endl;
|
||||||
}
|
return 1;
|
||||||
unsigned short int tmp_port = valid_port(argv[1]);
|
}
|
||||||
if (tmp_port == 0) {
|
Server server(parser.getPort(), parser.getPassword());
|
||||||
std::cerr << CLR_RED << "Error: Invalid port: " << argv[1] << CLR_RESET << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
Server server(tmp_port, argv[2]);
|
|
||||||
server.showInfo();
|
server.showInfo();
|
||||||
server.start();
|
server.start();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
43
sources/core/parser.cpp
Normal file
43
sources/core/parser.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/19 14:48:52 by omoudni #+# #+# */
|
||||||
|
/* Updated: 2025/05/19 15:08:18 by omoudni ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parser.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
Parser::Parser(int argc, char* argv[]) : _port(0), _valid(false) {
|
||||||
|
if (argc != 3) {
|
||||||
|
_errorMsg = "Usage: ./ircserv <port> <password>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_port = valid_port(argv[1]);
|
||||||
|
if (_port == 0) {
|
||||||
|
_errorMsg = "Error: Invalid port: " + std::string(argv[1]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_password = argv[2];
|
||||||
|
if (_password.empty()) {
|
||||||
|
_errorMsg = "Error: Password cannot be empty";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < _password.size(); ++i) {
|
||||||
|
if (!isprint(_password[i])) {
|
||||||
|
_errorMsg = "Error: Password contains non-printable characters";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_valid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Parser::isValid() const { return _valid; }
|
||||||
|
unsigned short int Parser::getPort() const { return _port; }
|
||||||
|
const std::string& Parser::getPassword() const { return _password; }
|
||||||
|
const std::string& Parser::getErrorMsg() const { return _errorMsg; }
|
||||||
Loading…
Add table
Add a link
Reference in a new issue