Skip to content

Commit

Permalink
🚧 Database initializer - ex00
Browse files Browse the repository at this point in the history
  • Loading branch information
okbrandon committed Feb 15, 2024
1 parent 6b8e3ce commit efe0762
Show file tree
Hide file tree
Showing 5 changed files with 1,861 additions and 0 deletions.
73 changes: 73 additions & 0 deletions ex00/BitcoinExchange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsoubaig <bsoubaig@student.42nice.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/21 14:54:45 by bsoubaig #+# #+# */
/* Updated: 2023/12/21 17:59:43 by bsoubaig ### ########.fr */
/* */
/* ************************************************************************** */

#include "BitcoinExchange.hpp"

/* Constructors & Destructors */
BitcoinExchange::BitcoinExchange(void) {

}

BitcoinExchange::BitcoinExchange(const BitcoinExchange &origin) {
*this = origin;
}

BitcoinExchange::~BitcoinExchange(void) {}

/* Private functions */
int BitcoinExchange::_ft_stoi(std::string &str) {
int i;

std::istringstream(str) >> i;
return (i);
}

float BitcoinExchange::_ft_stof(std::string &str) {
float f;

std::istringstream(str) >> f;
return (f);
}

/* Exceptions */
const char* BitcoinExchange::FileReadException::what() const throw() {
return ("An error occurred while reading the database.");
}

/* Functions */
void BitcoinExchange::initDatabase(std::string fileName) {
std::ifstream infile(fileName.c_str());
std::string line;

if (infile.fail() || !infile.is_open())
throw BitcoinExchange::FileReadException();
std::getline(infile, line);
while (std::getline(infile, line)) {
std::string date;
std::string rate;
size_t delimiter;

delimiter = line.find(",");
date = line.substr(0, delimiter);
rate = line.substr(delimiter + 1);
if (date.empty() || rate.empty())
throw BitcoinExchange::FileReadException();
this->_database.insert(std::pair<std::string, float>(date, _ft_stof(rate)));
}
infile.close();
}

/* Overloaded operators */
BitcoinExchange &BitcoinExchange::operator=(const BitcoinExchange &origin) {
this->_database = origin._database;
return (*this);
}
60 changes: 60 additions & 0 deletions ex00/BitcoinExchange.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsoubaig <bsoubaig@student.42nice.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/21 14:40:52 by bsoubaig #+# #+# */
/* Updated: 2023/12/21 17:59:37 by bsoubaig ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef BITCOINEXCHANGE_HPP
# define BITCOINEXCHANGE_HPP

# include <iostream>
# include <sstream>
# include <fstream>
# include <map>

# define BRED "\033[1;31m"
# define BGRN "\033[1;32m"
# define BYEL "\033[1;33m"
# define BBLU "\033[1;34m"
# define BMAG "\033[1;35m"
# define BCYN "\033[1;36m"
# define BWHT "\033[1;37m"
# define CRESET "\033[0m"

class BitcoinExchange {

private:
/* Attributes */
std::map<std::string, float> _database;

/* Private functions */
int _ft_stoi(std::string &str);
float _ft_stof(std::string &str);

public:
/* Constructors & Destructors */
BitcoinExchange(void);
BitcoinExchange(const BitcoinExchange &origin);
~BitcoinExchange(void);

/* Exceptions */
class FileReadException : public std::exception {
public:
virtual const char* what() const throw();
};

/* Functions */
void initDatabase(std::string fileName);

/* Overloaded operators */
BitcoinExchange &operator=(const BitcoinExchange &origin);

};

#endif
85 changes: 85 additions & 0 deletions ex00/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: bsoubaig <bsoubaig@student.42nice.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/03/12 17:45:22 by bsoubaig #+# #+# #
# Updated: 2023/12/21 17:47:18 by bsoubaig ### ########.fr #
# #
# **************************************************************************** #

# Colors constants
PURPLE = \033[38;5;141m
GREEN = \033[38;5;46m
RED = \033[0;31m
GREY = \033[38;5;240m
RESET = \033[0m
BOLD = \033[1m
CLEAR = \r\033[K

# Executable and compilation
NAME = btc

SRC_DIR = ./
SRCS = BitcoinExchange.cpp \
main.cpp

OBJ_DIR = ./objs/
OBJS = ${addprefix ${OBJ_DIR}, ${SRCS:.cpp=.o}}

CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98 -pedantic
RM = rm -rf

# Checking OS type
UNAME_S := $(shell uname -s)

ifneq ($(UNAME_S),Linux)
ifneq ($(UNAME_S),Darwin)
$(error Unsupported OS $(UNAME_S))
endif
endif

# Adding a specific flag for MacOS not Intel based
ifeq ($(UNAME_S),Darwin)
CFLAGS += -arch x86_64
endif

# Debug compilation is launched
ifdef DEBUG
CFLAGS += -fsanitize=leak -g
endif

${OBJ_DIR}%.o: ${SRC_DIR}%.cpp
@printf "${CLEAR}${RESET}${GREEN}»${RESET} [${PURPLE}${BOLD}${NAME}${RESET}]: Compiling ${GREEN}%s${RESET}...${GREY}" ${notdir $<}
@${CC} ${CFLAGS} -I${SRC_DIR} -c $< -o $@

all: $(NAME)

$(NAME): $(OBJS)
@$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
@clear
@printf "${CLEAR}${RESET}${GREY}────────────────────────────────────────────────────────────────────────────\n${RESET}${GREEN}»${RESET} [${PURPLE}${BOLD}${NAME}${RESET}]: ${RED}${BOLD}${NAME} ${RESET}compiled ${GREEN}successfully${RESET}.${GREY}\n${RESET}${GREY}────────────────────────────────────────────────────────────────────────────\n${RESET}"

${OBJS}: | ${OBJ_DIR}

${OBJ_DIR}:
@mkdir ${OBJ_DIR}

debug:
@make DEBUG=1 re

clean:
@${RM} ${OBJ_DIR}
@printf "${CLEAR}${RESET}${GREEN}»${RESET} [${PURPLE}${BOLD}${NAME}${RESET}]: Objects were cleaned ${GREEN}successfully${RESET}.\n${RESET}"

fclean: clean
@${RM} ${NAME}
@printf "${CLEAR}${RESET}${GREY}────────────────────────────────────────────────────────────────────────────\n${RESET}${GREEN}»${RESET} [${PURPLE}${BOLD}${NAME}${RESET}]: Project cleaned ${GREEN}successfully${RESET}.${GREY}\n${RESET}${GREY}────────────────────────────────────────────────────────────────────────────\n${RESET}"

re: fclean all

.SILENT: all clean fclean re
.PHONY: all clean fclean re
Loading

0 comments on commit efe0762

Please sign in to comment.