home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_01.ZIP / WPRO2.C < prev   
Encoding:
C/C++ Source or Header  |  1987-12-29  |  3.4 KB  |  112 lines

  1. /* WPRO2.C - From page 362 of "Microsoft C Programming for      */
  2. /* the IBM" by Robert Lafore. This adds some more features to   */
  3. /* the WPRO1.C simple word processor on this disk. Hit Alt-     */
  4. /* u to toggle underline on and off. I changed the switch       */
  5. /* statement to enable the right arrow key when cursor is       */
  6. /* at the last column.                                          */
  7. /****************************************************************/
  8.  
  9. #include "dos.h"           /* for REGS definition */
  10. #define TRUE 1
  11. #define COMAX 80           /* max number of columns */
  12. #define R_ARRO 77
  13. #define L_ARRO 75
  14. #define BK_SPC 8
  15. #define ALT_U 22           /* [Alt] and [u] keys */
  16. #define VIDEO 0x10         /* video ROM BIOS service */
  17. #define NORM 0x07          /* normal attribute */
  18. #define UNDR 0x01          /* underline attribute */
  19. int col = 0;               /* cursor position */
  20. int length = 0;            /* length of phrase */
  21. int far *farptr;           /* pointer to screen memory */
  22. union REGS regs;           /* for ROM BIOS calls */
  23.  
  24. main()
  25. {
  26. char ch, attr = NORM;
  27.  
  28.    farptr = (int far *) 0xB0000000;    /*start of screen mem*/
  29.    clear();
  30.    cursor();
  31.    while(TRUE) {
  32.       if ((ch = getch()) == 0) {       /*if extended code key*/
  33.          ch = getch();                 /*read extended code*/
  34.          switch(ch) {
  35.             case R_ARRO: {
  36.                if (col < length) {
  37.                   ++col;
  38.                   break;
  39.                }
  40.                else if (col >= length) {
  41.                   ++col;
  42.                   ++length;
  43.                   break;
  44.                }
  45.             }
  46.             case L_ARRO:
  47.                if (col > 0)
  48.                   --col;
  49.                break;
  50.             case ALT_U:
  51.                attr = (attr == NORM) ? UNDR : NORM;
  52.          }
  53.       }
  54.       else
  55.          switch(ch) {
  56.             case BK_SPC:
  57.                if (length > 0)
  58.                   delete();
  59.                break;
  60.             default:
  61.                if (length < COMAX)
  62.                   insert(ch, attr);
  63.          }
  64.       cursor();
  65.    }                    /* end while */
  66. }                       /* end main */
  67.  
  68. /* cursor() */    /* move cursor to row = 0, col */
  69. cursor()
  70. {
  71. regs.h.ah = 2;       /* 'set cursor pos' service */
  72. regs.h.dl = col;     /* column varies */
  73. regs.h.dh = 0;       /* always top row */
  74. regs.h.bh = 0;       /* page zero */
  75.  
  76.    int86(VIDEO, ®s, ®s);     /* call video interrupt */
  77. }
  78.  
  79. /* insert() */    /*inserts cursor at cursor position */
  80. insert(ch, attr)
  81. char ch, attr;
  82. {
  83. int j;
  84.  
  85.    for (j = length; j > col; j--)         /* shift chars right */
  86.       *(farptr + j) = *(farptr + j -1);   /*     to make room */
  87.    *(farptr + col) = ch | attr << 8;      /* insert char */
  88.    ++length;                              /* increment count */
  89.    ++col;                                 /* move cursor right */
  90. }
  91.  
  92. /* delete() */    /* deletes char at pos 1 left of cursor */
  93. delete()
  94. {
  95. int j;
  96.  
  97.    for(j = col; j <= length; j++)         /* shift chars left */
  98.       *(farptr + j - 1) = *(farptr + j);
  99.    --length;                              /* decrement count */
  100.    --col;                                 /* move cursor left */
  101. }
  102.  
  103. /* clear() */  /*clears screen by inserting 0 at every location */
  104. clear()
  105. {
  106. int j;
  107.  
  108.    for(j = 0; j < 2000; j++)
  109.       *(farptr + j) = 0x0700;    /* zeros with attr = 07 */
  110. }
  111.  
  112.