[Code] Linewrapping

From: Daniel Koepke (dkoepke@california.com)
Date: 03/02/97


Here's a little function to linewrap lines at 79. It's not perfect,
but it works fairly well. There's no question about the fact that
it's ugly.

  char *linewrap(char *str) {
    char xbuf2[MAX_STRING_LENGTH];
    char xbuf[MAX_STRING_LENGTH];
    bool cap=TRUE;
    char *tmp;
    int i;

    xbuf[0]=xbuf2[0]='\0';
    i = 0;

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

    for (tmp = str; *tmp; tmp++) {
      if (*tmp=='\r') {
        if (xbuf[i-1] != ' ') {
          xbuf[[i]=' ';
          i++;
        }
      } else if (*tmp=='\n')
        ;
      else if (*tmp == ' ') {
        if (xbuf[i-1] != ' ') {
          xbuf[i] = ' ';
          i++;
        }
      } else {
        xbuf[i] = *tmp;
        xbuf[i+1] = ' ';
        i += 2;
      }
      cap = TRUE;
    } else {
      xbuf[i]=*tmp;
      if (cap) {
        cap = FALSE;
        xbuf[i] = UPPER(xbuf[i]);
      }
      i++;
    }
  }

  xbuf[i]='\0';
  strcpy(xbuf2, xbuf);
  tmp=xbuf2;
  xbuf[0]='\0';

  for (;;) {
    for (i=0; i<79; i++)
      if (!*(tmp+i))
        break;
    if (i<79)
      break;

    // locate the last space
    for (i=(xbuf[0]?78:75); i; i--)
      if (*(tmp+i)==' ')
        break;

    if (i) {
      *(tmp+i)='\0';
      strcat(xbuf, tmp);
      strcat(xbuf, "\r\n");
      tmp += i+1;
      while (*tmp == ' ')
        tmp++;
    } else {
      *(tmp+77)='\0';
      strcat(xbuf,tmp);
      strcat(xbuf,"-\r\n");
      tmp += 78;
    }
  }

  while (*(tmp+i) && (*(tmp+i)==' ' || IS_NEWL(*(tmp+i))))
    i--;
  *(tmp+i+1)='\0';
  strcat(xbuf,tmp);
  if (xbuf[strlen(xbuf)-2] != '\r')
    strcat(xbuf, "\r\n");
  return (str_dup(xbuf)); }

Note that the above wasn't written for beauty or for speed, it's
a cheap and (very) dirty hack. Furthermore, note that I retyped
this from memory, so it may not even work. Oh, and here, too, is
a function to properly capitalize a string despite ansi codes:

  char *ProperString(char *str) {
    char xbuf[MAX_STRING_LENGTH];
    int i = 0;

    *xbuf = '\0';
    strcpy(xbuf, str);

    if (xbuf[i] == '\x1b') {
      while ((xbuf[i] >= '0' && xbuf[i] <= 9) || xbuf[i] == ';')
        i++;
      i++; 
    }

    xbuf[i] = UPPER(xbuf[i]);
    return (str_dup(xbuf));
  }

Neither are the most efficient things in the world, but they work (or
did before I retyped them :P).


--
Daniel Koepke
dkoepke@california.com
Forgive me father, for I am sin.


+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
|    Or send 'info circle' to majordomo@cspo.queensu.ca     |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST