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

  1. /* $Header: /nfs/unmvax/faculty/crowley/x/pt/RCS/piece.c,v 1.4 1992/03/04 17:07:18 crowley Exp crowley $ */
  2.  
  3. #include "pt.h"
  4.  
  5. /* piece table */
  6. Piece freePList;
  7. unsigned int bytesLeft;
  8. unsigned int piecesLeft;
  9.  
  10. Piece 
  11. dupPieces(pp)
  12.     Piece pp;
  13. {
  14.     Piece pp2;
  15.     Piece lastpp, retpp;
  16.  
  17.     lastpp = retpp = NULL;
  18.     while( pp != NULL ) {
  19.         pp2 = getFreePiece();
  20.         /* link to the previous piece */
  21.         if( lastpp != NULL )
  22.             lastpp->nextPiece = pp2;
  23.         else    /* first time through, remember the first piece */
  24.             retpp = pp2;
  25.         pp2->prevPiece = lastpp;
  26.         /* copy the field values */
  27.         pp2->file = pp->file;
  28.         pp2->position = pp->position;
  29.         pp2->length = pp->length;
  30.         lastpp = pp2;
  31.         pp = pp->nextPiece;
  32.     }
  33.     /* pp2->nextPiece == NULL already since getFreePiece does that */
  34.     return retpp;
  35. }
  36.  
  37. Piece 
  38. getFreePiece()
  39. {
  40.     extern char msgBuffer[];
  41.     extern Piece freePList;
  42.     extern unsigned int bytesLeft;
  43.     extern unsigned int piecesLeft;
  44.     extern int piecesRequested;
  45.     extern int piecesAllocated;
  46.     extern int debug;
  47.     
  48.     Piece pp;
  49.     
  50. ++piecesRequested;
  51.     pp = freePList;
  52.     if( pp != NULL ) {
  53.         --piecesLeft;
  54.         freePList = freePList->nextPiece;
  55.     } else { /* we have to allocate a piece structure from the free space */
  56. ++piecesAllocated;
  57.         pp = (Piece)PtMalloc(sizeof(struct piece), "piece" );
  58.     }
  59.     pp->nextPiece = pp->prevPiece = NULL;
  60.     pp->flags = 0;
  61.     pp->blockID = NullObject;
  62.     pp->nextClone = pp;
  63.     return pp;
  64. }
  65.  
  66. void
  67. freePieces(pp)
  68.     Piece pp;
  69. {
  70.     extern Piece freePList;
  71.     extern unsigned int piecesLeft;
  72.     extern int piecesFreed;
  73.     
  74.     Piece pp2;
  75.     
  76.     if( pp == NULL )
  77.         return;
  78. ++piecesFreed;
  79.     pp2 = pp;
  80.     while( pp2->nextPiece != NULL ) {
  81.         pp2 = pp2->nextPiece;
  82.         ++piecesLeft;
  83.     }
  84.     pp2->nextPiece = freePList;
  85.     freePList = pp;
  86.     ++piecesLeft;
  87. }
  88.  
  89. Piece 
  90. findPiece(logPos, ff, beginLogPos)
  91.     Offset logPos, *beginLogPos;
  92.     struct openFile *ff;
  93. {
  94.     extern char msgBuffer[];
  95.     extern int piecesSearchedFor;
  96.     extern int piecesScanned;
  97.     extern int scanned0pieces;
  98.     extern int scanned1pieces;
  99.     extern int scanned2pieces;
  100.     extern int scanned3PlusPieces;
  101.  
  102.     Piece pp;
  103.     Offset nn, n2;
  104.     int savePiecesScanned;
  105.  
  106. ++piecesSearchedFor;
  107. savePiecesScanned = piecesScanned;
  108.     /* see if we already know what piece it is in */
  109.     pp = ff->logPiece;
  110.     nn = ff->loLogPiece;
  111.     if( ff->loLogPiece <= logPos && logPos <= ff->hiLogPiece )
  112.         /*EMPTY*/
  113.     {
  114.         /* now nn = first logical byte in this piece */
  115.     } else {    /* go through the piece table */
  116.         if( logPos < nn ) {    /* below this piece? */
  117.             if( logPos < 0 ) {  /* error checking to be safe */
  118.                 msg("findPiece: byte number < 0", 1);
  119.                 return pp;    /* what else? */
  120.             }
  121.             /* search down for the piece */
  122.             while( logPos < nn ) {
  123.                 pp = pp->prevPiece;
  124. ++piecesScanned;
  125.                 nn -= pp->length;
  126.             }
  127.         } else {    /* must be at or above this piece */
  128.             if( logPos >= ff->fileSize ) {
  129.                 /* return the last piece */
  130.                 while( pp->nextPiece != NULL ) {
  131.                     nn += pp->length;
  132. ++piecesScanned;
  133.                     pp = pp->nextPiece;
  134.                 }
  135.             } else {
  136.                 /* search up for the piece */
  137.                 while( 1 ) {
  138.                     n2 = nn + pp->length;
  139.                     if( logPos < n2 )
  140.                         break;
  141.                     if( pp->nextPiece == NULL )
  142.                         break;
  143.                     nn = n2;
  144. ++piecesScanned;
  145.                     pp = pp->nextPiece;
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     *beginLogPos = nn;
  151. switch( piecesScanned - savePiecesScanned ) {
  152. case 0: ++scanned0pieces; break;
  153. case 1: ++scanned1pieces; break;
  154. case 2: ++scanned2pieces; break;
  155. default: ++scanned3PlusPieces; break;
  156. }
  157.     return pp;
  158. }
  159.