REM PickTextLines - Pick up Selected lines from file (like head(1) & tail(1)) REM This code is in the public doain. REM by reishi@anet.ne.jp, Jan 4, 1998. INCLUDE "Const.oph" INCLUDE "System.oxh" REM main proedure... make dialogues and call PichTextLines&: routine PROC Main: GLOBAL sj1&(256) GLOBAL sj2%(256) LOCAL id% LOCAL s$(255) LOCAL ret&, ret2% LOCAL fNameIn$(255) LOCAL fNameOut$(255) LOCAL c% LOCAL startLine& LOCAL lines& LOCAL countlinesp% REM dialogue for the input file dINIT "PickTextLines - input file" dFILE fNameIn$, "Input File,Folder,Disk", 16+256+512 dCHECKBOX countLinesp%, "Need to count the lines" dBUTTONS "Cancel",-27,"OK",13 IF DIALOG = 0 RETURN ENDIF IF countlinesp% <> 0 PRINT "Counting lines of "; fNameIn$ lines& = CountLines&:( fNameIn$ ) PRINT "Count done... "; lines&; " lines." ENDIF REM dialogue for the output file & lines dINIT "PickTextLines - counts & output file" dFILE fNameOut$, "Output File,Folder,Disk", 1+8+256+512 IF countLinesp% = 0 lines& = 1 dLONG startLine&, "Start line", 0, 10000000 dLONG lines&, "Lines", 1, 10000000 ELSE dLONG startLine&, "Start line", 0, lines& dLONG lines&, "Lines", 1, lines& ENDIF dBUTTONS "Cancel",-27,"OK",13 IF DIALOG = 0 RETURN ENDIF REM This is the main routine ret& = PickTextLines&:( fNameIn$, fNameOut$, startline&, lines& ) IF ret& < 0 RETURN ENDIF REM dialogue for end dINIT "PickTextLines - done" dTEXT "Input:", fNameIn$ dTEXT "Start line:", NUM$(startline&,10) dTEXT "Lines:", NUM$(ret&,10) dTEXT "Output:", fNameOut$ dBUTTONS "OK",13 DIALOG ENDP REM This is the main routine. PROC PickTextLines&:( fNameIn$, fNameOut$, startline&, lines& ) LOCAL handleIn% LOCAL handleOut% LOCAL ret2% LOCAL s$(255) LOCAL outflag% LOCAL currentLine& LOCAL EndLine& LOCAL c% BUSY "Procesing", 2 ret2% = IOOPEN( handleIn%, fNameIn$, $0400 ) : REM open & share IF ret2% < 0 ALERT( "Error in opening input file.", fNameIn$, "QUIT" ) RETURN -1 ENDIF ret2% = IOOPEN( handleOut%, fNameOut$, $0002 ) : REM replace or create IF ret2% < 0 ALERT( "Error in opening Output file.", fNameOut$, "QUIT" ) RETURN -1 ENDIF outFlag% = 0 currentLine& = 1 EndLine& = startLine& + lines& BUSY "Skip leading lines", 2 WHILE currentLine& < startLine& c% = Getc%:( handleIn% ) IF c% < 0 BREAK ENDIF IF c% = 10 currentLine& = currentLine& + 1 PRINT "Skip "; currentLine& ENDIF ENDWH BUSY "Writing selected lines", 2 WHILE currentLine& < endLine& c% = Getc%:( handleIn% ) IF c% < 0 BREAK ENDIF IF c% = 10 REM control+J currentLine& = currentLine& + 1 Putc%:( handleOut%, 10 ) Putc%:( handleOut%, 13 ) PRINT "Output "; currentLine& ELSEIF c% = 13 REM cotrol+M ELSE Putc%:( handleOut%, c% ) ENDIF ENDWH BUSY "Closing", 2 IOCLOSE( handleIn% ) IOCLOSE( handleOut% ) BUSY OFF RETURN currentLine& - startLine& ENDP REM Count total lines in the input file, if specified. PROC CountLines&:( fNameIn$ ) LOCAL handleIn% LOCAL ret2% LOCAL lines& LOCAL c% ret2% = IOOPEN( handleIn%, fNameIn$, $0400 ) : REM open & share IF ret2% < 0 ALERT( "Error in opening input file.", fNameIn$, "QUIT" ) RETURN 1 ENDIF lines& = 0 WHILE 1 c% = Getc%:( handleIn% ) IF c% < 0 BREAK ENDIF IF c% = 10 lines& = lines& + 1 PRINT "Count "; lines& ENDIF ENDWH IOCLOSE( handleIn% ) RETURN( lines& ) ENDP REM Get 1 character from the file. PROC Getc%:( handleIn% ) local txt$(4), adr&, ret%, c2% adr& = ADDR( txt$ ) ret%=IOREAD( handleIn%, adr&+1, 1 ) IF ret% < 0 IF ret% = -36 RETURN -1 : REM EOF ELSE ALERT( "Error in Reading", "", "QUIT" ) RETURN -2 : REM Error ENDIF ENDIF POKEB adr&, ret% c2%=ASC(txt$) REturn c2% ENDP REM Put 1 character to the file. PROC Putc%:( handleOut%, c% ) local txt$(4), adr&, ret% txt$ = chr$( c% ) adr& = ADDR( txt$ ) ret%=IOWRITE( handleOut%, adr&+1, 1 ) IF ret% < 0 IF ret% = -36 RETURN -1 : REM EOF ELSE ALERT( "Error in Writing", "", "QUIT" ) RETURN -2 : REM Error ENDIF ENDIF RETURN 0 ENDP