Browse Source

Fixed issue with charman, inserting at end of string.

Steve Thielemann 4 years ago
parent
commit
98c59dcca7
7 changed files with 74 additions and 20 deletions
  1. 1 1
      CMakeLists.txt
  2. 27 9
      charman.cpp
  3. 6 0
      hharry.cpp
  4. 11 0
      logs_utils.cpp
  5. 8 0
      logs_utils.h
  6. 1 1
      utils.h
  7. 20 9
      wordplay.cpp

+ 1 - 1
CMakeLists.txt

@@ -103,7 +103,7 @@ add_custom_command(
     WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
 )
 
-add_executable(hharry hharry.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp images.h wordplay.cpp charman.cpp)
+add_executable(hharry hharry.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp images.h wordplay.cpp charman.cpp logs_utils.cpp)
 target_link_libraries(hharry util)
 target_link_libraries(hharry zf_log)
 # target_compile_definitions(hharry PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_INFO)

+ 27 - 9
charman.cpp

@@ -9,8 +9,13 @@
 
 void CharMan::validate(void) {
   bool valid = true;
+  ZF_LOGE("validate: text_offsets %d", (int)text_offsets.size());
+  ZF_LOGE("validate work size %d, buffer size %d, text size %d",
+          (int)work.size(), (int)buffer.size(), (int)text.size());
+
   for (int x = 0; x < text_offsets.size(); ++x) {
     int offset = text_offsets[x];
+    ZF_LOGE("%d : %d", x, offset);
 
     if (offset >= 0) {
       if (text[x] != work[offset]) {
@@ -69,10 +74,12 @@ void CharMan::regular_expressions(void) {
   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()));
+
     int pos = it->position(), len = it->length();
     ZF_LOGD("pos %d len %d (%s)", pos, len,
             this->text.substr(pos, len).c_str());
+    if (len > 4)
+      pos_len.push_back(std::make_pair(it->position(), it->length()));
   }
 }
 
@@ -89,9 +96,18 @@ void CharMan::set(int pos, char ch) {
 
 void CharMan::insert(int pos, std::string str) {
   int len = str.size();
-  int idx = this->text_offsets[pos];
-  ZF_LOGE("insert( POS %d, LEN %d, IDX %d, %s)", pos, len, idx, str.c_str());
-  diagnostics();
+  // What happens if pos is at the end of the buffer?
+  int idx;
+  if (pos == text_offsets.size()) {
+    // Ok, this is at the very end of the buffer, which is beyond what the
+    // vector is holding.
+    idx = this->text_offsets[pos - 1] + 1;
+    ZF_LOGE("Use %d for idx", idx);
+  } else
+    idx = this->text_offsets[pos];
+
+  // ZF_LOGE("insert( POS %d, LEN %d, IDX %d, %s)", pos, len, idx, str.c_str());
+  // diagnostics();
   std::string blank(len, ' ');
   // Don't insert into text.
   // Insert blank into work.
@@ -108,8 +124,8 @@ void CharMan::insert(int pos, std::string str) {
         *it += len;
       }
     }
-    ZF_LOGE("Indexes updated... check your work");
-    diagnostics();
+    // ZF_LOGE("Indexes updated... check your work");
+    // diagnostics();
   }
 }
 
@@ -214,8 +230,10 @@ int CharMan::word_tangler(std::pair<int, int> pos_len) {
     buffer << "^S" << r;
     tangle = buffer.str();
     std::string reset = "^R0^S0";
+    ZF_LOGD("insert reset %s", reset.c_str());
     this->insert(p + len, reset);
     this->validate();
+    ZF_LOGD("insert tangle %s", tangle.c_str());
     this->insert(p, tangle);
     this->validate();
     return 1;
@@ -246,7 +264,7 @@ this->text_offsets = text_offsets;
   if (pos_len.size() > 0) {
     for (int i = 0; i < pos_len.size(); ++i) {
       int active = 0;
-      if (random_activate(level * 2)) { // 8
+      if (random_activate((level + 1) / 2)) { // 8
         int c = word_mangler(pos_len[i]);
         if (c) {
           active = 1;
@@ -255,14 +273,14 @@ this->text_offsets = text_offsets;
         }
       }
 
-      if (random_activate(level)) { // 4
+      if (random_activate((level + 1) / 2)) { // 4
         if (word_wrangler(pos_len[i])) {
           this->mangle_count++;
           active = 1;
         }
       }
 
-      if (!active && random_activate(level)) {
+      if (!active && random_activate((level + 1) / 2)) {
         if (word_tangler(pos_len[i])) {
           this->need_render = 1;
         }

+ 6 - 0
hharry.cpp

@@ -330,9 +330,15 @@ int main(int argc, char *argv[]) {
     if (strncmp("-U", argv[x], 2) == 0) {
       username.assign(argv[x] + 2);
     }
+    /* Changed in latest version
     if (strncmp("-SL", argv[x], 3) == 0) {
       node = atoi(argv[x] + 3) + 1;
     }
+    */
+    // -TID7, -TID9, -TID11 for node 1, 2, 3
+    if (strncmp("-TID", argv[x], 4) == 0) {
+      node = (atoi(argv[x] + 4) - 5) / 2;
+    }
   }
 
   if (node == -1) {

+ 11 - 0
logs_utils.cpp

@@ -0,0 +1,11 @@
+#include "logs_utils.h"
+#include "utils.h"
+#include "zf_log.h"
+#include <string>
+
+void ZF_LOGV_LR(const char *desc, std::string &buffer, int len) {
+  ZF_LOGV("%s", desc);
+  for (size_t i = 0; i < buffer.length(); i += len) {
+    ZF_LOGV("%s", logrepr(buffer.substr(i, len).c_str()));
+  }
+}

+ 8 - 0
logs_utils.h

@@ -0,0 +1,8 @@
+#ifndef LOGS_UTILS_H
+#define LOGS_UTILS_H
+
+#include <string>
+
+void ZF_LOGV_LR(const char *desc, std::string &buffer, int len = 64);
+
+#endif

+ 1 - 1
utils.h

@@ -35,7 +35,7 @@ void string_toupper(std::string &str);
 void string_trim(std::string &value);
 std::map<std::string, std::string> read_configfile(std::string filename);
 extern std::map<std::string, std::string> CONFIG;
-bool replace(std::string& str, const std::string& from, const std::string& to);
+bool replace(std::string &str, const std::string &from, const std::string &to);
 
 int harry_level(void);
 

+ 20 - 9
wordplay.cpp

@@ -1,6 +1,7 @@
 #include "charman.h"
 #include "images.h"
 #include "lastseen.h"
+#include "logs_utils.h"
 #include "render.h"
 #include "terminal.h"
 #include "utils.h"
@@ -10,9 +11,8 @@
 #include <sstream>
 #include <string.h>
 #include <string>
-#include <vector>
-
 #include <unistd.h> // write
+#include <vector>
 
 extern struct console_details console;
 
@@ -92,7 +92,7 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
     restore_color.assign(color_restore(&temp_console));
     */
 
-    if (random_activate(3)) {
+    if (random_activate((level + 1) / 2)) {
       std::ostringstream display;
       int needs_cls = 0;
 
@@ -166,7 +166,7 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
       work.insert(pos, std::string(display_output.size(), ' '));
       return 1; // need_render = 1;
     } else {
-      if (random_activate(4)) {
+      if (random_activate((level + 1) / 2)) {
         int r;
         std::ostringstream display;
 
@@ -241,7 +241,8 @@ 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("mangle [%s]", logrepr(buffer.c_str()));
+  // ZF_LOGV("mangle [%s]", logrepr(buffer.c_str()));
+  ZF_LOGV_LR("mangle:", buffer);
   /*
   ZF_LOGV_MEM(buffer.data(), buffer.size(), "mangle(%d): %lu bytes", fd,
               buffer.size());
@@ -272,7 +273,12 @@ int mangle(int fd, std::string &buffer) {
     static int bbs_match = 0;
 
     if (!bbs_match) {
-      std::regex bbs_what("Mystic BBS Version [0-9.]+ A[0-9]+");
+      std::regex bbs_what(
+          "(?:Mystic BBS Version [0-9.]+ A[0-9]+)|(?:Mystic BBS "
+          "v[0-9.]+ A[0-9]+ for Linux Node [0-9]+)");
+      // std::regex bbs_what("Mystic BBS Version [0-9.]+ A[0-9]+");
+      // std::regex_constants::ECMAScript);
+
       // Mystic BBS v[0-9.]+ A[0-9]+ for Linux Node [0-9]+
 
       if (std::regex_search(buffer, match, bbs_what)) {
@@ -402,9 +408,14 @@ int mangle(int fd, std::string &buffer) {
     }
   }
 
-  ZF_LOGV("Buffer: %s", logrepr(buffer.c_str()));
-  ZF_LOGV("Work: %s", logrepr(work.c_str()));
-  ZF_LOGV("Text: %s", logrepr(text.c_str()));
+  // ZF_LOGV_LR("mangle:", buffer);
+  ZF_LOGV_LR("Buffer:", buffer);
+  ZF_LOGV_LR("Work:", work);
+  ZF_LOGV_LR("Text:", text);
+  // ZF_LOGV("Buffer: %s", logrepr(buffer.c_str()));
+  // ZF_LOGV("Work: %s", logrepr(work.c_str()));
+  // ZF_LOGV("Text: %s", logrepr(text.c_str()));
+
   // ZF_LOGV_MEM(buffer.data(), buffer.size(), "Buffer:");
   // ZF_LOGV_MEM(work.data(), work.size(), "Work:");
   // ZF_LOGV_MEM(text.data(), text.size(), "Text Buffer:");