Browse Source

Initial mangle class.

Steve Thielemann 4 years ago
parent
commit
64646f2709
5 changed files with 107 additions and 146 deletions
  1. 1 1
      CMakeLists.txt
  2. 57 0
      charman.cpp
  3. 25 0
      charman.h
  4. 21 144
      wordplay.cpp
  5. 3 1
      wordplay.h

+ 1 - 1
CMakeLists.txt

@@ -88,7 +88,7 @@ add_subdirectory(zf_log)
 # )
 # add_executable(yourProj ${SOURCES})
 
-add_executable(hharry hharry.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp images.h wordplay.cpp)
+add_executable(hharry hharry.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp images.h wordplay.cpp charman.cpp)
 target_link_libraries(hharry util)
 target_link_libraries(hharry zf_log)
 # target_compile_definitions(hharry PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_INFO)

+ 57 - 0
charman.cpp

@@ -0,0 +1,57 @@
+#include <regex>
+#include <string>
+#include <vector>
+
+#include "charman.h"
+#include "zf_log.h"
+
+void CharMan::validate(void) {
+  bool valid = true;
+  for (int x = 0; x < text_offsets.size(); ++x) {
+    int offset = text_offsets[x];
+
+    if (offset >= 0) {
+      if (text[x] != buffer[offset]) {
+        ZF_LOGE("validate: %d off %d [%c != %c]", x, offset, text[x],
+                buffer[offset]);
+        valid = false;
+      }
+      if (text[x] != work[offset]) {
+        ZF_LOGE("validate: %d off %d [%c != %c]", x, offset, text[x],
+                work[offset]);
+        valid = false;
+      }
+    }
+  }
+  if (!valid) {
+    ZF_LOGE("* NOT VALID*  Somethings hosed.");
+  }
+}
+
+void CharMan::regular_expressions(void) {
+  std::regex words("[a-zA-Z]+( [a-zA-Z]+)+");
+  // I need position and length.
+
+  for (auto it =
+           std::sregex_iterator(this->text.begin(), this->text.end(), words);
+       it != std::sregex_iterator(); ++it) {
+    pos_len.push_back(std::make_pair(it->position(), it->length()));
+    ZF_LOGD("pos %d len %d", (int)it->position(), (int)it->length());
+  }
+}
+
+CharMan::CharMan(std::string &buffer, std::string &work, std::string &text,
+                 std::vector<int> &text_offsets)
+    : buffer(buffer), work(work), text(text), text_offsets(text_offsets) {
+  /*
+this->buffer = buffer;
+this->work = work;
+this->text = text;
+this->text_offsets = text_offsets;
+*/
+  validate();
+  regular_expressions();
+  ZF_LOGD("Found %d word groups", pos_len.size());
+};
+
+CharMan::~CharMan() { validate(); }

+ 25 - 0
charman.h

@@ -0,0 +1,25 @@
+#ifndef CHARMAN_H
+#define CHARMAN_H
+
+#include <string>
+#include <vector>
+
+class CharMan {
+private:
+  std::string &buffer;
+  std::string &work;
+  std::string &text;
+  std::vector<int> &text_offsets;
+
+  std::vector<std::pair<int, int>> pos_len;
+
+  void validate(void);
+  void regular_expressions();
+
+public:
+  CharMan(std::string &buffer, std::string &work, std::string &text,
+          std::vector<int> &text_offsets);
+  ~CharMan();
+};
+
+#endif

+ 21 - 144
wordplay.cpp

@@ -4,12 +4,13 @@
 #include "terminal.h"
 #include "utils.h"
 #include <iomanip>
-#include <regex>
+// #include <regex.h>
 #include <sstream>
-// #include <string.h>
+#include <string.h>
 #include <string>
 #include <vector>
 
+#include "charman.h"
 #include "zf_log.h"
 
 #include <unistd.h> // write
@@ -67,78 +68,7 @@ void init_harry() {
   console_init(&console);
 }
 
-#ifdef UNWANTED
-regex_t ANSI;
-regex_t WORDS;
-regex_t WORD;
-
-int init_regex(void) {
-  int ret;
-  char ansi[] = "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]";
-  char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
-  char word[] = "[a-zA-Z]+";
-  char errorbuf[100];
-
-  if (ret = regcomp(&ANSI, ansi, REG_EXTENDED | REG_NEWLINE)) {
-    regerror(ret, &ANSI, errorbuf, sizeof(errorbuf));
-    ZF_LOGW("Regex %s failed to compile: %s", ansi, errorbuf);
-    return 0;
-  };
-
-  if (ret = regcomp(&WORDS, words, REG_EXTENDED | REG_NEWLINE)) {
-    regerror(ret, &WORDS, errorbuf, sizeof(errorbuf));
-    ZF_LOGW("Regex %s failed to compile: %s", words, errorbuf);
-    return 0;
-  };
-
-  if (ret = regcomp(&WORD, word, REG_EXTENDED | REG_NEWLINE)) {
-    regerror(ret, &WORD, errorbuf, sizeof(errorbuf));
-    ZF_LOGW("Regex %s failed to compile: %s", word, errorbuf);
-    return 0;
-  };
-
-  return 1;
-}
-
-int regmatch(regex_t *preg, const char *string, size_t nmatch,
-             regmatch_t pmatch[], int eflags) {
-  // returns number of matches found.  (Max nmatch)
-  int matches = 0;
-  int offset = 0;
-
-  while (matches < nmatch) {
-    int ret = regexec(preg, string + offset, nmatch - matches, pmatch + matches,
-                      eflags);
-    if (!ret) {
-      int current = offset;
-      offset += pmatch[matches].rm_eo;
-      pmatch[matches].rm_so += current;
-      pmatch[matches].rm_eo += current;
-      matches++;
-    } else if (ret == REG_NOMATCH) {
-      break;
-    } else {
-      break;
-    }
-  }
-  return matches;
-}
-
-#define MAX_MATCH 32
-regmatch_t rxmatch[MAX_MATCH];
-
-int rx_match(regex_t *regex, const char *buffer) {
-  int ret;
-
-  ret = regmatch(regex, buffer, MAX_MATCH, rxmatch, 0);
-  if (0) {
-    for (int i = 0; i < ret; i++) {
-      ZF_LOGI("%d : (%d-%d)", i, rxmatch[i].rm_so, rxmatch[i].rm_eo);
-    }
-  }
-  return ret;
-}
-#endif
+// char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
 
 /**
  * random_activate()
@@ -160,6 +90,8 @@ int random_activate(int w) {
   return 0;
 }
 
+#ifdef UNWANTED
+
 /*
   word_state():    // deprecated
 
@@ -211,12 +143,6 @@ Given a buffer and length, mangle away.
 
 toupper, tolower, flipper
  */
-int word_mangler(std::string &buffer, std::string& word, std::string& text, std::vector<int> &text_offsets, std::pair<int,int> pos_len) {
-
-}
-
-#ifdef UNWANTED
-
 int word_mangler(char *buffer, int len) {
   int p;
   int count = 0;
@@ -308,9 +234,7 @@ int buffer_insert(char *buffer, int len, int max_length, int pos,
   strncpy(buffer + pos, insert, strlen(insert));
   return 1;
 }
-#endif
 
-#ifdef UNWANTED
 /*
  * The buffer that we've been given is much larger now.
  *
@@ -674,9 +598,7 @@ int mangle(int fd, const char *buffer, int len) {
   };
   return need_render && mangled;
 }
-#endif
 
-/*
 int harry_happens(time_t *last_event, int wakeup) {
   time_t now = time(NULL);
   int elapsed = now - *last_event;
@@ -688,32 +610,7 @@ int harry_happens(time_t *last_event, int wakeup) {
   }
   return 0;
 }
-*/
-
-/*
- * find_words()
- * 
- * This uses regex to look for words where a word is defined as one or more letter.
- * 
- * We do throw out words with a len of 3.  (That would be "A B".  We don't want that.)
- * We return a vector of pairs (position, length), and the number of matches.
- */
-int find_words(std::string &text, std::vector<std::pair<int, int>> &words) {
-  words.clear();
-// Yes I want & in there so BZ&BZ BBS matches.  
-  std::regex words_re("[a-zA-Z&]+( [a-zA-Z&]+)+");
-  int count = 0;
-
-  for (auto it = std::sregex_iterator(text.begin(), text.end(), words_re);
-       it != std::sregex_iterator(); ++it) {
-    if (it->length() > 3) {
-      words.push_back(std::make_pair(it->position(), it->length()));
-      count++;
-    }
-  }
-
-  return count;
-}
+#endif // UNWANTED
 
 int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
   static int ANSI_CLS_count = 0;
@@ -879,7 +776,7 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
 
 int mangle(int fd, std::string &buffer) {
   // a simple default for now.
-  ZF_LOGV_MEM(buffer.data(), buffer.size(), "mangle(%d): %lu bytes", fd,
+  ZF_LOGI_MEM(buffer.data(), buffer.size(), "mangle(%d): %lu bytes", fd,
               buffer.size());
 
   int need_render = 0;
@@ -914,12 +811,14 @@ int mangle(int fd, std::string &buffer) {
   text_offsets.clear();
 
   for (stri = 0; stri < buffer.size(); ++stri) {
-      termchar tc = console_char(&console, work[stri]);
+    // why wasn't \x1b[?1000h   handled by console_char?
+    // what happened to \x0c ?   It is there.
+    termchar tc = console_char(&console, work[stri]);
 
     if (tc.in_ansi) {
       if (tc.ansi != START) {
         // Ok, this is something.  What is it?
-        // ZF_LOGV("ANSI type %d at %lu", tc.ansi, stri);
+        ZF_LOGD("ANSI type %d at %lu", tc.ansi, stri);
         switch (tc.ansi) {
         case CURSOR:
         case CLEAR:
@@ -944,12 +843,11 @@ int mangle(int fd, std::string &buffer) {
     }
   }
 
-  // The current situation:
   ZF_LOGD_MEM(buffer.data(), buffer.size(), "Buffer:");
   ZF_LOGD_MEM(work.data(), work.size(), "Work:");
   ZF_LOGD_MEM(text.data(), text.size(), "Text Buffer:");
 
-  // output vector information
+  // Output vector contents
   std::ostringstream oss;
   int comma = 0;
   for (auto it = std::begin(text_offsets); it != std::end(text_offsets); ++it) {
@@ -970,36 +868,15 @@ int mangle(int fd, std::string &buffer) {
   std::string vector_output = oss.str();
   ZF_LOGD("Vector: %s", vector_output.c_str());
 
-  // find matches
-  std::vector<std::pair<int, int>> words;
-
-  int found = find_words(text, words);
-  ZF_LOGD("Found %d word groups in text.", found);
-
-  if (found > 0) {
-    for (int i = 0; i < found; i++) {
-      std::pair<int, int> pos_len = words[i];
-
-      // Yes!  Be random!
-      if (random_activate(8)) {
-        int c = word_mangler(buffer, work, text, text_offsets, pos_len);
-        /*
-        int c = word_mangler(play + rxmatch[i].rm_so,
-                             rxmatch[i].rm_eo - rxmatch[i].rm_so);*/ 
-        if (c) {
-          mangled++;
-          mangled_chars += c;
-        }
-      }
+  // reset oss (if we need it)
+  oss.str(std::string());
+  oss.clear();
 
-      if (random_activate(4)) {
-        /*
-        word_wrangler(play + rxmatch[i].rm_so,
-                      rxmatch[i].rm_eo - rxmatch[i].rm_so);
-                      */
-      }
-    }
-  }
+  // Begin the mangle process 2.0
+  {
+    ZF_LOGD("CharMan");
+    CharMan cm(buffer, work, text, text_offsets);
+  };
 
   if (need_render) {
     render(fd, buffer);

+ 3 - 1
wordplay.h

@@ -1,8 +1,10 @@
 #ifndef WORDPLAY_H
 #define WORDPLAY_H
+#include <string>
+
 void harry_idle_event(int fd);
 void init_harry();
 /* int init_regex(void); */
 /* int mangle(int fd, const char *buffer, int len); */
-int mangle(int fd, string& buffer);
+int mangle(int fd, std::string &buffer);
 #endif