Linewrapping Function, more [by Doppleganger Software]
Snippet Posted Wednesday, August 12th @ 11:30:17 PM, by George Greer in the Utils dept.
Added Jun 23, 1997. Click the link below to read it or download it.

From: Doppleganger Software <baator@geocities.com>
Subject: Linewrapping function

char *linewrap(char *str, int max)
{
  char xbuf2[MAX_STRING_LENGTH];
  char xbuf[MAX_STRING_LENGTH];
  char *tmp;
  int i, lline = 0, curr = 0;
  bool spec_code = FALSE;

  xbuf[0] = xbuf2[0] = '\0';
  i = max - 1;

  if (strlen(str) < i)
    return (str);

  for (tmp = str; *tmp; tmp++) {
    if (*tmp == '\x1B' && !spec_code)
      spec_code = TRUE;
    if (*tmp == ' ') {
      spec_code = FALSE;
      if (lline > i) {
        sprintf(xbuf, "%s\r\n%s ", xbuf, xbuf2);
        lline = 0;
        curr = 0;
        xbuf2[0] = '\0';
      } else {
        sprintf(xbuf, "%s%s ", xbuf, xbuf2);
        lline++;
        curr = 0;
        xbuf2[0] = '\0';
      }
    } else if (*tmp == '\r') {
      spec_code = FALSE;
      if (lline > (i + 1))
        sprintf(xbuf, "%s\r\n%s", xbuf, xbuf2);
      else
        sprintf(xbuf, "%s%s\r", xbuf, xbuf2);
      lline = 0;
      curr = 0;
      xbuf2[0] = '\0';
    } else {
      xbuf2[curr] = *tmp;
      xbuf2[curr + 1] = '\0';
      curr++;
      if (!spec_code)
        lline++;
    }
    if (*tmp == 'm' && spec_code)
      spec_code = FALSE;
  }
  if (lline > i)
    sprintf(xbuf, "%s\r\n%s", xbuf, xbuf2);
  else
    sprintf(xbuf, "%s%s", xbuf, xbuf2);

  return (str_dup(xbuf));
}

If I remember correctly, I put

char *conv;

conv = linewrap(messg, 80);

then send conv instead of messg.  It's pretty simple.

into the send_to_room, send_to_char and send_to_all functions (I think
that last one is right...you will know them)  It's easy to make it
configurable.  Just use an unused in the player saved, and instead of 80,
have it send that to the function.  You might want to put

#define GET_LINES(ch)  (IS_NPC(ch) ? 80 : whatever_var_used)

so that mobs get standard 80, or maybe have it try and find the char's
setting anyway...have to check on that.

I would like to mention that I have had a few weird instances of a word
being auto-wrapped, then a new line being placed after that word, and the
rest of the sentance below.  If I can get some help figuring out why that
is occuring, I would appreciate it.  :)


<< Linewrapping Function [by Daniel Koepke] | Reply | View as text | Threaded | Load Command Changes [by The Lion King] >>

 


Related Links
  download
Related Articles
More by greerga
 
 

CircleMUD Snippets
 
Note: Not all of these snippets will work perfectly with your version of code, so be prepared to fix one or two bugs that may arise, and please let me know what you needed to do to fix it. Sending a corrected version is always welcome.
Finally, if you wish to use any of the snippets from this page, you are more than welcome, just mention the authors in your credits. If you wish to release any of these snippets to the public on another site, contact me FIRST.