Hello Kurt, thank you for your efforts. I can comprehend much of
the code and follow what you are doing, but the show_menu function
has me beat at this stage. I _NEED_ comments in it. I can recognise
that you are using the ternary operator, and appreciate some of the
subtle elegance, but at my level, it may as well be the winning
entry in the obfuscated code competition.
I have put the show_menu function into it's own file for study, and
adding comments as I study and see how it works. My level of
experience is not yet up to loading it in the TC IDE and watching
it run to probe it. I do use the context sensitive on line help,
but some of your elegant code has me baffled.
int show_menu(char **txt, int x, int y)
{
/* parameter declaration **txt is a pointer to an array of
character strings, the last of which is a null string */
int selection = 0, key = 0, max, wide = 0;
char *screenbuffer, format[16], buffer[80];
for(max = 0; *txt[max]; max++)
/* this steps through the strings that **txt points
to tallying them in max */
wide = ((key = strlen(txt[max])) > wide) ? key : wide;
/* this stores the length of the longest menu item string */
screenbuffer = malloc((max + 1) * (wide + 3) * 2);
if(!screenbuffer)
return -1; /* check that malloc succeeds */
gettext(x, y, x + wide + 2, y + max, screenbuffer);
/* the piece of screen which is about to be used is saved */
sprintf(format, " %s-%d.%ds ", "%", wide, wide);
/* this fills the format string array with formatting for the next
sprintf call. format[] should contain [ %-(wide).(wide)s ] which
is a left justified string displaying at least (wide) characters
and displaying no more than (wide) characters */
for(key = 0; key < max; key++) /* stepping through the strings */
{
sprintf(buffer, format, txt[key]); /* put them into the buffer */
norm_text(buffer, x, key + y); /* and output the buffer to screen */
}
while(key != '\r' && key != 27) /* while not enter or escape */
{ /* more commenting on the way */
sprintf(buffer, format, txt[selection]);
norm_text(buffer, x, selection + y);
selection = (selection + max - (key == -72) + (key == -80)) % max;
sprintf(buffer, format, txt[selection]);
high_text(buffer, x, selection + y);
key = ((key = getch()) > 0) ? key : -getch();
}
selection = (selection + 1) * (key != 27);
puttext(x, y, x + wide + 2, y + max, screenbuffer);
free(screenbuffer);
return (key == '\r') ? selection : 0;
}
Regards, Mark Trickett
--- Blue Wave/DOS v2.30
---------------
* Origin: Micom's Maximus CBCS (03) 9752-3949 (3:633/371)
|