home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume17 / tcl-editor / part02 / lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-18  |  3.3 KB  |  141 lines

  1. /* $Header: /nfs/unmvax/faculty/crowley/x/pt/RCS/lines.c,v 1.1 1991/07/05 16:34:41 crowley Exp crowley $ */
  2.  
  3. #include <ctype.h>
  4. #include "pt.h"
  5.  
  6. Offset
  7. readLine( fid, cp, buffer, makeLowerCase )
  8.     int fid;
  9.     Offset cp;
  10.     char *buffer;
  11.     int makeLowerCase;
  12. {
  13.     extern int getSpanSize;
  14.  
  15.     char *limit = buffer + MSGBUFFERSIZE - 1;
  16.     char ch;
  17.     unsigned char *firstByte = (unsigned char *)1;
  18.     unsigned char *lastByte = (unsigned char *)0;
  19.  
  20.     while( buffer < limit ) {
  21.                 if( firstByte > lastByte ) {
  22.                         if( getSpan( fid, cp, &firstByte, &lastByte, 0 ) )
  23.                 break;
  24.                 }
  25.         ++cp;
  26.         ch = (char)(*firstByte++);
  27. ++getSpanSize;
  28.         if( makeLowerCase && isupper(ch) )
  29.             ch = tolower(ch);
  30.         *buffer++ = ch;
  31.         if( ch == '\n' )
  32.             break;
  33.     }
  34.     *buffer = '\0';
  35.     return cp;
  36. }
  37.  
  38. Offset
  39. nextLine( fid, cp, n)
  40.     int fid;
  41.     Offset cp;
  42.     int *n;
  43. {
  44.     extern int getSpanSize;
  45.  
  46.     int nLines = 0;
  47.     unsigned char *firstByte = (unsigned char *)1;
  48.     unsigned char *lastByte = (unsigned char *)0;
  49.  
  50.     while( nLines < *n ) {
  51.                 if( firstByte > lastByte ) {
  52.                         if( getSpan( fid, cp, &firstByte, &lastByte, 0 ) )
  53.                 break;
  54.                 }
  55.         if( (char)(*firstByte++) == '\n' )
  56.             ++nLines;
  57. ++getSpanSize;
  58.         ++cp;
  59.     }
  60.     *n = nLines;
  61.     return cp;
  62. }
  63.  
  64. /*
  65.  * prevLine backs up 'n' lines or partial lines.  That is, if it starts at the
  66.  * beginning of a line it will not count that line but if it starts in the
  67.  * middle of a line it will count that part of the line.
  68.  *
  69.  * A special case is when n == -1, then prevLine backs up to the beginning
  70.  * of the current line.  If it is already at the beginning of the line it
  71.  * does not change cp, else it moves cp to the first character of the line.
  72.  */
  73. Offset
  74. prevLine( fid, cp, n )
  75.     int fid;
  76.     Offset cp;
  77.     int *n;
  78. {
  79.     int uch;
  80.     int nLines = 0;
  81.     unsigned char *firstByte = (unsigned char *)1;
  82.     unsigned char *lastByte = (unsigned char *)0;
  83.  
  84.     /* are we already off one of the beginning? */
  85.     if( cp <= 0 ) {
  86.         *n = 0;
  87.         return (Offset)0;
  88.     }
  89.  
  90.     /* read the characters before the one we are on */
  91.     uch = getFileByte( fid, --cp );
  92.  
  93.     /* you can't move past the beginning of the text so just return */
  94.     if( uch == BLOCK_EOF ) {
  95.         *n = 0;
  96.         /* Move back to the first character of the file. */
  97.         return (Offset)0;
  98.     }
  99.  
  100.     /* Now see if we are starting at the beginning of a line */
  101.     if( (char)uch == '\n' ) {
  102.         if( *n == -1 ) {
  103.             *n = 0;
  104.             return cp + 1;
  105.         }
  106.     }
  107.  
  108.     /* since we handled the beginning of line case above.  The n==-1 */
  109.     /* special case reduces to the case of n==1 */
  110.     if( *n == -1 )
  111.         *n = 1;
  112.  
  113.     /* Now loop through the lines */
  114.     while( nLines < *n ) {
  115.         --cp;
  116.                 if( firstByte > lastByte ) {
  117.                         if( getSpan( fid, cp, &firstByte, &lastByte, 1 ) ) {
  118.                             /* end of file, count as a line */
  119.                 ++nLines;
  120.                 break;
  121.             }
  122.                 }
  123.         if( ((char)(*lastByte--)) == '\n' )
  124.             ++nLines;
  125. #ifdef XXXXXX
  126.                 uch = getFileByte( fid, --cp );
  127.                 if( uch == BLOCK_EOF ) {
  128.                         ++nLines;
  129.                         break;
  130.                 }
  131.                 if( ((char)uch) == '\n' )
  132.                         ++nLines;
  133. #endif
  134.     }
  135.     /* send back the number of lines we really did back up */
  136.     *n = nLines;
  137.  
  138.     /* we moved one past the character we are looking for */
  139.     return cp + 1;
  140. }
  141.