mpc_format2.c
snippets · 4.4 KB · 2010-03-30
mpc_format2.c by Amras of Hexahedron: second version.
Download « Back to the snippets list
/* Code was created by Amras of Hexahedron
* hexahedron.genesismuds.com port 1414
* Purpose of this code is to format the show command
* on mprogs, to be more like a design enviroment.
* Copy code to bottem of olc_mprog.c and replace
* line in show command of mpedit_show with a call
* to the function:
format_code_output(ch, pMcode->code);
* This code is free to use as long as I get some kind of credit,
* and this header is included in the code. :)
*/
void print_tabs(CHAR_DATA * ch, int tabs);
char *str_replace(char *seed, char *apple, char *tree);
extern const char * fn_keyword[];
extern const char * fn_evals[];
void format_code_output(CHAR_DATA * ch, const char * code)
{
int tabs = 0, x = 0, placeholder = 0;
char buf[MSL];
char buf2[MIL];
char buf3[20];
while(code[placeholder] != '\0')
{
int i;
//Reset the buffer to all NULL bits
for(i = 0; i < MSL; i++)
buf[i] = '\0';
//Copy the string until we hit a new line
for(i = 0; code[placeholder] != '\n'
&& code[placeholder] != '\r'
&& code[placeholder] != '\0'; i++)
{
buf[i] = code[placeholder];
placeholder++;
}
//Re-Add the end-line to the string we copied
buf[i] = '\n';
buf[i+1] = '\r';
//Clear any whitespace
while(code[placeholder] == '\n'
|| code[placeholder] == '\r'
|| code[placeholder] == ' '
|| code[placeholder] == '\t')
placeholder++;
//Start the formatting and coloring
if(!str_prefix("if", buf))
{
str_replace("if", "{Cif{x", buf);
print_tabs(ch, tabs);
for(x = 0; fn_keyword[x][0] != '\n'; x++)
{
if(str_infix(fn_keyword[x], buf))
continue;
{
char buf3[20];
sprintf(buf3, " %s ", fn_keyword[x]);
sprintf(buf2, " {Y%s{x ", fn_keyword[x]);
str_replace(buf3, buf2, buf);
}
}
for(x = 0; fn_evals[x][0] != '\n'; x++)
{
if(str_infix(fn_evals[x], buf))
continue;
sprintf(buf3, "%s", fn_evals[x]);
sprintf(buf2, "{R%s{x", fn_evals[x]);
str_replace(buf3, buf2, buf);
}
send_to_char(buf, ch);
tabs++;
}
else if(!str_prefix("end if", buf))
{
tabs--;
str_replace("end if", "{Cend if{x", buf);
print_tabs(ch, tabs);
send_to_char(buf, ch);
}
else if(!str_prefix("endif", buf))
{
tabs--;
str_replace("endif", "{Cendif{x", buf);
print_tabs(ch, tabs);
send_to_char(buf, ch);
}
else if(!str_prefix("else", buf))
{
tabs--;
str_replace("else", "{Celse{x", buf);
print_tabs(ch, tabs);
send_to_char(buf, ch);
tabs++;
}
else
{
print_tabs(ch, tabs);
for(x = 0; fn_keyword[x][0] != '\n'; x++)
{
if(str_infix(fn_keyword[x], buf))
continue;
sprintf(buf3, " %s ", fn_keyword[x]);
sprintf(buf2, " {Y%s{x ", fn_keyword[x]);
str_replace(buf3, buf2, buf);
}
for(x = 0; fn_evals[x][0] != '\n'; x++)
{
if(str_infix(fn_evals[x], buf))
continue;
sprintf(buf3, "%s", fn_evals[x]);
sprintf(buf2, "{R%s{x", fn_evals[x]);
str_replace(buf3, buf2, buf);
}
send_to_char(buf, ch);
}
//Lets double check our math
if(tabs < 0)
tabs = 0;
}
}
void print_tabs(CHAR_DATA * ch, int tabs)
{
while(tabs > 0)
{
tabs--;
send_to_char(" ", ch);
}
}
/**********************************************************************************
* If you need the str_replace() function, use this one, I did not code it and
* I don't remember who did.
**********************************************************************************/
char *str_replace(char *seed, char *apple, char *tree)
{
static char buffer[MSL * 2]; // working buffer
char *nail; // pointer to location in tree
char *bpt; // pointer to location in buffer
buffer[0] = '\0'; // initialize static buffer
bpt = &buffer[0]; // point bpt to first char of buffer
nail = &tree[0]; // point nail to first char of tree
while( *nail != '\0' ) // while nail doesn't point to end
{
if(strncasecmp(nail,seed,strlen(seed))) // if seed isn't found
{
strncat(bpt,nail,1); // append first char of nail to bpt
bpt++; nail++; // increment them to the next char
}
else // if seed was found
{
strcat(buffer,apple); // tag apple onto buffer
bpt += strlen(apple); // increment bpt by length of apple
nail += strlen(seed); // increment nail by length of seed
}
}
*bpt++ = '\0'; // add null character to end of buffer
strcpy(tree,buffer); // copy buffer into original string
return buffer; // and return buffer
}