home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / vrac / pclcjs.zip / LINE1.C < prev    next >
C/C++ Source or Header  |  1993-06-12  |  4KB  |  116 lines

  1. /* A simple function for line input */
  2. /* Philip J. Erdelsky - 01 JAN 1991 */
  3.  
  4. static int key(void) {
  5.    int k = bdos(8)&0xFF;
  6.    /* two-byte sequence for special key is collapsed into single code */
  7.    if (k==0) k = bdos(8)&0xFF|0x100;
  8.    return k;
  9. }
  10.  
  11. /*static void display(int c) {bdos(2,c);}*/
  12. static void display(int c)
  13.  
  14. {
  15.     writechs(c,79,1);
  16. }
  17.  
  18. int line_input(char *text, int text_length, int display_length)  {
  19.    /* start display as though operator had struck Home or End key */
  20.    int k = 0x147; /* Home 0x147 or End 0x14F */
  21.    int finished = 0;
  22.    int desired_cursor_position;
  23.    int position_within_text;
  24.    int actual_cursor_position = 0;
  25.    while (1) {
  26.     switch (k)  {
  27.       /* finish line input and exit if k==0x00D (Enter key) */
  28.       /* other terminating keys could be added, if desired */
  29.       case 0x00D:
  30.         finished = 1;
  31.         /* fall through to next case */
  32.       /* move cursor to left end of text if k==0x147 (Home key) */
  33.       case 0x147:
  34.         desired_cursor_position = position_within_text = 0;
  35.         break;
  36.       /* move cursor to right end of text if k==0x14F (End key) */
  37.       case 0x14F:
  38.         position_within_text = strlen (text) ;
  39.         desired_cursor_position =
  40.           position_within_text<display_length ? position_within_text :
  41.           display_length-1;
  42.          break;
  43.        /* move cursor left one place if k==0x14B (left arrow key) */
  44.        case 0x14B:
  45.          if (position_within_text>0) {
  46.            position_within_text--;
  47.            if (desired_cursor_position>0) desired_cursor_position--;
  48.          }
  49.          break;
  50.        /* move cursor right one place if k==0x14D (right arrow key) */
  51.        case 0x14D:
  52.          if (position_within_text<strlen(text))  {
  53.            position_within_text++;
  54.            if (desired_cursor_position<display_length-1)
  55.             desired_cursor_position++;
  56.          }
  57.          break;
  58.        /* move cursor left one place and delete character under */
  59.        /* cursor if k==0x008 (backspace key) */
  60.        case 0x008:
  61.          if (position_within_text==0) break;
  62.          position_within_text--;
  63.          if (desired_cursor_position>0) desired_cursor_position--;
  64.          /* fall through to next case */
  65.        /* delete character under cursor if k==0x153 (Del key) */
  66.        case 0x153:
  67.          if (text[position_within_text] !=0)
  68.            strcpy(text+position_within_text, text+position_within_text+1);
  69.          break;
  70.        /* insert new character to left of character under curior */
  71.        default:
  72.          if (' '<=k && k<= '~' && strlen(text)<text_length) {
  73.            char *t = text+position_within_text;
  74.            do {
  75.             int temp = *t;
  76.             *t++ = k;
  77.             k = temp;
  78.            } while (k!=0);
  79.            *t = 0;
  80.            position_within_text++ ;
  81.            if (desired_cursor_position<display_length-1)
  82.              desired_cursor_position++;
  83.          }
  84.    }
  85.   /* redisplay entire line */
  86.   {
  87.     int hidden_characters_at_left =
  88.      position_within_text -desired_cursor_position ;
  89.     char *t = text+hidden_characters_at_left;
  90.     /* move cursor to left end of line */
  91.     while (actual_cursor_position !=0)
  92.      {display ( '\b' ) ; actual_cursor_position-- ;}
  93.     /* display arrows at left end if necessary */
  94.     display('\b');            /* 0xAE = left arrows */
  95.     display (hidden_characters_at_left!=0 ? 0xAE : ' ') ;
  96.     /* display text and trailing spaces, if any */
  97.     while ( actual_cursor_position<display_length ) {
  98.      int c;
  99.      display(*t==0 ? ' ' : (c = *t++)==' ' ? 0xFA : c);
  100.      actual_cursor_position++ ;      /* 0xFA = tiny dot */
  101.     }
  102.     /* display arrows at right end if necessary */
  103.     display(*t!=0 ? 0xAF : ' '); /* 0xAF = right arrows */
  104.     actual_cursor_position++;
  105.     /* move cursor back to desired position */
  106.     while ( actual_cursor_position>desired_cursor_position)
  107.      {display('\b'); actual_cursor_position--;}
  108.    }
  109.    if (finished) return k;
  110.    k = key();
  111.   }
  112. }
  113.  
  114.  
  115.  
  116.