| Re: Help: Countlines on Win 95 version of IDL [message #10531 is a reply to message #10502] |
Thu, 18 December 1997 00:00   |
mgs
Messages: 144 Registered: March 1995
|
Senior Member |
|
|
In article <34981500.572F0480@fz-juelich.de>, "R. Bauer"
<r.bauer@fz-juelich.de> wrote:
> PDW wrote:
>
>> I am converting programs from Unix version of IDL to Windows 95, and have a
>> problem with routines using SPAWN and "wc -l" to get the number of lines in
>> a file in order to dimension an array. How can this be done on the PC
>> version?
Here's another "Fileline". Same purpose and name, different author. Works
cross-platform (UNIX, Mac and PC) and shouldn't gag on huge files.
;########################################################### ################
; File Name: %M%
; Version: %I%
; Author: Mike Schienle
; Orig Date: 97-03-09
; Delta Date: %G% @ %U%
;########################################################### ################
; Purpose:
; History:
;########################################################### ################
; %W%
;########################################################### ################
FUNCTION FileLine, file, LINETERM=lineTerm
; open the file and check for errors
OpenR, lun, file, /Get_Lun, Error=err, BufSize=2L^20
IF NOT (err) THEN BEGIN
; if line terminator is not specified
; assume the file is native to the OS
; UNIX uses line feed
; Mac uses carriage return
; PC uses both
IF (N_Elements(lineTerm) EQ 0L) THEN $
IF (StrLowCase(!Version.OS_Family) EQ 'unix') THEN $
lineTerm = 10b $
ELSE $
lineTerm = 13b
; use FStat function to get access to file size
fInfo = FStat(lun)
; set an arbitrary size threshold
lThresh = 2L^20
; compare the file size to the threshold
IF (fInfo.Size LT lThresh) THEN BEGIN
; allocate array to read entire file into memory
aData = BytArr(fInfo.Size, /NoZero)
ReadU, lun, aData
; find the number of characters matching line terminator
; Provided in lCount. Not interested in positions.
aPos = Where(aData EQ lineTerm, lCount)
ENDIF ELSE BEGIN
; file arbitrarily too large to read into a single array
; determine the number of loops to be processed
lLoops = fInfo.Size / lThresh
; associate a variable for easy access
mVar = Assoc(lun, BytArr(lThresh, /Nozero))
; init a var to hold the terminator count
lCount = 0L
; loop through the data file one threshold chunk at a time
FOR i = 0L, (lLoops - 1) DO BEGIN
; find the number of characters matching line terminator
; Provided in lPos. Not interested in positions.
aPos = Where(mVar(i) EQ lineTerm, lPos)
; running sum of terminator counts
lCount = lCount + lPos
ENDFOR
; get amount of file remainder
lModSize = (fInfo.Size MOD lThresh)
; read remainder of file if necessary
IF (lModSize NE 0) THEN BEGIN
; create an array to read in remaining data
aData = BytArr(lModSize, /NoZero)
; put the data in the file
ReadU, lun, aData
; find the number of characters matching line terminator
; Provided in lPos. Not interested in positions.
aPos = Where(aData EQ lineTerm, lPos)
; running sum of terminator counts
lCount = lCount + lPos
ENDIF
ENDELSE
; free and close the lun
Free_Lun, lun
ENDIF ELSE BEGIN
; an error occurred
lCount = -1
ENDELSE
; return the lCount to the calling program
Return, lCount
END
--
Mike Schienle Interactive Visuals
mgs@sd.cybernex.net http://ww2.sd.cybernex.net/~mgs/
|
|
|
|