home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The CDPD Public Domain Collection for CDTV 4
/
CDPD_IV.bin
/
e
/
mailinglists
/
binaries
/
readstr-study.lha
/
rwt5.e
< prev
Wrap
Text File
|
1993-07-24
|
7KB
|
240 lines
/* Timing of the buffered read string function used in EPP, slightly */
/* modified here for readability. */
MODULE 'dos/dos'
OBJECT st_stackType /* Belongs to stack routines. */
top, count
ENDOBJECT
OBJECT et_procInfoType /* Belongs to timer routines. */
visits : LONG
started : datestamp
elapsed : datestamp
name : LONG
ENDOBJECT
/* Belong to timer routines. */
DEF et_procInfoArray : PTR TO et_procInfoType,
et_stack : st_stackType,
et_numberOfProcs = 0,
et_idRunning = -1 /* Used to turn timer off/on */
/* in case of recursive call. */
/* Used by et_StartTime AND */
/* et_StopTime. */
ENUM ER_NONE,
ER_MEM
RAISE ER_MEM IF String () = NIL,
ER_MEM IF New () = NIL
/* Belongs to readStr() routine. */
OBJECT readDataType /* You need one of these for each file. */
/*-- public: ---------*/
fh : LONG /* := handle of file you have opened */
bufSize : LONG /* := inputBuffer size */
maxLineLength : LONG /* := StrMax() (may be dynamic to read */
/*-- private: --------*/ /* in strings of different length.) */
inputBuffer : LONG /* create this with New() */
length : LONG /* num bytes read into inputBuffer */
pos : LONG /* next available char in inputBuffer */
ENDOBJECT
/*-- Stack routines used by timer ----------------------------------------*/
PROC st_init (theStack : PTR TO st_stackType)
/* Simply declare the stack variable as st_stackType. The status of the */
/* stack can be checked by testing stackname.count. */
theStack.top := NIL
theStack.count := 0
ENDPROC
/* st_init */
PROC st_push (theStack : PTR TO st_stackType, addr)
DEF newList, tempList
newList := List (1)
ListCopy (newList, [addr], ALL)
tempList := Link (newList, theStack.top)
theStack.top := tempList
theStack.count := theStack.count + 1
ENDPROC
/* st_push */
PROC st_pop (theStack : PTR TO st_stackType)
DEF list, addr = NIL
IF theStack.count
list := theStack.top
addr := ^list
theStack.top := Next (list)
theStack.count := theStack.count - 1
ENDIF
ENDPROC addr
/* st_pop */
/*-- Timer routines ------------------------------------------------------*/
PROC et_init (numberOfProcs)
DEF i, elapsed : PTR TO datestamp
et_numberOfProcs := numberOfProcs + 1
et_procInfoArray := New ((SIZEOF et_procInfoType) * et_numberOfProcs)
FOR i := 0 TO numberOfProcs
et_procInfoArray [i].visits := 0
elapsed := et_procInfoArray [i].elapsed
elapsed.minute := 0
elapsed.tick := 0
ENDFOR
st_init (et_stack)
ENDPROC
PROC et_startTimer (id, name)
DEF current : datestamp,
started : PTR TO datestamp,
elapsed : PTR TO datestamp
VOID DateStamp (current)
/* Update the elapsed time of the proc that relinquished control to */
/* child. Init if et_idRunning = -1 (PROC main () is the *only* case.) */
IF et_idRunning = -1
et_init (id)
ELSE
started := et_procInfoArray [et_idRunning].started
elapsed := et_procInfoArray [et_idRunning].elapsed
IF current.tick < started.tick
current.tick := current.tick + 3000
current.minute := current.minute - 1
ENDIF
elapsed.tick := elapsed.tick + (current.tick - started.tick)
elapsed.minute := elapsed.minute + (current.minute - started.minute)
ENDIF
st_push (et_stack, et_idRunning)
/* Update the start time of the child proc. */
started := et_procInfoArray [id].started
started.minute := current.minute
started.tick := current.tick
et_procInfoArray [id].name := IF name = NIL THEN '' ELSE name
et_idRunning := id
et_procInfoArray [id].visits := et_procInfoArray [id].visits + 1
ENDPROC
PROC et_toMinutes (ticks) RETURN ticks / 3000
PROC et_report ()
DEF i,
totalMinute = 0,
totalTick = 0,
ds : PTR TO datestamp
FOR i := 0 TO (et_numberOfProcs - 1)
ds := et_procInfoArray [i].elapsed
ds.minute := ds.minute + et_toMinutes (ds.tick)
ds.tick := ds.tick - (et_toMinutes (ds.tick) * 3000)
WriteF ('\nid=\d, visits=\d, minute=\d, tick=\d, name=\s',
i,
et_procInfoArray [i].visits,
ds.minute,
ds.tick,
et_procInfoArray [i].name)
totalMinute := totalMinute + ds.minute
totalTick := totalTick + ds.tick
ENDFOR
totalMinute := totalMinute + et_toMinutes (totalTick)
totalTick := totalTick - (et_toMinutes (totalTick) * 3000)
WriteF ('\ntotalMinute=\d totalTick=\d\n', totalMinute, totalTick)
ENDPROC
PROC et_stopTimer ()
DEF current : datestamp,
started : PTR TO datestamp,
elapsed : PTR TO datestamp
VOID DateStamp (current)
/* Update the elapsed time of the child proc that id returning control */
/* to the parent. None if et_idRunning = -1 (PROC main () is the */
/* *only* case.) */
started := et_procInfoArray [et_idRunning].started
elapsed := et_procInfoArray [et_idRunning].elapsed
IF current.tick < started.tick
current.tick := current.tick + (50 * 60)
current.minute := current.minute - 1
ENDIF
elapsed.tick := elapsed.tick + (current.tick - started.tick)
elapsed.minute := elapsed.minute + (current.minute - started.minute)
/* Update the start time of the parent proc. None if et_idRunning = -1 */
/* (PROC main () is the *only* case.) */
et_idRunning := st_pop (et_stack)
IF et_idRunning > -1
started := et_procInfoArray [et_idRunning].started
started.minute := current.minute
started.tick := current.tick
ELSE
et_report ()
ENDIF
ENDPROC
/*-- readStr() routine ---------------------------------------------------*/
PROC readStr (rData : PTR TO readDataType,
string)
DEF inputBuffer,
i = 0,
pos
inputBuffer := rData.inputBuffer
pos := rData.pos
WHILE i < rData.bufSize
IF pos >= rData.length
rData.length := Read (rData.fh, inputBuffer, rData.bufSize)
IF rData.length < 1
IF i > 0
string [i++] := 10
string [i] := 0
rData.length := i
ENDIF
rData.pos := rData.length
RETURN i
ENDIF
pos := 0
ENDIF
string [i] := inputBuffer [pos++]
IF (string [i++] = 10) OR /* LF */
(i = rData.maxLineLength)
SetStr (string, i)
rData.pos := pos
RETURN i
ENDIF
ENDWHILE
ENDPROC
/* read */
PROC main () HANDLE
DEF fho, rd : readDataType, s, l
s := String (80)
/* Init read data object. */
rd.bufSize := 128
rd.inputBuffer := New (rd.bufSize)
rd.maxLineLength := 80 /* <-- can be dynamic if reading into */
rd.length := 0 /* strings of different length. */
rd.pos := 0
fho := Open ('rwt5.out', NEWFILE)
rd.fh := Open ('testfile.txt', OLDFILE)
IF rd.fh AND fho
et_startTimer (0, 'readStr() [rewrite #3 with buffer]')
WHILE (l := readStr (rd, s)) > 0
Write (fho, s, l)
ENDWHILE
et_stopTimer ()
Close (rd.fh)
Close (fho)
ENDIF
CleanUp (0)
EXCEPT
IF rd.fh THEN Close (rd.fh)
IF fho THEN Close (fho)
IF exception = ER_MEM THEN WriteF ('Not enough memory.\n')
CleanUp (0)
ENDPROC