daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "TrackingDeque"
  2. #include "BufferedFile.cpp"
  3. #include <iostream>
  4. #include <unistd.h>
  5.  
  6. using namespace std;
  7.  
  8. enum State
  9. {
  10.     SEARCHING,
  11.     REPLACING,
  12.     PARTIAL_MATCH,
  13.     POTENTIAL_MATCH,
  14.     FAILED_MATCH,
  15.     SUCCESSFUL_MATCH,
  16. };
  17.  
  18. class StreamSource {
  19.   public:
  20.     StreamSource(const char *srch, const char *repl, const string &fname);
  21.     StreamSource() = delete;
  22.     ~StreamSource();
  23.     StreamSource& get(char &);
  24.     void put(const char &);
  25.     void matched_search_string();
  26.     void found_partial_match(int &count, char &ch);
  27.     void start();
  28.     bool eof();
  29.     State get_state();
  30.     void set_state(State new_state);
  31.     void set_file_length(int len);
  32.     int srch_len; //move to private
  33.     int repl_len;
  34.     bool internal_eof;
  35.     int match_count;
  36.   private:
  37.     const char * search;
  38.     const char * replace;
  39.     string filename;
  40.     int partial_match_index;
  41.     int replacing_index;
  42.     char current_char;
  43.     State state;
  44.     BufferedFile buf_file;
  45.     string partial_match;
  46. };
  47.  
  48. StreamSource::StreamSource(const char *srch, const char *repl, const string &fname)
  49.     : search(srch), replace(repl), buf_file(fname), filename(fname), match_count(0), partial_match("&"),
  50.       replacing_index(0), srch_len(strlen(srch)), repl_len(strlen(repl)),
  51.       internal_eof(false), partial_match_index(0)
  52. {
  53. }
  54.  
  55. StreamSource::~StreamSource() {
  56.     //buf_file.flushy();
  57.     truncate(filename.c_str(), buf_file.get_putpos());
  58. }
  59.  
  60. StreamSource& StreamSource::get(char &ch) {
  61.     if (state == SEARCHING) {
  62.         buf_file.get(ch);
  63.     } else if (state == REPLACING) {
  64.         ch = replace[replacing_index];
  65.         replacing_index++;
  66.     } else if (state == PARTIAL_MATCH) {
  67.         if (partial_match.length() < 1) {
  68.             assert(false);
  69.         }
  70.         ch = partial_match[partial_match_index];
  71.         partial_match_index++;
  72.         if (partial_match_index >= partial_match.length()) { // change to strlen
  73.             partial_match_index = 0;
  74.             set_state(SEARCHING);
  75.         }
  76.     } else {
  77.         assert(false);
  78.     }
  79.     if (buf_file.eof()) {
  80.         internal_eof = true;
  81.     }
  82.     return *this;
  83. }
  84.  
  85. void StreamSource::put(const char &ch) {
  86.     if (state == SEARCHING) {
  87.         // do nothing
  88.     } else if (state == REPLACING) {
  89.         if (replacing_index == strlen(replace)) {
  90.             replacing_index = 0;
  91.             set_state(SEARCHING);
  92.         }
  93.     } else if (state == PARTIAL_MATCH) {
  94.         // do nothing
  95.     } else {
  96.         assert(false);
  97.     }
  98.     buf_file.put(ch);
  99. }
  100.  
  101. void StreamSource::matched_search_string() {
  102.     //assert(false);
  103.     //for (int i = 0; i < strlen(replace); i++) {
  104.     //    put(replace[i]);
  105.     //}
  106. }
  107.  
  108. void StreamSource::found_partial_match(int &match_count, char &ch) {
  109.     if (state == SEARCHING) {
  110.         assert(match_count > 0);
  111.         set_state(PARTIAL_MATCH);
  112.         partial_match = "";
  113.         for (int i = 0; i < match_count; i++) {
  114.             partial_match += search[i];
  115.         }
  116.         partial_match += ch;
  117.         ch = partial_match[0];
  118.         match_count = 0;
  119.         partial_match_index = 1;
  120.     } else if (state == PARTIAL_MATCH) {
  121.         assert(match_count > 0);
  122.         set_state(PARTIAL_MATCH);
  123.         partial_match.erase(0,1);
  124.         ch = partial_match[0];
  125.         match_count = 0;
  126.         partial_match_index = 1;
  127.     } else {
  128.     //    assert(false);
  129.     }
  130. }
  131.  
  132. void StreamSource::start() {
  133.     //while(!get(current_char).eof()) {
  134.     //}
  135. }
  136.  
  137. bool StreamSource::eof() {
  138.     return internal_eof;
  139. }
  140.  
  141. State StreamSource::get_state() {
  142.     return state;
  143. }
  144.  
  145. void StreamSource::set_state(State new_state) {
  146.     state = new_state;
  147. }
  148.  
  149. void StreamSource::set_file_length(int len) {
  150.     buf_file.set_file_length(len);
  151. }
  152.  
  153. int main(int argc, char *argv[]) {
  154.     const char * search_word = argv[1];
  155.     const char * replace_word = argv[2];
  156.     string filename =  argv[3];
  157.     StreamSource ss(argv[1], argv[2], argv[3]);
  158.     ifstream is;
  159.     is.open(filename.c_str(), ios::binary);
  160.     is.seekg(0, ios::end);
  161.     ss.set_file_length(is.tellg());
  162.  
  163.     char ch = '%';
  164.     //int match_count = 0;
  165.     while(!ss.get(ch).eof()) {
  166.         if(ss.get_state() != REPLACING && ch == search_word[ss.match_count]) {
  167.             ss.match_count++;
  168.             if (ss.match_count < strlen(search_word)) {
  169.                 continue;
  170.             }
  171.         }
  172.         if (ss.match_count == strlen(search_word)) {
  173.             ss.set_state(REPLACING);
  174.             //ss.matched_search_string();
  175.         } else if (ss.match_count > 0) {
  176.             ss.found_partial_match(ss.match_count, ch);
  177.             ss.put(ch);
  178.         } else {
  179.             ss.put(ch);
  180.         }
  181.         ss.match_count = 0;
  182.     }
  183.     return 0;
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top