diff --git a/d01/ex04/Sed.cpp b/d01/ex04/Sed.cpp index b65a350..74cc9f4 100644 --- a/d01/ex04/Sed.cpp +++ b/d01/ex04/Sed.cpp @@ -14,29 +14,31 @@ void Sed::replace() { std::ifstream file(this->_file.c_str()); std::ofstream new_file(this->_new_file.c_str()); - char str[this->_find.length() - 1]; int len = this->_find.length(); - char tmp; + std::string str; + char c_str[len + 1]; - while (file.get(str, 1 + 1, EOF)) + if (file.fail() || new_file.fail()) + return; + + file.get(c_str, len + 1, EOF); + str.assign(c_str); + while (!file.eof()) { - if (str[0] == this->_find[0]) + if (this->_find.compare(str) == 0) { - if (len == 1) - new_file << this->_replacement; - else - { - tmp = str[0]; - file.get(str, len, EOF); - if (this->_find.compare(1, len - 1, str) == 0) - new_file << this->_replacement; - else - new_file << tmp << str; - } + new_file << this->_replacement; + file.get(c_str, len + 1, EOF); + str.assign(c_str); + continue; } else - new_file << str; + new_file << str[0]; + str.erase(str.begin()); + str.push_back(file.get()); } + for (int i = 0; str[i + 1]; i++) + new_file << str[i]; } diff --git a/d01/ex04/ldksliejf.replace b/d01/ex04/ldksliejf.replace deleted file mode 100644 index e69de29..0000000 diff --git a/d01/ex04/sed b/d01/ex04/sed deleted file mode 100755 index fabdcbb..0000000 Binary files a/d01/ex04/sed and /dev/null differ diff --git a/d01/ex04/test_log/test1 b/d01/ex04/test_log/test1 deleted file mode 100644 index 8d1c8b6..0000000 --- a/d01/ex04/test_log/test1 +++ /dev/null @@ -1 +0,0 @@ - diff --git a/d01/ex04/test_log/test2 b/d01/ex04/test_log/test2 deleted file mode 100644 index 23b0c96..0000000 --- a/d01/ex04/test_log/test2 +++ /dev/null @@ -1,5 +0,0 @@ -ce fichier -contient -plusieurs lignes -les unes au dessus des autres -youhouuu ioieux diff --git a/d01/ex04/test_log/test3 b/d01/ex04/test_log/test3 deleted file mode 100644 index 922f88c..0000000 --- a/d01/ex04/test_log/test3 +++ /dev/null @@ -1 +0,0 @@ -....................................................; diff --git a/d01/ex04/test_log/test4 b/d01/ex04/test_log/test4 deleted file mode 100644 index 0880dee..0000000 --- a/d01/ex04/test_log/test4 +++ /dev/null @@ -1,2 +0,0 @@ -...................................................; -. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test.sh b/d01/ex04/unitests/test.sh index 1fb520a..e41091b 100644 --- a/d01/ex04/unitests/test.sh +++ b/d01/ex04/unitests/test.sh @@ -1,8 +1,8 @@ #!/bin/bash cd $(dirname $0) source ./colors.sh - mkdir -p test_log +EXECUTABLE="sed" # RUN TESTS function run_tests @@ -10,7 +10,7 @@ mkdir -p test_log touch $TESTNAME echo "$CONTENT" > $TESTNAME - ../sed $TESTNAME "$FIND" "$REPLACEMENT" + ../$EXECUTABLE $TESTNAME "$FIND" "$REPLACEMENT" OUTPUT=$( cat $TESTNAME.replace ) if [ "$OUTPUT" == "$RESULT" ] then @@ -96,3 +96,113 @@ EOF ) run_tests +# TEST 6 ######################################## +TESTNAME="test6" +FIND="toutouille" +REPLACEMENT="||||||||||" +CONTENT=$(cat << EOF +trouve ce mot toutouille +et celui-la toutoutouille +EOF +) +RESULT=$(cat << EOF +trouve ce mot |||||||||| +et celui-la tou|||||||||| +EOF +) +run_tests + +# TEST 7 ######################################## +TESTNAME="test7" +FIND="n't" +REPLACEMENT="000" +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 + +You have to go out of my door +And leave just like you did before +I know I said that I was sure +But rich men can't imagine poor. + +One day baby, we'll be old +Oh baby, we'll be old +And think of all the stories that we could have told + +Little me and little you +Kept doing all the things they do +They never really think it through +Like I can never think you're true + +Here I go again - the blame +The guilt, the pain, the hurt, the shame +The founding fathers of our plane +That's stuck in heavy clouds of rain. + +One day baby, we'll be old +Oh baby, we'll be old +And think of all the stories that we could have told +EOF +) +RESULT=$(cat << EOF +No more tears, my heart is dry +I do000 laugh and I do000 cry +I do000 think about you all the time +But when I do - I wonder why + +You have to go out of my door +And leave just like you did before +I know I said that I was sure +But rich men ca000 imagine poor. + +One day baby, we'll be old +Oh baby, we'll be old +And think of all the stories that we could have told + +Little me and little you +Kept doing all the things they do +They never really think it through +Like I can never think you're true + +Here I go again - the blame +The guilt, the pain, the hurt, the shame +The founding fathers of our plane +That's stuck in heavy clouds of rain. + +One day baby, we'll be old +Oh baby, we'll be old +And think of all the stories that we could have told +EOF +) +run_tests + +# TEST 8 ######################################## +TESTNAME="test8" +FIND=$(cat << EOF +ry +I don't +EOF +) +REPLACEMENT=$(cat << EOF +oo +oooooooo +EOF +) +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 doo +oooooooolaugh and I don't coo +oooooooothink about you all the time +But when I do - I wonder why +EOF +) +run_tests + diff --git a/d01/ex04/unitests/test_log/test1 b/d01/ex04/unitests/test_log/test1 deleted file mode 100644 index 8d1c8b6..0000000 --- a/d01/ex04/unitests/test_log/test1 +++ /dev/null @@ -1 +0,0 @@ - diff --git a/d01/ex04/unitests/test_log/test1.replace b/d01/ex04/unitests/test_log/test1.replace deleted file mode 100644 index ce01362..0000000 --- a/d01/ex04/unitests/test_log/test1.replace +++ /dev/null @@ -1 +0,0 @@ -hello diff --git a/d01/ex04/unitests/test_log/test2 b/d01/ex04/unitests/test_log/test2 deleted file mode 100644 index 23b0c96..0000000 --- a/d01/ex04/unitests/test_log/test2 +++ /dev/null @@ -1,5 +0,0 @@ -ce fichier -contient -plusieurs lignes -les unes au dessus des autres -youhouuu ioieux diff --git a/d01/ex04/unitests/test_log/test2.replace b/d01/ex04/unitests/test_log/test2.replace deleted file mode 100644 index a573c23..0000000 --- a/d01/ex04/unitests/test_log/test2.replace +++ /dev/null @@ -1,5 +0,0 @@ -ce fich++r -cont++nt -plus++urs lignes -les unes au dessus des autres -youhouuu io++ux diff --git a/d01/ex04/unitests/test_log/test3 b/d01/ex04/unitests/test_log/test3 deleted file mode 100644 index 922f88c..0000000 --- a/d01/ex04/unitests/test_log/test3 +++ /dev/null @@ -1 +0,0 @@ -....................................................; diff --git a/d01/ex04/unitests/test_log/test3.replace b/d01/ex04/unitests/test_log/test3.replace deleted file mode 100644 index d3b95af..0000000 --- a/d01/ex04/unitests/test_log/test3.replace +++ /dev/null @@ -1 +0,0 @@ -++++++++++++++++++++++++++++++++++++++++++++++++++++; diff --git a/d01/ex04/unitests/test_log/test4 b/d01/ex04/unitests/test_log/test4 deleted file mode 100644 index 0880dee..0000000 --- a/d01/ex04/unitests/test_log/test4 +++ /dev/null @@ -1,2 +0,0 @@ -...................................................; -. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test_log/test4.replace b/d01/ex04/unitests/test_log/test4.replace deleted file mode 100644 index 80f497e..0000000 --- a/d01/ex04/unitests/test_log/test4.replace +++ /dev/null @@ -1,2 +0,0 @@ -++++++++++++++++++++++++++++++++++++++++++++++++++.; -. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test_log/test5 b/d01/ex04/unitests/test_log/test5 deleted file mode 100644 index 671f5ed..0000000 --- a/d01/ex04/unitests/test_log/test5 +++ /dev/null @@ -1 +0,0 @@ -test de mmdr foubar diff --git a/d01/ex04/unitests/test_log/test5.replace b/d01/ex04/unitests/test_log/test5.replace deleted file mode 100644 index 671f5ed..0000000 --- a/d01/ex04/unitests/test_log/test5.replace +++ /dev/null @@ -1 +0,0 @@ -test de mmdr foubar