home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.08 Aug 91 / Browser sources / LineCount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-19  |  2.7 KB  |  92 lines  |  [TEXT/KAHL]

  1. /******************************************************/
  2. /*                                                                                                      */
  3. /*    LineCount.c                                                                              */
  4. /*                                                                                                      */
  5. /*    Written in Think C version 4.0.2                                  */
  6. /*                                                                                                      */
  7. /*    Allen Stenger    January 1991                                              */
  8. /*                                                                                                      */
  9. /******************************************************/
  10.  
  11. #include "LineCount.h"
  12.  
  13. #define DOTIMING    0    /* set to 1 if timing                         */
  14.                                         /* measurements desired                     */
  15.  
  16. void DoLineCount(     Handle     textHandle, 
  17.                                         short        *theLineCtP,
  18.                                         long        ***theLineStartsHP )
  19. {
  20.     register 
  21.     char            *dp = *textHandle;
  22.                                                 /* character pointer in loop     */
  23.     long            offset = 0;    /* = dp - *textHandle                 */
  24.     long            dataSize;        /* size of **textHandle             */
  25.     long            theLineStartsSize;    
  26.                                                 /* allocated number of                 */
  27.                                                 /* entries in theLineStarts     */
  28.     long            **localLineStartsH;    /* shadow variable        */
  29.     short            localLineCt;/* shadow variable                        */
  30.     char            saveChar;        /* saves last char of file         */
  31.     
  32.     #if DOTIMING
  33.         long            startTime,
  34.                             endTime;
  35.         float            elapsedTime;/* time to build table (sec)*/
  36.     #endif
  37.  
  38.     #if DOTIMING    
  39.         startTime = TickCount();
  40.     #endif
  41.     
  42.     theLineStartsSize = 2;
  43.     localLineStartsH = 
  44.                             (long **) NewHandle(theLineStartsSize * 
  45.                                 sizeof(**localLineStartsH));
  46.     (*localLineStartsH)[0] = 0;
  47.     dataSize = GetHandleSize(textHandle);
  48.     if (dataSize == 0) {/* special case - file is empty */
  49.         localLineCt = 1;
  50.         (*localLineStartsH)[1] = 1;
  51.         }
  52.     else { /* normal case - file not empty */
  53.         /* This search uses a "sentinel"; the last                 */
  54.         /* character of the file is replaced with \r, so    */
  55.         /* that the check for end-of-file can be moved out*/
  56.         /* of the inner loop and into the outer linecount    */
  57.         /* loop. This speeds up the search.                                */
  58.         localLineCt = 0;
  59.         saveChar = *(*textHandle+dataSize-1);
  60.         *(*textHandle+dataSize-1) = '\r'; /* sentinel         */
  61.         
  62.         while (offset != dataSize) {
  63.             while (*dp++ != '\r')
  64.                 ;    /* skip to next \r */
  65.             localLineCt++;
  66.             offset = dp - *textHandle;
  67.             if (localLineCt >= theLineStartsSize) { 
  68.                 /* add more space to localLineStartsH */
  69.                 theLineStartsSize += 1000;
  70.                 SetHandleSize( localLineStartsH, 
  71.                         theLineStartsSize * 
  72.                             sizeof(**localLineStartsH) );
  73.                 dp = offset + *textHandle;    
  74.                 }
  75.             (*localLineStartsH)[localLineCt] = offset;
  76.             }
  77.             
  78.         *(*textHandle+dataSize-1) = saveChar;    /* restore    */
  79.         SetHandleSize( localLineStartsH, 
  80.                 (localLineCt+1) * sizeof(**localLineStartsH) ); 
  81.                     /* shrink to needed size */
  82.     }
  83.     /* return variables */
  84.     *theLineCtP = localLineCt;
  85.     *theLineStartsHP = localLineStartsH;
  86.     
  87.     #if DOTIMING
  88.         endTime = TickCount();
  89.         elapsedTime = (endTime - startTime) / 60.0;
  90.     #endif
  91. }
  92.