diff --git a/d01/ex03/HumanB.cpp b/d01/ex03/HumanB.cpp index 03c8a1f..49fb4c1 100644 --- a/d01/ex03/HumanB.cpp +++ b/d01/ex03/HumanB.cpp @@ -1,22 +1,18 @@ #include "HumanB.hpp" HumanB::HumanB( std::string name ) { - this->_name = name; - } HumanB::~HumanB() {} void HumanB::setWeapon( Weapon &weapon ) { - this->_weapon = &weapon; - } void HumanB::attack( void ) { - + if (!this->_weapon) + return; std::cout << this->_name << " attacks with their "; std::cout << this->_weapon->getType() << std::endl; - } diff --git a/d01/ex03/Weapon.cpp b/d01/ex03/Weapon.cpp index d92b812..5232fff 100644 --- a/d01/ex03/Weapon.cpp +++ b/d01/ex03/Weapon.cpp @@ -1,21 +1,15 @@ #include "Weapon.hpp" Weapon::Weapon( std::string type ) { - this->_type = type; return; - } Weapon::~Weapon() {} std::string const & Weapon::getType( void ) const { - return this->_type; - } void Weapon::setType( std::string type ) { - this->_type = type; - } diff --git a/d01/ex03/main.cpp b/d01/ex03/main.cpp index 539263c..b3ff5e0 100644 --- a/d01/ex03/main.cpp +++ b/d01/ex03/main.cpp @@ -14,6 +14,7 @@ int main() { Weapon club = Weapon("crude spiked club"); HumanB jim("Jim"); + jim.attack(); jim.setWeapon(club); jim.attack(); club.setType("some other type of club"); diff --git a/d01/ex03/war b/d01/ex03/war new file mode 100755 index 0000000..b82da62 Binary files /dev/null and b/d01/ex03/war differ diff --git a/d01/ex04/Makefile.replace b/d01/ex04/Makefile.replace new file mode 100644 index 0000000..3f95f8a --- /dev/null +++ b/d01/ex04/Makefile.replace @@ -0,0 +1,66 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value . name is case sensitive # +# VARIABLES . or name = value \ . use VPATH only for .c # +# . value . or .cpp # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = sed + +CC = c++ +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 -g3 + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = . +SRCS = main.cpp \ + Sed.cpp + +D_HEADERS = . +HEADERS = Sed.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o) + +RM_D_OBJS = rm -rf $(D_OBJS) +ifeq "$(D_OBJS)" "." +RM_D_OBJS = +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.cpp | $(D_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) + $(CC) $(OBJS) -o $@ $(LIBS) + +leaks: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) Makefile OBJS BANANE + +clean: + rm -f $(OBJS) + rm -f *.replace + +fclean: clean + rm -f $(NAME) + $(RM_D_OBJS) + +re: fclean all + +.PHONY : all clean fclean re bonus run valgrind diff --git a/d01/ex04/Sed.cpp b/d01/ex04/Sed.cpp index b5556e6..32736dd 100644 --- a/d01/ex04/Sed.cpp +++ b/d01/ex04/Sed.cpp @@ -16,20 +16,23 @@ void Sed::replace() { std::ofstream new_file(this->_new_file.c_str()); int len = this->_find.length(); std::string str; - char c_str[len + 1]; + char cstr[len + 1]; if (file.fail() || new_file.fail()) return; - file.get(c_str, len + 1, EOF); - str.assign(c_str); + if (len) + file.get(cstr, len + 1, EOF); + else + file.get(cstr, len + 2, EOF); + str.assign(cstr); while (!file.eof()) { - if (this->_find.compare(str) == 0) + if (len && this->_find.compare(str) == 0) { new_file << this->_replacement; - file.get(c_str, len + 1, EOF); - str.assign(c_str); + file.get(cstr, len + 1, EOF); + str.assign(cstr); continue; } else @@ -39,6 +42,4 @@ void Sed::replace() { } str.erase(str.end() - 1); new_file << str; - } - diff --git a/d01/ex04/sed b/d01/ex04/sed new file mode 100755 index 0000000..7faa8e9 Binary files /dev/null and b/d01/ex04/sed differ diff --git a/d01/ex04/unitests/test.sh b/d01/ex04/unitests/test.sh index 9031d4b..b014a20 100644 --- a/d01/ex04/unitests/test.sh +++ b/d01/ex04/unitests/test.sh @@ -213,3 +213,43 @@ EOF ) run_tests +# TEST 9 ######################################## +TESTNAME="test9" +FIND="" +REPLACEMENT="p" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +run_tests + +# TEST 10 ######################################## +TESTNAME="test10" +FIND="" +REPLACEMENT="" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +run_tests + diff --git a/d01/ex04/unitests/test_log/test9 b/d01/ex04/unitests/test_log/test9 new file mode 100644 index 0000000..652b428 --- /dev/null +++ b/d01/ex04/unitests/test_log/test9 @@ -0,0 +1,4 @@ +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why diff --git a/d01/ex04/unitests/test_log/test9.replace b/d01/ex04/unitests/test_log/test9.replace new file mode 100644 index 0000000..652b428 --- /dev/null +++ b/d01/ex04/unitests/test_log/test9.replace @@ -0,0 +1,4 @@ +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why