diff --git a/d01/ex04/Sed.cpp b/d01/ex04/Sed.cpp index 748c9ca..b4acbed 100644 --- a/d01/ex04/Sed.cpp +++ b/d01/ex04/Sed.cpp @@ -2,30 +2,35 @@ Sed::Sed(char *file, char *find, char *replacement) : _file( file ) -// , _new_file( file.append(".replace") ) + , _new_file( std::string(file).append(".replace") ) , _find( find ) - , _replacement( replacement ) -{ + , _replacement( replacement ) { -// this->_buf = return; - -} -Sed::~Sed() { - -// delete[] this->_buf; - return; - } +Sed::~Sed() {return;} void Sed::replace() { std::ifstream file(this->_file.c_str()); - char c; + std::ofstream new_file(this->_new_file.c_str()); + char str[this->_find.length() - 1]; + int len = this->_find.length(); + char tmp; - while (file.get(c)) + while (file.get(str, 1 + 1, EOF)) { - std::cout << c; + if (str[0] == this->_find[0]) + { + 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; + } + else + new_file << str; } } diff --git a/d01/ex04/Sed.hpp b/d01/ex04/Sed.hpp index e197139..79ae005 100644 --- a/d01/ex04/Sed.hpp +++ b/d01/ex04/Sed.hpp @@ -2,6 +2,7 @@ # define SED_HPP #include +#include #include #include @@ -17,6 +18,7 @@ public: private: // std::string _buf; + std::string _tmp; std::string const _file; std::string const _new_file; std::string const _find; diff --git a/d01/ex04/sed b/d01/ex04/sed index df9acd2..6ef7124 100755 Binary files a/d01/ex04/sed and b/d01/ex04/sed differ diff --git a/d01/ex04/test.txt b/d01/ex04/test.txt deleted file mode 100644 index 3547a3f..0000000 --- a/d01/ex04/test.txt +++ /dev/null @@ -1,27 +0,0 @@ -No 1 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 diff --git a/d01/ex04/unitests/colors.sh b/d01/ex04/unitests/colors.sh new file mode 100644 index 0000000..47b5a4f --- /dev/null +++ b/d01/ex04/unitests/colors.sh @@ -0,0 +1,19 @@ +# COLORS + RED="\e[0;31m" + GREEN="\e[0;32m" + YELLOW="\e[0;33m" + BLUE="\e[0;34m" + MAGENTA="\e[0;35m" + CYAN="\e[0;36m" + WHITE="\e[0;37m" + + B_RED="\e[1;31m" + B_GREEN="\e[1;32m" + B_YELLOW="\e[1;33m" + B_BLUE="\e[1;34m" + B_MAGENTA="\e[1;35m" + B_CYAN="\e[1;36m" + B_WHITE="\e[1;37m" + + ENDCO="\e[0m" + diff --git a/d01/ex04/unitests/test.sh b/d01/ex04/unitests/test.sh new file mode 100644 index 0000000..05c1a55 --- /dev/null +++ b/d01/ex04/unitests/test.sh @@ -0,0 +1,83 @@ +#!/bin/bash +source ./colors.sh + +mkdir -p test_log + +# RUN TESTS + function run_tests + { + touch $TESTNAME + echo "$CONTENT" > $TESTNAME + + ../sed $TESTNAME "$FIND" "$REPLACEMENT" + OUTPUT=$( cat $TESTNAME.replace ) + if [ "$OUTPUT" == "$RESULT" ] + then + echo -e $B_GREEN"test1 success"$ENDCO + else + echo -e $B_RED"test1 faillure"$ENDCO + fi + + mv $TESTNAME $TESTNAME.replace ./test_log + } + +# TEST 1 ######################################## +TESTNAME="test1" +FIND=" " +REPLACEMENT="hello" +CONTENT=" " +RESULT="hello" +run_tests + +# TEST 2 ######################################## +TESTNAME="test2" +FIND="ie" +REPLACEMENT="++" +CONTENT=$(cat << EOF +ce fichier +contient +plusieurs lignes +les unes au dessus des autres +youhouuu ioieux +EOF +) +RESULT=$(cat << EOF +ce fich++r +cont++nt +plus++urs lignes +les unes au dessus des autres +youhouuu io++ux +EOF +) +run_tests + +# TEST 3 ######################################## +TESTNAME="test3" +FIND="." +REPLACEMENT="+" +CONTENT=$(cat << EOF +....................................................; +EOF +) +RESULT=$(cat << EOF +++++++++++++++++++++++++++++++++++++++++++++++++++++; +EOF +) +run_tests + +# TEST 3 ######################################## +TESTNAME="test4" +FIND=".." +REPLACEMENT="++" +CONTENT=$(cat << EOF +...................................................; +. . . . . . . . . . . . . . . . . . . . . . . . . .; +EOF +) +RESULT=$(cat << EOF +++++++++++++++++++++++++++++++++++++++++++++++++++.; +. . . . . . . . . . . . . . . . . . . . . . . . . .; +EOF +) +run_tests + diff --git a/d01/ex04/unitests/test_log/test1 b/d01/ex04/unitests/test_log/test1 new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/d01/ex04/unitests/test_log/test1 @@ -0,0 +1 @@ + diff --git a/d01/ex04/unitests/test_log/test1.replace b/d01/ex04/unitests/test_log/test1.replace new file mode 100644 index 0000000..b6fc4c6 --- /dev/null +++ b/d01/ex04/unitests/test_log/test1.replace @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/d01/ex04/unitests/test_log/test2 b/d01/ex04/unitests/test_log/test2 new file mode 100644 index 0000000..23b0c96 --- /dev/null +++ b/d01/ex04/unitests/test_log/test2 @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..a573c23 --- /dev/null +++ b/d01/ex04/unitests/test_log/test2.replace @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..922f88c --- /dev/null +++ b/d01/ex04/unitests/test_log/test3 @@ -0,0 +1 @@ +....................................................; diff --git a/d01/ex04/unitests/test_log/test3.replace b/d01/ex04/unitests/test_log/test3.replace new file mode 100644 index 0000000..9b26e9b --- /dev/null +++ b/d01/ex04/unitests/test_log/test3.replace @@ -0,0 +1 @@ ++ \ No newline at end of file diff --git a/d01/ex04/unitests/test_log/test4 b/d01/ex04/unitests/test_log/test4 new file mode 100644 index 0000000..0880dee --- /dev/null +++ b/d01/ex04/unitests/test_log/test4 @@ -0,0 +1,2 @@ +...................................................; +. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test_log/test4.replace b/d01/ex04/unitests/test_log/test4.replace new file mode 100644 index 0000000..80f497e --- /dev/null +++ b/d01/ex04/unitests/test_log/test4.replace @@ -0,0 +1,2 @@ +++++++++++++++++++++++++++++++++++++++++++++++++++.; +. . . . . . . . . . . . . . . . . . . . . . . . . .;