;+ ; NAME: ; MYNLINES ; PURPOSE: ; Count the number of lines in a formatted (i.e., text) file. ; CATEGORY: ; Utility. ; CALLING SEQUENCE: ; n=MYNLINES(file,/Help) ; INPUTS: ; file: Name of the file whose lines are to be counted. ; OPTIONAL INPUTS: ; None. ; KEYWORD PARAMETERS: ; /Help: Display the header of this routine. ; OUTPUTS: ; n: The number of lines (delimited with CR-LF) in the file. ; OPTIONAL OUTPUTS: ; None: ; COMMON BLOCKS: ; None: ; SIDE EFFECTS: ; The file is opened for reading. ; RESTRICTIONS: ; The file must exist and be accessible, i.e., not in use by another ; application. ; PROCEDURE: ; The file is opened, and FSTAT is used to determine the number of ; bytes in the file. A BYTARR big enough to hold the file is ; created and the whole file is read into it. Where() is used to ; locate all occurrences of 10B (the LF ASCII code). The number ; of such occurrences is returned. ; EXAMPLE: ; n=MYNLINES('c:\idl\steele\mynlines.pro') ; SEE ALSO: ; C:\IDL\USER_CON\KNIGHT\NLINES.PRO for a more general but much ; slower implementation of the same function. ; MODIFICATION HISTORY: ; Written by: DPSteele, 970120. ;- FUNCTION mynlines,file,help=help ; Provide help if requested. IF Keyword_Set(help) THEN BEGIN doc_library,'mynlines' Return,-1 ENDIF On_IOError, Alert ; Verify file's existence first. f=RFindFile(file,Count=nf) IF nf EQ 0 THEN BEGIN Message,file+' not found',/INFORMATIONAL Return,0 ENDIF ; Read the file. OpenR,lun,file,/Get_LUN fs=FStat(lun) b=BytArr(fs.SIZE) ReadU,lun,b ; We got this far; use GOTO to get to file closure. GOTO, Done ; An I/O error occurred; notify the user and close the file. Alert: Print,'An I/O error occurred.' ; Close the file. Done: Free_LUN,lun ; Count the lines. w=Where(b EQ 10B,nw) ; Return. Return,nw END