Selaa lähdekoodia

Removing snprintf for ostringstream.

Yes, sprintf can be done in C++.
Learn how, today!
bugz 4 vuotta sitten
vanhempi
commit
197911c181
1 muutettua tiedostoa jossa 83 lisäystä ja 43 poistoa
  1. 83 43
      wordplay.cpp

+ 83 - 43
wordplay.cpp

@@ -3,7 +3,9 @@
 #include "render.h"
 #include "terminal.h"
 #include "utils.h"
+#include <iomanip>
 #include <regex.h>
+#include <sstream>
 #include <string.h>
 #include <string>
 #include <vector>
@@ -24,32 +26,37 @@ extern struct console_details console;
  */
 void harry_idle_event(int fd) {
   // Make something happen
-  char buffer[100];
-  int slen;
+  std::ostringstream buffer;
   int r;
   // This is no where near finished, BUT!
+  // Do not put any ^ codes in these -- the strlen() would be wrong.
   const char *phrases[] = {"Hahaha",    "Snicker, snicker", "Boo!",
                            "MeOW",      "I see U",          "Arrooo!",
                            "Ahh-wooo!", "Aaaooo!"};
-  const char *cp;
+
   static LastSeen last_seen_harry_event(2);
 
   do {
     r = randint((sizeof(phrases) / sizeof(char *)));
   } while (last_seen_harry_event.seen_before(r));
 
-  cp = phrases[r];
   int color = random() % 15 + 1;
+  // %02d = std::setfill('0') << std::setw(2) << (int)
 
+  buffer << "^S2^C" << std::setfill('0') << std::setw(2) << color << phrases[r]
+         << "^P2^CR^D" << std::setw(2) << strlen(phrases[r]);
+
+  /*
   slen = snprintf(buffer, sizeof(buffer), "^S2^C%02d%s^P2^CR^D%02d", color, cp,
                   (int)strlen(cp));
   if (slen >= sizeof(buffer)) {
     ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(buffer));
     buffer[0] = 0;
   }
-  ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
-  std::string str(buffer);
-  // render(fd, buffer, strlen(buffer));
+  */
+
+  std::string str = buffer.str();
+  ZF_LOGD("harry_event: render(%d, \"%s\")", fd, str.c_str());
   render(fd, str);
 }
 
@@ -680,12 +687,11 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
     struct console_details temp_console;
     memcpy(&temp_console, &console, sizeof(console));
     console_receive(&temp_console, buffer.substr(0, pos));
-    char restore_color[30];
-    strncpy(restore_color, color_restore(&temp_console), sizeof(restore_color));
+    std::string restore_color;
+    restore_color.assign(color_restore(&temp_console));
 
     if (random_activate(3)) {
-      char display[100] = "";
-      int slen;
+      std::ostringstream display;
       int needs_cls = 0;
 
       struct image {
@@ -709,7 +715,7 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         r = randint((sizeof(images) / sizeof(image)));
       } while (last_files.seen_before(r));
 
-      char fgoto[32];
+      std::string fgoto;
 
       if (!images[r].cls) {
         int x = 0, y = 0;
@@ -717,15 +723,17 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         x = randint(79 - images[r].width) + 1;
         y = randint(24 - images[r].size) + 1;
 
-        int slen;
+        display << "^f" << std::setfill('0') << std::setw(2) << x
+                << std::setw(2) << y << "\x1b[1;1H";
+        fgoto = display.str();
+        // reset display
+        display.str(std::string());
+        display.clear();
+
         // render image, home cursor
-        slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d\x1b[1;1H", x, y);
-        if (slen >= sizeof(fgoto)) {
-          ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(fgoto));
-          fgoto[0] = 0;
-        }
+        // slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d\x1b[1;1H", x, y);
       } else {
-        strcpy(fgoto, "^F");
+        fgoto.assign("^F");
       }
 
       needs_cls = images[r].cls;
@@ -741,23 +749,25 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
       // Ok, yes, there's no filename being sent.  :P
       render_image(images[r].lines, images[r].size);
 
-      slen = snprintf(display, sizeof(display), "%s%s%s^P3",
-                      needs_cls ? "\x1b[2J" : "", fgoto, restore_color);
-      if (slen >= sizeof(display)) {
-        ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
-        display[0] = 0;
-      }
-      ZF_LOGI("mangle(ANSI_CLS): %d file inserted %s", r, repr(display));
+      display << (needs_cls ? "\x1b[2J" : "") << fgoto << restore_color
+              << "^P3";
+
+      // slen = snprintf(display, sizeof(display), "%s%s%s^P3",
+      //                 needs_cls ? "\x1b[2J" : "", fgoto, restore_color);
+
+      std::string display_output = display.str();
+
+      ZF_LOGI("mangle(ANSI_CLS): %d file inserted %s", r,
+              repr(display_output.c_str()));
 
       // Move the buffer so there's room for the display string.
-      buffer.insert(pos, display);
-      work.insert(pos, std::string(strlen(display), ' '));
+      buffer.insert(pos, display_output);
+      work.insert(pos, std::string(display_output.size(), ' '));
       return 1; // need_render = 1;
     } else {
       if (random_activate(4)) {
         int r;
-        char display[100] = "";
-        int slen;
+        std::ostringstream display;
 
         const char *phrasing[] = {
             "^R1Haha^P1ha^P1ha", "Poof!", "Got U", "Anyone there?",
@@ -794,19 +804,23 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         */
 
         if (strncmp(phrasing[r], "^G", 2) == 0) {
+          display << "^S3^P1" << phrasing[r] << "^S0^R0" << restore_color
+                  << "^P1^G0101";
           // This starts with a GOTO, so don't use our random position
-          slen = snprintf(display, sizeof(display), "^S3^P1%s^S0^R0%s^P1^G0101",
-                          phrasing[r], restore_color);
+          // slen = snprintf(display, sizeof(display),
+          // "^S3^P1%s^S0^R0%s^P1^G0101",
+          //                 phrasing[r], restore_color);
         } else {
-          slen = snprintf(display, sizeof(display),
-                          "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
-                          color, phrasing[r], restore_color);
+          display << "^G" << std::setw(2) << std::setfill('0') << x
+                  << std::setw(2) << y << "^S3^C" << std::setw(2) << color
+                  << "^P1" << phrasing[r] << "^S0^R0" << restore_color
+                  << "^P1^G0101";
+          // slen = snprintf(display, sizeof(display),
+          //                "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
+          //                color, phrasing[r], restore_color);
         };
-        if (slen >= sizeof(display)) {
-          ZF_LOGE("snprintf %d > size %d (Phrase: %d, %s)", slen,
-                  (int)sizeof(display), r, phrasing[r]);
-          display[0] = 0;
-        }
+
+        std::string display_output = display.str();
 
         // sprintf(display, "^P1^S3^C%02d%s^S0^R0%s^P1", color, phrasing[r],
         // restore_color);
@@ -814,11 +828,11 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         // number picked and what that is
         ZF_LOGD("mangle(ANSI_CLS): Inserted color=%02d r=%d phrase='%s'", color,
                 r, phrasing[r]);
-        ZF_LOGI("mangle(ANSI_CLS): %d %s", r, repr(display));
+        ZF_LOGI("mangle(ANSI_CLS): %d %s", r, repr(display_output.c_str()));
 
         // Move the buffer so there's room for the display string.
-        buffer.insert(pos, display);
-        work.insert(pos, std::string(strlen(display), ' '));
+        buffer.insert(pos, display_output);
+        work.insert(pos, std::string(display_output.size(), ' '));
         return 1; // need_render = 1;
       }
     }
@@ -862,6 +876,8 @@ int mangle(int fd, std::string buffer) {
   text_offsets.clear();
 
   for (stri = 0; stri < buffer.size(); ++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) {
@@ -892,8 +908,32 @@ int mangle(int fd, std::string buffer) {
     }
   }
 
+  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:");
 
+  std::ostringstream oss;
+  int comma = 0;
+  for (auto it = std::begin(text_offsets); it != std::end(text_offsets); ++it) {
+    if (comma) {
+      oss << ", ";
+    };
+    comma++;
+    oss << *it;
+    if (comma == 30) {
+      std::string temp_output = oss.str();
+      ZF_LOGD("Vector: %s", temp_output.c_str());
+      // reset ostringstream
+      oss.str(std::string());
+      oss.clear();
+      comma = 0;
+    }
+  }
+  std::string vector_output = oss.str();
+
+  ZF_LOGD("Vector: %s", vector_output.c_str());
+
   if (need_render) {
     render(fd, buffer);
   } else {