summaryrefslogtreecommitdiff
path: root/graphs/cpp/replace
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/replace
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/replace')
-rw-r--r--graphs/cpp/replace/inputfile1
-rw-r--r--graphs/cpp/replace/replace.cc39
-rw-r--r--graphs/cpp/replace/replace.hh6
-rw-r--r--graphs/cpp/replace/test_replace.cc16
4 files changed, 62 insertions, 0 deletions
diff --git a/graphs/cpp/replace/inputfile b/graphs/cpp/replace/inputfile
new file mode 100644
index 0000000..b4e5a2f
--- /dev/null
+++ b/graphs/cpp/replace/inputfile
@@ -0,0 +1 @@
+abcabcbc \ No newline at end of file
diff --git a/graphs/cpp/replace/replace.cc b/graphs/cpp/replace/replace.cc
new file mode 100644
index 0000000..c24bf05
--- /dev/null
+++ b/graphs/cpp/replace/replace.cc
@@ -0,0 +1,39 @@
+#include "replace.hh"
+
+#include <fstream>
+#include <iostream>
+#include <string>
+
+void replace(const std::string& input_filename,
+ const std::string& output_filename, const std::string& src_token,
+ const std::string& dst_token)
+{
+ std::ofstream file_out;
+ file_out.open(output_filename);
+
+ if (!file_out.is_open())
+ {
+ std::cerr << "Cannot write output file\n";
+ return;
+ }
+
+ std::ifstream file_in;
+ file_in.open(input_filename);
+ if (!file_in.is_open())
+ {
+ std::cerr << "Cannot open input file\n";
+ return;
+ }
+
+ std::string token;
+ while (std::getline(file_in, token))
+ {
+ std::string::size_type n = 0;
+ while ((n = token.find(src_token, n)) != std::string::npos)
+ {
+ token.replace(n, src_token.size(), dst_token);
+ n += dst_token.size();
+ }
+ file_out << token << "\n";
+ }
+} \ No newline at end of file
diff --git a/graphs/cpp/replace/replace.hh b/graphs/cpp/replace/replace.hh
new file mode 100644
index 0000000..cbb2e46
--- /dev/null
+++ b/graphs/cpp/replace/replace.hh
@@ -0,0 +1,6 @@
+#pragma once
+#include <string>
+
+void replace(const std::string& input_filename,
+ const std::string& output_filename, const std::string& src_token,
+ const std::string& dst_token); \ No newline at end of file
diff --git a/graphs/cpp/replace/test_replace.cc b/graphs/cpp/replace/test_replace.cc
new file mode 100644
index 0000000..be7a488
--- /dev/null
+++ b/graphs/cpp/replace/test_replace.cc
@@ -0,0 +1,16 @@
+#include <iostream>
+
+#include "replace.hh"
+
+int main(int argc, char** argv)
+{
+ if (argc < 5)
+ {
+ std::cerr << "Usage: INPUT_FILE OUTPUT_FILE SRC_TOKEN DST_TOKEN\n";
+ return 1;
+ }
+
+ replace(argv[1], argv[2], argv[3], argv[4]);
+
+ return 0;
+}