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

  1. /*************************************************************************
  2.  ***                        input.c                      (JJB TEMPLAR) ***
  3.  *** Date modifications begun: 7/8/89.                                 ***
  4.  *** Last modified: 27/8/89.                                           ***
  5.  *************************************************************************/
  6. /*** High level routines dealing with getting lines of input from the  ***
  7.  *** file being viewed. When we speak of "lines" here, we mean         ***
  8.  *** PRINTABLE lines; lines processed with respect to the screen width.***
  9.  *** We use the term "raw line" to refer to lines simply delimited by  ***
  10.  *** newlines; not processed with respect to screen width.             ***
  11.  *************************************************************************/
  12.  
  13. #include "less.h"
  14.  
  15. extern int  do_bs;
  16. extern int  squeeze;
  17. extern char *line;
  18.  
  19. int     page_break;
  20.  
  21. LONG    forw_line(curr_pos) /*===========================================*/
  22. LONG    curr_pos;           /* Get next line, given old position.        */
  23. {
  24. LONG        new_pos;
  25. register int c;
  26.  
  27.     if ((curr_pos == NULL_POSITION) || ch_seek(curr_pos)) return(NULL_POSITION);
  28.     prewind();
  29.     if (page_break) {
  30.         new_pos = curr_pos;
  31.         goto PAGE_SKIP;
  32.     }
  33.     if ((c = ch_forw_get()) == EOF) return(NULL_POSITION);
  34.     if (c == ' ') {                /* Must be first char on line */
  35.         page_break = 1;
  36.     }
  37.  
  38.     for (;;) {
  39.       /* Check for EOF too, since file may not end with '\n' */
  40.         if ((c == '\n') || (c == EOF)) {
  41.             new_pos = ch_tell();    /* End of line */
  42.             break;
  43.         }
  44.  
  45.       /* Append the char to the line and get the next char. */
  46.         if (pappend(c)) {
  47.           /* The char won't fit in the line; the line
  48.              is too long to print in the screen width.
  49.              End the line here.          */
  50.             new_pos = ch_tell() - 1;
  51.             break;
  52.         }
  53.         c = ch_forw_get();
  54.     }
  55. PAGE_SKIP:
  56.     pappend('\0');
  57.  
  58.     if (squeeze && !(*line) && !page_break) {
  59.         /* This line is blank.
  60.            Skip down to the last contiguous blank line
  61.            and pretend it is the one which we are returning. */
  62.         while ((c = ch_forw_get()) == '\n') ;
  63.         if (c != EOF) ch_back_get();
  64.         new_pos = ch_tell();
  65.     }
  66.     return(new_pos);
  67. }
  68.  
  69. LONG    back_line(curr_pos) /*===========================================*/
  70. LONG    curr_pos;           /* Get previous line.                        */
  71. {
  72. LONG    new_pos, begin_new_pos;
  73. register int    c;
  74.  
  75.     if ((curr_pos == NULL_POSITION) || (curr_pos <= 0) || ch_seek(curr_pos-1))
  76.         return (NULL_POSITION);
  77.  
  78.     if (page_break) {
  79.         begin_new_pos = curr_pos;
  80.         prewind();
  81.         goto PAGE_BREAK;
  82.     }
  83.  
  84.     if (squeeze) {              /* page_break disables squeeze */
  85.       /* Find out if the "current" line was blank. */
  86.         ch_forw_get();          /* Skip the newline */
  87.         c = ch_forw_get();      /* First char of "current" line */
  88.         ch_back_get();          /* Restore our position */
  89.         ch_back_get();
  90.  
  91.         if (c == '\n') {
  92.           /* The "current" line was blank.
  93.              Skip over any preceeding blank lines,
  94.              since we skipped them in forw_line(). */
  95.             while ((c = ch_back_get()) == '\n') ;
  96.             if (c == EOF) return (NULL_POSITION);
  97.             ch_forw_get();
  98.         }
  99.     }
  100.  
  101.   /* Scan backwards until we hit the beginning of the line. */
  102.     for (;;) {
  103.         c = ch_back_get();
  104.         if (c == '\n') {
  105.           /* This is the newline ending the previous line.
  106.              We have hit the beginning of the line. */
  107.             new_pos = ch_tell() + 1;
  108.             break;
  109.         }
  110.         if (c == EOF) {
  111.           /* We have hit the beginning of the file.
  112.              This must be the first line in the file.
  113.              This must, of course, be the beginning of the line. */
  114.             new_pos = 0;
  115.             break;
  116.         }
  117.     }
  118.  
  119.   /* Now scan forwards from the beginning of this line.
  120.      We keep discarding "printable lines" (based on screen width)
  121.      until we reach the curr_pos. */
  122.  
  123.     if (ch_seek(new_pos)) return(NULL_POSITION);
  124. loop:
  125.     begin_new_pos = new_pos;
  126.     prewind();
  127.  
  128.     do {
  129.         c = ch_forw_get();
  130.         new_pos++;
  131.         if (c == '\n') break;
  132.         if (pappend(c)) {
  133.           /* Got a full printable line, but we haven't reached our curr_pos yet.
  134.              Discard the line and start a new one. */
  135.             pappend('\0');
  136.             ch_back_get();
  137.             new_pos--;
  138.             goto loop;      /* Hey hey hey! */
  139.         }
  140.     } while (new_pos < curr_pos);
  141. PAGE_BREAK:
  142.     pappend('\0');
  143.  
  144.     if (*line == ' ') page_break = 1;
  145.  
  146.     return(begin_new_pos);
  147. }
  148.