Browse Source

* Simplify and fix dumplots() to properly handle const data / static strings

Bryan Drewery 16 năm trước cách đây
mục cha
commit
8d3d79f389
2 tập tin đã thay đổi với 6 bổ sung38 xóa
  1. 4 37
      src/dccutil.c
  2. 2 1
      src/dccutil.h

+ 4 - 37
src/dccutil.c

@@ -210,52 +210,19 @@ colorbuf(char *buf, size_t len, int idx, size_t bufsiz)
 
 /* Dump a potentially super-long string of text.
  */
-void dumplots(int idx, const char *prefix, const char *data)
+void dumplots(int idx, const char *prefix, const bd::String data)
 {
   if (unlikely(!*data)) {
     dprintf(idx, "%s\n", prefix);
     return;
   }
 
-  char *p = (char*)data, *q = NULL, *n = NULL, c = 0;
   const size_t max_data_len = 120 - strlen(prefix);
+  bd::Array<bd::String> lines = data.split('\n');
 
-  while ((strlen(p) - ansi_len(p)) > max_data_len) {
-    q = p + max_data_len;
-    /* Search for embedded linefeed first */
-    n = strchr(p, '\n');
-    if (n && n < q) {
-      /* Great! dump that first line then start over */
-      *n = 0;
-      dprintf(idx, "%s%s\n", prefix, p);
-      *n = '\n';
-      p = n + 1;
-    } else {
-      /* Search backwards for the last space */
-      while (*q != ' ' && q != p)
-        q--;
-      if (q == p)
-        q = p + max_data_len;
-      c = *q;
-      *q = 0;
-      dprintf(idx, "%s%s\n", prefix, p);
-      *q = c;
-      p = q;
-      if (c == ' ')
-        p++;
-    }
-  }
-  /* Last trailing bit: split by linefeeds if possible */
-  n = strchr(p, '\n');
-  while (n) {
-    *n = 0;
-    dprintf(idx, "%s%s\n", prefix, p);
-    *n = '\n';
-    p = n + 1;
-    n = strchr(p, '\n');
+  for (size_t i = 0; i < lines.length(); ++i) {
+    dprintf(idx, "%s%s\n", prefix, bd::String(lines[i]).c_str());
   }
-  if (*p)
-    dprintf(idx, "%s%s\n", prefix, p);  /* Last trailing bit */
 }
 
 void

+ 2 - 1
src/dccutil.h

@@ -14,6 +14,7 @@ struct portmap {
 
 namespace bd {
   class Stream;
+  class String;
 }
 
 #define dprintf dprintf_eggdrop
@@ -37,7 +38,7 @@ namespace bd {
 
 
 void init_dcc(void);
-void dumplots(int, const char *, const char *);
+void dumplots(int, const char *, bd::String);
 void rdprintf(const char*, int, const char *, ...) __attribute__((format(printf, 3, 4)));
 void dprintf(int, const char *, ...) __attribute__((format(printf, 2, 3)));
 void dprintf_real(int, char*, size_t, size_t, const char* = NULL);