Browse Source

Updated using C++ objects. Tests next to do.

Steve Thielemann 5 years ago
parent
commit
5b83c281c0
4 changed files with 59 additions and 41 deletions
  1. 10 1
      CMakeLists.txt
  2. 25 0
      lastseen.cpp
  3. 15 0
      lastseen.h
  4. 9 40
      mystic.cpp

+ 10 - 1
CMakeLists.txt

@@ -69,7 +69,16 @@ add_subdirectory(zf_log)
 # add_executable(mytest tester.cpp)
 # target_link_libraries(mytest gmock_main)
 
-add_executable(mystic mystic.cpp)
+# Here's how to add all *.h and *.cpp files
+# to a project:
+#
+# file(GLOB SOURCES
+#     header-folder/*.h
+#     source-folder/*.cpp
+# )
+# add_executable(yourProj ${SOURCES})
+
+add_executable(mystic mystic.cpp lastseen.cpp)
 target_link_libraries(mystic util)
 target_link_libraries(mystic zf_log)
 target_compile_definitions(mystic PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_VERBOSE)

+ 25 - 0
lastseen.cpp

@@ -0,0 +1,25 @@
+#include "lastseen.h"
+
+LastSeen::LastSeen(int max_nondups) {
+  this->max = max_nondups;
+  this->tracker = new int[max_nondups];
+  for (int i = 0; i < max_nondups; i++) {
+    this->tracker[i] = -1;
+  }
+}
+
+LastSeen::~LastSeen() { delete this->tracker; }
+
+bool LastSeen::seen_before(int this_one) {
+  int i;
+  for (i = 0; i < this->max; i++) {
+    if (this->tracker[i] == this_one)
+      return true;
+  }
+  // Ok, it hasn't been seen before.
+  for (i = 0; i < this->max - 1; i++) {
+    this->tracker[i] = this->tracker[i + 1];
+  };
+  this->tracker[i] = this_one;
+  return false;
+}

+ 15 - 0
lastseen.h

@@ -0,0 +1,15 @@
+#ifndef LASTSEEN_H
+#define LASTSEEN_H
+
+class LastSeen {
+private:
+  int *tracker;
+  int max;
+
+public:
+  LastSeen(int max_nondups);
+  ~LastSeen();
+  bool seen_before(int this_one);
+};
+
+#endif

+ 9 - 40
mystic.cpp

@@ -45,6 +45,8 @@ static void file_output_open(const char *const log_path) {
 
 // END LOGGING
 
+#include "lastseen.h"
+
 /*
 What is the name of the actual, real Mystic executable
 that we'll be executing and mangling?
@@ -946,43 +948,9 @@ void init_have_seen(int *list, int len) {
 
  */
 
-class LastSeen {
-private:
-  int *tracker;
-  int max;
-
-public:
-  LastSeen(int max_nondups);
-  ~LastSeen();
-  bool seen_before(int this_one);
-};
-
-LastSeen::LastSeen(int max_nondups) {
-  this->max = max_nondups;
-  this->tracker = new int[max_nondups];
-  for (int i = 0; i < max_nondups; i++) {
-    this->tracker[i] = -1;
-  }
-}
-
-LastSeen::~LastSeen() { delete this->tracker; }
-
-bool LastSeen::seen_before(int this_one) {
-  int i;
-  for (i = 0; i < this->max; i++) {
-    if (this->tracker[i] == this_one)
-      return true;
-  }
-  // Ok, it hasn't been seen before.
-  for (i = 0; i < this->max - 1; i++) {
-    this->tracker[i] = this->tracker[i + 1];
-  };
-  this->tracker[i] = this_one;
-  return false;
-}
-
-#define MAX_HARRY_EVENT_DUPS 2
-int last_seen_harry_event[MAX_HARRY_EVENT_DUPS];
+// #define MAX_HARRY_EVENT_DUPS 2
+// int last_seen_harry_event[MAX_HARRY_EVENT_DUPS];
+LastSeen last_seen_harry_event(2);
 
 #ifdef CPP_MADMAN_STL_CODE
 
@@ -1019,9 +987,10 @@ void harry_idle_event(int fd) {
 
   do {
     r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
-  } while (have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS, r));
+  } while (!last_seen_harry_event.seen_before(r));
 
-  ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0], last_seen_harry_event[1]);
+  // ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0],
+  // last_seen_harry_event[1]);
 
   cp = phrases[r];
   int color = random() % 16;
@@ -1037,7 +1006,7 @@ void harry_idle_event(int fd) {
 }
 
 void init_harry() {
-  init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
+  // init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
   // ZF_LOGD("init => %d %d", last_seen_harry_event[0],
   // last_seen_harry_event[1]);
   console_init(&console);