RE: [cc65] Input string routine for a line of data?

From: Shawn Jefferson <sjefferson1shaw.ca>
Date: 2011-06-07 05:57:40
I wrote this for some program I was doing, using cgetc and a loop, allowing
some rudimentary editing.  Not very portable I guess, but you could replace
the case statements with CH_ defines.  Hmm, actually now that I look at it,
there are a few things that I don't like about it as a general-purpose
routine, but I'm sure you get the idea.

 

/* text_input

 *

 * Get some text input from the user.  Pass in the max number of characters

 * to be accepted, and a pointer to a string that is at least as big plus
one

 * for the null terminator to hold the string.

 *

 * Returns the number of characters entered if everything went ok

 * and the user pressed return, 0 if the user pressed Escape.

 */

unsigned char __fastcall__ text_input(unsigned char x, unsigned char y, char
*text, unsigned char max, unsigned char num_only)

{

  char c;

  unsigned char i;

 

 

  //--- Initialize ---//

  i = 0;

  cursor(1);

  cputsxy(x, y, text);                  // show text

  gotoxy(x, y); 

 

  while (1) {

    c = cgetc();

 

    switch(c) {

      case 27:                          // user pressed escape

            text[0] = 27;                   // flag that user pressed escape


            cursor(0);                      // since a blank string and
escape 

            return(0);                      // both have a return value of
zero

            break;

 

      case 155:                         // user pressed return

            text[strlen(text)] = '\0';

            cursor(0);

            return(i);

            break;

 

      case 126:                         // user pressed backspace 

            if (i != 0) {

              --i;

              if (i == (strlen(text)-1))

                text[i] = '\0';

              else

                text[i] = ' ';

              //cclearxy(x+i, y, 1);

              cputcxy(x+i, y, ' ');

              gotoxy(x+i, y);

            }

            break;

 

      case 30:                          // user pressed left arrow

            if (i != 0) {

              --i;

              gotoxy(x+i, y);

            }

            break;

 

      case 31:                          // user pressed right arrow

            if (i < strlen(text)) {

              ++i;

              gotoxy(x+i, y); 

            }

 

      default:

            if (i == max) continue;                     // maxed out

 

            if (!num_only) {

              if (isprint(c)) {                           // is a printable
char

                text[i] = c;

                cputc(c);

                ++i;

                break;

              }

            }

 

            if (isdigit(c)) {                           // is a number

              text[i] = c;

              cputc(c);

              ++i;

              break;

            }

 

            break;

    }

  }

  // end of function

}

 

  _____  

From: owner-cc65@musoftware.de [mailto:owner-cc65@musoftware.de] On Behalf
Of Joseph Rose
Sent: Sunday, June 05, 2011 12:00 PM
To: cc65 mailing list
Subject: [cc65] Input string routine for a line of data?

 

Under cc65, what is the fastest way to get a string of input from the user?
I just need a quick-and-dirty approach for experimentation.  I am working
mainly for the C64 but may work fopr other targets as well.

-----------------------

Joseph Rose, a.k.a. Harry Potter

Working magic in the computer community




----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Tue Jun 7 06:00:51 2011

This archive was generated by hypermail 2.1.8 : 2011-06-07 06:00:54 CEST