VHP Computing Group. Home Page.
TechnologiesSpecialistsDownloadLinksSign GuestbookView Guestbook

27 July, 2000
Vladimir Trukhin
Senior Software Engineer
JSC "Votkinsk Hydroelectric Power Plant"
Fax: +7 (34241) 63297
E-mail:
vlt@votges.ru

Multi-channel output into text files.

Handy tools give great results.

The Problem

There are such tasks, in which the program makes record of data into text files from time to time. You can find many examples for this case.

As rule, programmers use '?' command for such output. Of course, if program outputs line by line then usage of command is very easy. That's a horse of a different color, when you need make output of a single line, close file and a little later make output for another line and so on. Even worse, if you need output lines by turns in different files. The 'SET' commands are flashing before your eyes.

The Solution

All becomes simpler if you have some class, which controls data stream to specific file and uses 'SET' commands itself according situation.

Such class helps to create objects which will control output streams into files. Now you can forget about 'SET' commands and output data very easy.

Approximate properties and methods

Properties:

· File name for the output channel
· Output mode: .T. - additive, .F. - overwrite
· Status of the channel: .T.-opened, .F.-closed

Methods:

· Open channel for output
· Open channel, output text line and close channel
· Output text line and retain channel status
· Close channel

Approximate class definition

DEFINE CLASS textfile AS custo

Name = "textfile"

*-- File name for output channel
filename = "output.txt"

*-- Path
path = ""

*-- Output mode: .T. - add records, .F. - create new file
additive = .F.

*-- Channel status: .T.-opened, .F.-closed
status = .F.

*-- Internal status
HIDDEN intstatus

*-- Console state befor channel openning
HIDDEN consol

*-- Output line openning and closing channel
PROCEDURE output
    LPARAMETERS lcString, llMode
    LOCAL lnParam
    lnParam=PARAMETER()
    IF lnParam < 2
        llMode=.T.
    ENDIF
    THIS.OpenOutput()
    IF llMode
        ? lcString
    ELSE
        ?? lcString
    ENDIF
    THIS.CloseOutput()
ENDPROC

*-- Open output channel
PROCEDURE openoutput
    IF this.Additive
        SET ALTERNATE TO (ALLT(THIS.Path)+'\'+ALLT(THIS.FileName)) ;
         ADDITIVE
    ELSE
        SET ALTERNATE TO (ALLT(THIS.Path)+'\'+ALLT(THIS.FileName))
    ENDIF
    SET ALTERNATE ON
    SET CONSOLE OFF
    THIS.IntStatus=.T.
ENDPROC

*-- Close output channel
PROCEDURE closeoutput
    SET ALTERNATE OFF
    SET ALTERNATE TO
    SET CONSOLE ON
    THIS.IntStatus=.F.
ENDPROC

PROCEDURE additive_assign
    LPARAMETERS vNewVal
    IF THIS.Status
        THIS.CloseOutput()
    ENDIF
    THIS.additive = m.vNewVal
ENDPROC

PROCEDURE filename_assign
    LPARAMETERS vNewVal
    IF THIS.Status
        THIS.CloseOutput()
    ENDIF
    THIS.filename = m.vNewVal
ENDPROC

PROCEDURE path_assign
    LPARAMETERS vNewVal
    IF THIS.Status
        THIS.CloseOutput()
    ENDIF
    THIS.path = m.vNewVal
ENDPROC

PROCEDURE status_assign
    LPARAMETERS vNewVal
    THIS.status = this.IntStatus
ENDPROC

*-- Output line does not changing channel state
PROCEDURE listoutput
    LPARAMETERS lcString, llMode
    LOCAL lnParam
    lnParam=PARAMETER()
    IF lnParam < 2
        llMode=.T.
    ENDIF
    IF llMode
        ? lcString
    ELSE
        ?? lcString
    ENDIF
ENDPROC

PROCEDURE status_access
    RETURN this.IntStatus
ENDPROC

PROCEDURE release
    RELEASE THIS
ENDPROC

PROCEDURE Init
    LPARAMETERS lcPath,lcFile,llAdd
    IF TYPE('lcPath')='C'
        THIS.Path=lcPath
    ENDIF
    IF TYPE('lcFile')='C'
        THIS.FileName=lcFile
    ENDIF
    IF TYPE('llAdd')='L'
        THIS.Additive=llAdd
    ENDIF
ENDPROC
ENDDEFINE

The example of usage

It creates two output channels into different text files.

#DEFINE CR .T.
#DEFINE NOCR .F.

LOCAL loChannelOne, loChannelTwo
* create output channels
loChannelOne=CREATEOBJECT('TextFile',CURDIR(),'First.txt',.T.)
loChannelTwo=CREATEOBJECT('TextFile',CURDIR(),'Second.txt',.T.)

loChannelOne.Additive=.F.
loChannelOne.Output('The Memoirs of Sherlock Holmes. Silver Blaze.',NOCR)
loChannelOne.Additive=.T.
loChannelOne.Output('',CR)
loChannelTwo.Additive=.F.
loChannelTwo.Output('The Return of Sherlock Holmes.',NOCR)
loChannelTwo.Additive=.T.
loChannelTwo.Output('The Adventury of the Empty House',CR)
loChannelTwo.Output('',CR)
* ... some code lines
loChannelOne.OpenOutput()
loChannelOne.ListOutput('...And so it happened that an hour '+;
'or so later I found myself in the',CR)
loChannelOne.ListOutput('corner of a first-class carriage flying '+;
'along en route for Exeter, while',CR)
loChannelOne.ListOutput('Sherlock Holmes, with his sharp, eager '+;
'face framed in his ear-flapped...',CR)
loChannelOne.CloseOutput()
* ... some code lines
loChannelTwo.Output(' '+;
'... Even now, after this long interval, I',CR)
loChannelTwo.Output('find myself thrilling as I think of it, '+;
'and feeling once more that sudden',CR)
* ... some code lines
loChannelTwo.Output('flood of joy, amazement, and incredulity which '+;
'utterly submerged my',CR)
loChannelTwo.Output('mind...',CR)
RETURN

The result:

File FIRST.TXT

The Memoirs of Sherlock Holmes. Silver Blaze.

...And so it happened that an hour or so later I found myself in the
corner of a first-class carriage flying along en route for Exeter, while
Sherlock Holmes, with his sharp, eager face framed in his ear-flapped...

File SECOND.TXT

The Return of Sherlock Holmes.
The Adventury of the Empty House

... Even now, after this long interval, I
find myself thrilling as I think of it, and feeling once more that sudden
flood of joy, amazement, and incredulity which utterly submerged my

You can download the article and program sample from file: outtext.zip. Size: 18KB.
There is a Russian version of article in file
outtextr.zip. Size: 15KB.


Home Page | Technologies | Specialists | Download | Links | Sign Guestbook | View Guestbook