aboutsummaryrefslogtreecommitdiff
path: root/cpp01/ex07
diff options
context:
space:
mode:
Diffstat (limited to 'cpp01/ex07')
-rw-r--r--cpp01/ex07/Makefile35
-rw-r--r--cpp01/ex07/main.cpp56
-rwxr-xr-xcpp01/ex07/test.sh5
-rw-r--r--cpp01/ex07/test16
-rw-r--r--cpp01/ex07/test25
5 files changed, 107 insertions, 0 deletions
diff --git a/cpp01/ex07/Makefile b/cpp01/ex07/Makefile
new file mode 100644
index 0000000..7a649fc
--- /dev/null
+++ b/cpp01/ex07/Makefile
@@ -0,0 +1,35 @@
+# **************************************************************************** #
+# #
+# ::: :::::::: #
+# Makefile :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/02/02 17:05:31 by cacharle #+# #+# #
+# Updated: 2020/02/02 17:58:08 by cacharle ### ########.fr #
+# #
+# **************************************************************************** #
+
+CXX = clang++
+CXXFLAGS= -Wall -Wextra #-Werror
+
+SRC = $(shell find . -type f -name "*.cpp")
+OBJ = $(SRC:.cpp=.o)
+
+NAME = replace
+
+all: $(NAME)
+
+$(NAME): $(OBJ)
+ $(CXX) -o $@ $^
+
+%.o: %.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+clean:
+ $(RM) $(OBJ)
+
+fclean: clean
+ $(RM) $(NAME)
+
+re: fclean all
diff --git a/cpp01/ex07/main.cpp b/cpp01/ex07/main.cpp
new file mode 100644
index 0000000..fbef95f
--- /dev/null
+++ b/cpp01/ex07/main.cpp
@@ -0,0 +1,56 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/02 17:08:28 by cacharle #+# #+# */
+/* Updated: 2020/02/02 18:00:25 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+# include <iostream>
+# include <fstream>
+# include <string>
+
+int main(int argc, char **argv)
+{
+ if (argc != 4)
+ {
+ std::cerr << "Usage: " << argv[0] << " filename s1 s2";
+ return 1;
+ }
+ std::string filename(argv[1]);
+ std::string find_str(argv[2]);
+ std::string replace_str(argv[3]);
+
+ std::ifstream file(filename);
+ std::ofstream outfile(filename + ".replace");
+ if (!file)
+ {
+ std::cerr << "Could not open " << filename;
+ outfile.close();
+ return 1;
+ }
+ if (!outfile)
+ {
+ std::cerr << "Could not create " << filename << ".replace";
+ file.close();
+ return 1;
+ }
+ std::string line;
+ while (std::getline(file, line))
+ {
+ while (true)
+ {
+ size_t i = line.find(find_str);
+ if (i == std::string::npos)
+ break;
+ line.replace(i, find_str.length(), replace_str);
+ }
+ outfile << line << std::endl;
+ }
+ file.close();
+ outfile.close();
+}
diff --git a/cpp01/ex07/test.sh b/cpp01/ex07/test.sh
new file mode 100755
index 0000000..69cdc43
--- /dev/null
+++ b/cpp01/ex07/test.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+make all
+./replace test1 bonjour aurevoir
+./replace test2 occ many
diff --git a/cpp01/ex07/test1 b/cpp01/ex07/test1
new file mode 100644
index 0000000..2851ba4
--- /dev/null
+++ b/cpp01/ex07/test1
@@ -0,0 +1,6 @@
+bonjour je suis un
+charles
+avec quelque bonjour
+dans ce fichier
+bonjour
+hi im bonj
diff --git a/cpp01/ex07/test2 b/cpp01/ex07/test2
new file mode 100644
index 0000000..227746e
--- /dev/null
+++ b/cpp01/ex07/test2
@@ -0,0 +1,5 @@
+occ occ occ hocket
+
+lalala occ asdfasdf occ
+
+occ occ occ occ