d04 correction deux erreurs

This commit is contained in:
Hugo LAMY
2022-03-15 21:11:15 +01:00
parent 66c68016ab
commit c19c89be85
10 changed files with 126 additions and 20 deletions

66
d01/ex04/Makefile.replace Normal file
View File

@@ -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

View File

@@ -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;
}

BIN
d01/ex04/sed Executable file

Binary file not shown.

View File

@@ -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

View File

@@ -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

View File

@@ -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