'Interactive' Color Parsing....

From: Sean P. Mountcastle (mountcas@scratchy.ee.cua.edu)
Date: 05/26/96


Hey folks, I just got back into my account since school ended, and 
finally finished going thru the piles of email.  I picked up that color 
code parse (the one with the \c## -- sorry I accidentally deleted the 
email, so I don't know your name).... Anyway, I've been trying to make it 
resize the passed string so that it can handle all the extra color 
codes.  This is what I did, but I've tried MANY different approaches and 
they all lock up the computer in DOS and crash in Ultrix.

void do_color(char *inbuf, int color)
{
    register int j = 0, p = 0;
    int      c, k, max;
    char     out_buf[32000], colorbuf[32];
    char     *newstring;

    if (inbuf[0] == '\0') return;

    while (inbuf[j] != '\0') {
       if ((inbuf[j] == '%') && (inbuf[j+1] != '%')) {
	  switch (inbuf[j+1]) {
	    /* Capital letters are BRIGHT */
	    case 'R':
	      max = strlen(KBRD);
	      strcpy(colorbuf, KBRD);
	      break;
	    case 'G':
	      max = strlen(KBGR);
	      strcpy(colorbuf, KBGR);
	      break;
	    case 'y':
	    case 'O':
	      max = strlen(KYEL);
	      strcpy(colorbuf, KYEL);
	      break;
	    case 'B':
	      max = strlen(KBBL);
	      strcpy(colorbuf, KBBL);
	      break;
	    case 'M':
	      max = strlen(KBMG);
	      strcpy(colorbuf, KBMG);
	      break;
	    case 'C':
	      max = strlen(KBCN);
	      strcpy(colorbuf, KBCN);
	      break;
            /* Here are the normal colors */
	    case 'r':
	    case '1':
	      max = strlen(KRED);
	      strcpy(colorbuf, KRED);
	      break;
	    case 'g':
	    case '2':
	      max = strlen(KGRN);
	      strcpy(colorbuf, KGRN);
	      break;
	    case 'o':
	    case '3':
	      max = strlen(KGLD);
	      strcpy(colorbuf, KGLD);
	      break;
	    case 'b':
	    case '4':
	      max = strlen(KBLU);
	      strcpy(colorbuf, KBLU);
	      break;
	    case 'm':
	    case '5':
	      max = strlen(KMAG);
	      strcpy(colorbuf, KMAG);
	      break;
	    case 'c':
	    case '6':
	      max = strlen(KCYN);
	      strcpy(colorbuf, KCYN);
	      break;
            /* These are the modifiers */
	    case '7':
	    case 'l':
	    case 'L':
	      max = strlen(MOD_BOLD);
	      strcpy(colorbuf, MOD_BOLD);
	      break;
	    case '8':
	    case 'F':
	    case 'f':
	      max = strlen(MOD_BLINK);
	      strcpy(colorbuf, MOD_BLINK);
	      break;
	    case '9':
	    case 'V':
	    case 'v':
	      max = strlen(MOD_REVERSE);
	      strcpy(colorbuf, MOD_REVERSE);
	      break;
	    case 'N':  /* Same as MOD_NORMAL */
	    case 'n':
	    case '0':
	    default:
	      max = strlen(KNRM);
	      strcpy(colorbuf, KNRM);
	      break;
	  }
	  /* This gets rid of the % and the color letter */
	  j += 2;
          /* make sure the player wants to see the color! */
	  if (color) {
	     for (k = 0; k < max; k++, p++)
		 out_buf[p] = colorbuf[k];
	  }
       } else
	  out_buf[p++] = inbuf[j++];
    }
    /* out_buf now contains the correct string of text */
    out_buf[p] = '\0';
    /* allocate contiguous memory for the string */
    newstring = (char *) calloc(strlen(out_buf)+1, sizeof(char));
    /* now copy output back into passed string */
    strcpy(newstring, out_buf);
    /* free the old memory used by the original string */
    free(inbuf);
    /* make inbuf point to the parsed string */
    inbuf = newstring;
}

/* This is just for testing in MS-DOS */
void main(void)
{
    char string[80] = "%F%rHello %N%BHow%N %ca%lre %N%yyou%m?%n";
    do_color(string, 1);
    printf("%s", string);
}


On the line with the calloc, I've tried malloc, realloc -- both on inbuf 
and creating a new char *, like newstring....  Any ideas on what's 
messing up the works?  Without this stuff, it works with just the 
strcpy(), the only problem is if inbuf isn't large enough to handle the 
new string :(

	Thanks in advance,

	- Sean


Sean Mountcastle
----------------
Be a better psychiatrist and the world will beat a psychopath to your
door.



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