Browse Source

Updated: moved more to utils.

bugz 4 years ago
parent
commit
3dfca8e138
5 changed files with 30 additions and 222 deletions
  1. 2 2
      CMakeLists.txt
  2. 1 91
      ansi-to-src.cpp
  3. 1 129
      mystic.cpp
  4. 25 0
      utils.cpp
  5. 1 0
      utils.h

+ 2 - 2
CMakeLists.txt

@@ -78,7 +78,7 @@ add_subdirectory(zf_log)
 # )
 # add_executable(yourProj ${SOURCES})
 
-add_executable(mystic mystic.cpp lastseen.cpp terminal.cpp render.cpp)
+add_executable(mystic mystic.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp)
 target_link_libraries(mystic util)
 target_link_libraries(mystic zf_log)
 target_compile_definitions(mystic PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_VERBOSE)
@@ -86,7 +86,7 @@ target_compile_definitions(mystic PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_VERBOSE)
 add_executable(try-re try-re.c)
 
 add_executable(ansi-color ansi-color.c)
-add_executable(ansi-to-src ansi-to-src.cpp)
+add_executable(ansi-to-src ansi-to-src.cpp utils.cpp)
 
 add_executable(images images.cpp images.h)
 add_executable(render-test render-test.cpp render.cpp terminal.cpp utils.cpp)

+ 1 - 91
ansi-to-src.cpp

@@ -4,97 +4,7 @@
 
 #define ZF_LOGD(...)
 
-/**
- * Display a repr of the given string.
- *
- * This converts most \n\r\v\f\t codes,
- * defaults to \xHH (hex value).
- */
-char *repr(const char *data) {
-  static char buffer[40960];
-  char *cp;
-
-  strcpy(buffer, data);
-  cp = buffer;
-  while (*cp != 0) {
-    char c = *cp;
-
-    if (c == ' ') {
-      cp++;
-      continue;
-    };
-    /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
-     * horizontal tab ('\t'), and vertical tab ('\v') */
-    if (strchr("\f\n\r\t\v\?", c) != NULL) {
-      memmove(cp + 1, cp, strlen(cp) + 1);
-      *cp = '\\';
-      cp++;
-      switch (c) {
-      case '\f':
-        *cp = 'f';
-        cp++;
-        break;
-      case '\n':
-        *cp = 'n';
-        cp++;
-        break;
-      case '\r':
-        *cp = 'r';
-        cp++;
-        break;
-      case '\t':
-        *cp = 't';
-        cp++;
-        break;
-      case '\v':
-        *cp = 'v';
-        cp++;
-        break;
-      default:
-        *cp = '?';
-        cp++;
-        break;
-      }
-      continue;
-    }
-
-    if (c == '\\') {
-      memmove(cp + 1, cp, strlen(cp) + 1);
-      *cp = '\\';
-      cp += 2;
-      continue;
-    }
-    if (c == '"') {
-      memmove(cp + 1, cp, strlen(cp) + 1);
-      *cp = '\\';
-      cp += 2;
-      continue;
-    }
-    if (strchr("[()]{}:;,.<>?!@#$%^&*", c) != NULL) {
-      cp++;
-      continue;
-    }
-    if (strchr("0123456789", c) != NULL) {
-      cp++;
-      continue;
-    }
-    if (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c) !=
-        NULL) {
-      cp++;
-      continue;
-    }
-
-    // Ok, default to \xHH output.
-    memmove(cp + 3, cp, strlen(cp) + 1);
-    char buffer[10];
-    sprintf(buffer, "\\x%02x", (int)c & 0xff);
-    strncpy(cp, buffer, 4);
-    cp += 4;
-    continue;
-  }
-
-  return buffer;
-}
+#include "utils.h"
 
 int read_file(const char *filename) {
   FILE *fp;

+ 1 - 129
mystic.cpp

@@ -76,115 +76,12 @@ static void file_output_open(const char *const log_path) {
 
 struct console_details console;
 
-/**
- * Display a repr of the given string.
- *
- * This converts most \n\r\v\f\t codes,
- * defaults to \xHH (hex value).
- */
-const char *repr(const char *data) {
-  static char buffer[4096];
-  char *cp;
-
-  strcpy(buffer, data);
-  cp = buffer;
-  while (*cp != 0) {
-    char c = *cp;
-
-    if (isspace(c)) {
-      if (c == ' ') {
-        cp++;
-        continue;
-      };
-      /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
-       * horizontal tab ('\t'), and vertical tab ('\v') */
-      memmove(cp + 1, cp, strlen(cp) + 1);
-      *cp = '\\';
-      cp++;
-      switch (c) {
-      case '\f':
-        *cp = 'f';
-        cp++;
-        break;
-      case '\n':
-        *cp = 'n';
-        cp++;
-        break;
-      case '\r':
-        *cp = 'r';
-        cp++;
-        break;
-      case '\t':
-        *cp = 't';
-        cp++;
-        break;
-      case '\v':
-        *cp = 'v';
-        cp++;
-        break;
-      default:
-        *cp = '?';
-        cp++;
-        break;
-      }
-      continue;
-    }
-
-    if (isprint(c)) {
-      cp++;
-      continue;
-    };
-
-    // Ok, default to \xHH output.
-    memmove(cp + 3, cp, strlen(cp) + 1);
-    *cp = '\\';
-    cp++;
-    *cp = 'x';
-    cp++;
-    char buffer[3];
-    sprintf(buffer, "%02x", (int)c & 0xff);
-    *cp = buffer[0];
-    cp++;
-    *cp = buffer[1];
-    cp++;
-    continue;
-  }
-
-  return buffer;
-}
+#include "utils.h"
 
 // END LOGGING
 
 #include "lastseen.h"
 
-// http://c-faq.com/lib/randrange.html
-int randint(int N) { return rand() / (RAND_MAX / N + 1); }
-
-// http://c-faq.com/lib/randrange.html
-// numbers in the range [M, N] could be generated with something like
-
-int randrange(int M, int N) {
-  return M + rand() / (RAND_MAX / (N - M + 1) + 1);
-}
-
-/*
- * strnstr()
- *
- * buffer safe version that looks for a string.
- */
-const char *strnstr(const char *source, int len, const char *needle) {
-  int pos;
-
-  for (pos = 0; pos < len; pos++) {
-    if (source[pos] == needle[0]) {
-      if (strncmp(source + pos, needle, strlen(needle)) == 0) {
-        return source + pos;
-      }
-    }
-  }
-  return NULL;
-}
-
 /*
 What is the name of the actual, real Mystic executable
 that we'll be executing and mangling?
@@ -964,31 +861,6 @@ ZModem:
 
  */
 
-/*
- * rstrnstr() Reverse string find in a string
- *
- * This obeys the len, and handles nulls in buffer.
- * find is a c-string (null terminated)
- */
-int rstrnstr(const char *buffer, int len, const char *find) {
-  int flen = strlen(find);
-
-  if (len < flen) {
-    // I can't find a string in a buffer smaller then it is!
-    return -1;
-  }
-  int pos = len - flen;
-  while (pos > 0) {
-    if (buffer[pos] == find[0]) {
-      // First chars match, check them all.
-      if (strncmp(buffer + pos, find, flen) == 0) {
-        return pos;
-      }
-    }
-    pos--;
-  }
-  return -1;
-}
 
 int main(int argc, char *argv[]) {
   int master;

+ 25 - 0
utils.cpp

@@ -124,4 +124,29 @@ const char *strnstr(const char *source, int len, const char *needle) {
   return NULL;
 }
 
+/*
+ * rstrnstr() Reverse string find in a string
+ *
+ * This obeys the len, and handles nulls in buffer.
+ * find is a c-string (null terminated)
+ */
+int rstrnstr(const char *buffer, int len, const char *find) {
+  int flen = strlen(find);
+
+  if (len < flen) {
+    // I can't find a string in a buffer smaller then it is!
+    return -1;
+  }
+  int pos = len - flen;
+  while (pos > 0) {
+    if (buffer[pos] == find[0]) {
+      // First chars match, check them all.
+      if (strncmp(buffer + pos, find, flen) == 0) {
+        return pos;
+      }
+    }
+    pos--;
+  }
+  return -1;
+}
 

+ 1 - 0
utils.h

@@ -17,6 +17,7 @@ int randrange(int M, int N);
  */
 char *repr(const char *data);
 const char *strnstr(const char *source, int len, const char *needle);
+int rstrnstr(const char *buffer, int len, const char *find);
 
 #endif