CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
FileReplacer.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* FileReplacer.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/18 15:28:38 by kamitsui #+# #+# */
9/* Updated: 2025/04/23 01:01:07 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "FileReplacer.hpp"
19#include <sstream>
20
26FileReplacer::FileReplacer() : filename(""), s1(""), s2("") {}
27
38FileReplacer::FileReplacer(const std::string &filename, const std::string &s1, const std::string &s2)
39 : filename(filename), s1(s1), s2(s2) {}
40
48FileReplacer::FileReplacer(const FileReplacer &other) : filename(other.filename), s1(other.s1), s2(other.s2) {}
49
59 if (this != &other) {
60 filename = other.filename;
61 s1 = other.s1;
62 s2 = other.s2;
63 }
64 return *this;
65}
66
71
77const std::string &FileReplacer::getFileName() const { return filename; }
78
84const std::string &FileReplacer::getS1() const { return s1; }
85
91const std::string &FileReplacer::getS2() const { return s2; }
92
101 if (getS1().empty()) {
102 throw std::runtime_error("Empty <string_find_to\n");
103 }
104 std::ifstream inputFile(filename.c_str());
105 if (!inputFile.is_open()) {
106 throw std::runtime_error("Could not open input file: " + filename);
107 }
108
109 std::string outputFilepath = filename + ".replace";
110 std::cout << "create filestream path [" << outputFilepath << "]" << std::endl;
111 std::ofstream outputFile(outputFilepath.c_str());
112 if (!outputFile.is_open()) {
113 throw std::runtime_error("Could not open output file: " + outputFilepath);
114 }
115
116 std::string line;
117 while (std::getline(inputFile, line)) {
118 std::string resultLine = "";
119 size_t pos = 0;
120 while (pos < line.length()) {
121 size_t foundPos = line.find(s1, pos);
122 if (foundPos == std::string::npos) {
123 resultLine += line.substr(pos);
124 break;
125 } else {
126 resultLine += line.substr(pos, foundPos - pos);
127 resultLine += s2;
128 pos = foundPos + s1.length();
129 }
130 }
131 outputFile << resultLine << std::endl;
132 }
133
134 inputFile.close();
135 outputFile.close();
136}
Declares the FileReplacer class for replacing strings in a file.
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
ostream cout
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &&__is, basic_string< _CharT, _Traits, _Alloc > &__str)
basic_string substr(size_type __pos=0, size_type __n=npos) const
size_type length() const noexcept
const _CharT * c_str() const noexcept
static const size_type npos
size_type find(_CharT __c, size_type __pos=0) const noexcept
A class that handles replacing occurrences of a string within a file.
FileReplacer()
Default constructor for the FileReplacer class.
void processFile()
Processes the input file and performs the string replacement.
FileReplacer & operator=(const FileReplacer &other)
Copy assignment operator for the FileReplacer class.
const std::string & getS1() const
Gets the string to find (s1).
~FileReplacer()
Destructor for the FileReplacer class.
const std::string & getFileName() const
Gets the filename.
const std::string & getS2() const
Gets the string to replace with (s2).