home *** CD-ROM | disk | FTP | other *** search
- /******************************************************/
- /* */
- /* LineCount.c */
- /* */
- /* Written in Think C version 4.0.2 */
- /* */
- /* Allen Stenger January 1991 */
- /* */
- /******************************************************/
-
- #include "LineCount.h"
-
- #define DOTIMING 0 /* set to 1 if timing */
- /* measurements desired */
-
- void DoLineCount( Handle textHandle,
- short *theLineCtP,
- long ***theLineStartsHP )
- {
- register
- char *dp = *textHandle;
- /* character pointer in loop */
- long offset = 0; /* = dp - *textHandle */
- long dataSize; /* size of **textHandle */
- long theLineStartsSize;
- /* allocated number of */
- /* entries in theLineStarts */
- long **localLineStartsH; /* shadow variable */
- short localLineCt;/* shadow variable */
- char saveChar; /* saves last char of file */
-
- #if DOTIMING
- long startTime,
- endTime;
- float elapsedTime;/* time to build table (sec)*/
- #endif
-
- #if DOTIMING
- startTime = TickCount();
- #endif
-
- theLineStartsSize = 2;
- localLineStartsH =
- (long **) NewHandle(theLineStartsSize *
- sizeof(**localLineStartsH));
- (*localLineStartsH)[0] = 0;
- dataSize = GetHandleSize(textHandle);
- if (dataSize == 0) {/* special case - file is empty */
- localLineCt = 1;
- (*localLineStartsH)[1] = 1;
- }
- else { /* normal case - file not empty */
- /* This search uses a "sentinel"; the last */
- /* character of the file is replaced with \r, so */
- /* that the check for end-of-file can be moved out*/
- /* of the inner loop and into the outer linecount */
- /* loop. This speeds up the search. */
- localLineCt = 0;
- saveChar = *(*textHandle+dataSize-1);
- *(*textHandle+dataSize-1) = '\r'; /* sentinel */
-
- while (offset != dataSize) {
- while (*dp++ != '\r')
- ; /* skip to next \r */
- localLineCt++;
- offset = dp - *textHandle;
- if (localLineCt >= theLineStartsSize) {
- /* add more space to localLineStartsH */
- theLineStartsSize += 1000;
- SetHandleSize( localLineStartsH,
- theLineStartsSize *
- sizeof(**localLineStartsH) );
- dp = offset + *textHandle;
- }
- (*localLineStartsH)[localLineCt] = offset;
- }
-
- *(*textHandle+dataSize-1) = saveChar; /* restore */
- SetHandleSize( localLineStartsH,
- (localLineCt+1) * sizeof(**localLineStartsH) );
- /* shrink to needed size */
- }
- /* return variables */
- *theLineCtP = localLineCt;
- *theLineStartsHP = localLineStartsH;
-
- #if DOTIMING
- endTime = TickCount();
- elapsedTime = (endTime - startTime) / 60.0;
- #endif
- }
-