Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TrackingDeque"
- #include "BufferedFile.cpp"
- #include <iostream>
- #include <unistd.h>
- using namespace std;
- enum State
- {
- SEARCHING,
- REPLACING,
- PARTIAL_MATCH,
- POTENTIAL_MATCH,
- FAILED_MATCH,
- SUCCESSFUL_MATCH,
- };
- class StreamSource {
- public:
- StreamSource(const char *srch, const char *repl, const string &fname);
- StreamSource() = delete;
- ~StreamSource();
- StreamSource& get(char &);
- void put(const char &);
- void matched_search_string();
- void found_partial_match(int &count, char &ch);
- void start();
- bool eof();
- State get_state();
- void set_state(State new_state);
- void set_file_length(int len);
- int srch_len; //move to private
- int repl_len;
- bool internal_eof;
- int match_count;
- private:
- const char * search;
- const char * replace;
- string filename;
- int partial_match_index;
- int replacing_index;
- char current_char;
- State state;
- BufferedFile buf_file;
- string partial_match;
- };
- StreamSource::StreamSource(const char *srch, const char *repl, const string &fname)
- : search(srch), replace(repl), buf_file(fname), filename(fname), match_count(0), partial_match("&"),
- replacing_index(0), srch_len(strlen(srch)), repl_len(strlen(repl)),
- internal_eof(false), partial_match_index(0)
- {
- }
- StreamSource::~StreamSource() {
- //buf_file.flushy();
- truncate(filename.c_str(), buf_file.get_putpos());
- }
- StreamSource& StreamSource::get(char &ch) {
- if (state == SEARCHING) {
- buf_file.get(ch);
- } else if (state == REPLACING) {
- ch = replace[replacing_index];
- replacing_index++;
- } else if (state == PARTIAL_MATCH) {
- if (partial_match.length() < 1) {
- assert(false);
- }
- ch = partial_match[partial_match_index];
- partial_match_index++;
- if (partial_match_index >= partial_match.length()) { // change to strlen
- partial_match_index = 0;
- set_state(SEARCHING);
- }
- } else {
- assert(false);
- }
- if (buf_file.eof()) {
- internal_eof = true;
- }
- return *this;
- }
- void StreamSource::put(const char &ch) {
- if (state == SEARCHING) {
- // do nothing
- } else if (state == REPLACING) {
- if (replacing_index == strlen(replace)) {
- replacing_index = 0;
- set_state(SEARCHING);
- }
- } else if (state == PARTIAL_MATCH) {
- // do nothing
- } else {
- assert(false);
- }
- buf_file.put(ch);
- }
- void StreamSource::matched_search_string() {
- //assert(false);
- //for (int i = 0; i < strlen(replace); i++) {
- // put(replace[i]);
- //}
- }
- void StreamSource::found_partial_match(int &match_count, char &ch) {
- if (state == SEARCHING) {
- assert(match_count > 0);
- set_state(PARTIAL_MATCH);
- partial_match = "";
- for (int i = 0; i < match_count; i++) {
- partial_match += search[i];
- }
- partial_match += ch;
- ch = partial_match[0];
- match_count = 0;
- partial_match_index = 1;
- } else if (state == PARTIAL_MATCH) {
- assert(match_count > 0);
- set_state(PARTIAL_MATCH);
- partial_match.erase(0,1);
- ch = partial_match[0];
- match_count = 0;
- partial_match_index = 1;
- } else {
- // assert(false);
- }
- }
- void StreamSource::start() {
- //while(!get(current_char).eof()) {
- //}
- }
- bool StreamSource::eof() {
- return internal_eof;
- }
- State StreamSource::get_state() {
- return state;
- }
- void StreamSource::set_state(State new_state) {
- state = new_state;
- }
- void StreamSource::set_file_length(int len) {
- buf_file.set_file_length(len);
- }
- int main(int argc, char *argv[]) {
- const char * search_word = argv[1];
- const char * replace_word = argv[2];
- string filename = argv[3];
- StreamSource ss(argv[1], argv[2], argv[3]);
- ifstream is;
- is.open(filename.c_str(), ios::binary);
- is.seekg(0, ios::end);
- ss.set_file_length(is.tellg());
- char ch = '%';
- //int match_count = 0;
- while(!ss.get(ch).eof()) {
- if(ss.get_state() != REPLACING && ch == search_word[ss.match_count]) {
- ss.match_count++;
- if (ss.match_count < strlen(search_word)) {
- continue;
- }
- }
- if (ss.match_count == strlen(search_word)) {
- ss.set_state(REPLACING);
- //ss.matched_search_string();
- } else if (ss.match_count > 0) {
- ss.found_partial_match(ss.match_count, ch);
- ss.put(ch);
- } else {
- ss.put(ch);
- }
- ss.match_count = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement