home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / TY_v1.3 / src / line.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  3.8 KB  |  118 lines

  1. /*************************************************************************
  2.  ***                        line.c                       (JJB TEMPLAR) ***
  3.  *** Date modifications begun: 7/8/89.                                 ***
  4.  *** Last modified: 9/8/89.                                            ***
  5.  *************************************************************************/
  6. /*** Routines to manipulate the "line buffer". The line buffer holds a ***
  7.  *** line of output as it is being built in preparation for output to  ***
  8.  *** the screen. We keep track of the PRINTABLE length of the line as  ***
  9.  *** it is being built (well, sort of).                                ***
  10.  *************************************************************************/
  11.  
  12. #include "less.h"
  13.  
  14. static char linebuf[1024];  /* Buffer which holds the current output line */
  15. static char *curr;          /* Pointer into linebuf */
  16. static int column;   /* Printable length, accounting for backspaces, etc. */
  17.  
  18. char    *line; /* Pointer to the current line. Usually points to linebuf. */
  19.  
  20. extern int sc_width, sc_height;
  21.  
  22. void    prewind() /*=====================================================*/
  23. {                 /* Rewind line buffer.                                 */
  24.     line = curr = linebuf;
  25.     column = 0;
  26. }
  27.  
  28. #define NEW_COLUMN(nc)  if ((nc) > sc_width+50) return(1); else column = (nc);
  29.  
  30. int     pappend(c) /*====================================================*/
  31. register int    c; /* Append char to buffer, return !success.            */
  32. {
  33.     if (!c) {       /* Add zero to string */
  34.         *curr = 0;
  35.         return(0);
  36.     }
  37.  
  38.   /* check if much buffer left */
  39.     if (curr - linebuf > sizeof(linebuf) - 12) return(1);
  40.  
  41.   /* Used to expand tabs. Let console worry about that. May cause probs
  42.    * if auto-wrap was on (which it isn't), and will frack the l_start
  43.    * stuff a little. (Actually put out ^I at the moment). */
  44.  
  45.   /* Let the ESC codesd pass through OK. */
  46.  
  47.   /* Deal with ctrl-characters at the put_line level, one of the benefits
  48.    * of which is that the high bit isn't required to flag carats */
  49.  
  50.   /* Ordinary character.  Just put it in the buffer */
  51.     NEW_COLUMN(column+1);
  52.     *curr++ = c;
  53.     return(0);
  54. }
  55.  
  56. LONG    forw_raw_line(curr_pos) /*=======================================*/
  57. LONG    curr_pos;        /* Similar to forw_line(), but for "raw lines". */
  58. {
  59. register char   *p;
  60. register int    c;
  61. LONG            new_pos;
  62.  
  63.     if ((curr_pos == NULL_POSITION) || ch_seek(curr_pos) || ((c = ch_forw_get()) == EOF))
  64.         return(NULL_POSITION);      /* Can't go forward */
  65.  
  66.     p = linebuf;
  67.  
  68.     for (;;) {
  69.         if ((c == '\n') || (c == EOF)) {
  70.             new_pos = ch_tell();
  71.             break;
  72.         }
  73.         if (p >= &linebuf[sizeof(linebuf)-1]) {
  74.       /* Overflowed the input buffer. Pretend the line ended here. */
  75.             new_pos = ch_tell() - 1;
  76.             break;
  77.         }
  78.         *p++ = c;
  79.         c = ch_forw_get();
  80.     }
  81.     *p = 0;
  82.     line = linebuf;
  83.     return(new_pos);
  84. }
  85.  
  86. LONG    back_raw_line(curr_pos) /*=======================================*/
  87. LONG    curr_pos;               /* As above, but backwards.              */
  88. {
  89. register char   *p;
  90. register int    c;
  91. LONG            new_pos;
  92.  
  93.     if ((curr_pos == NULL_POSITION) || (curr_pos <= 0) || ch_seek(curr_pos-1))
  94.         return(NULL_POSITION);
  95.  
  96.     p = &linebuf[sizeof(linebuf)];
  97.     *(--p) = 0;
  98.  
  99.     for (;;) {
  100.         c = ch_back_get();
  101.         if (c == '\n') {    /* Newline ending previous line */
  102.             new_pos = ch_tell() + 1;
  103.             break;
  104.         }
  105.         if (c == EOF) {     /* Hit beginning of file */
  106.             new_pos = 0;
  107.             break;
  108.         }
  109.         if (p <= linebuf) { /* Overflowed the input buffer */
  110.             new_pos = ch_tell() + 1;
  111.             break;
  112.         }
  113.         *(--p) = c;
  114.     }
  115.     line = p;
  116.     return(new_pos);
  117. }
  118.