Using a simple file class

Using a simple file class

Introduction


In this article I will describe a simple file class for IO with text files. As you will see it is not really rocket science. However because I use this class so much and enriches my applications functionality in an easy applicable way, I like to share this class with you. First I will explain the class for you and afterward I will give you a number of examples of how to use this class.

Class TextFile

The Class is a wrapper around a number of default file functions, probably already available in the Clipper era. However because it is a class it is possible to easily store a number of variables in the class definition.

CLASS TextFile
       PROTECT pHandle AS PTR
       EXPORT status AS INT
METHOD Open(cFile) CLASS TextFile
IF (SELF:pHandle := FCreate(cFile, FC_NORMAL))=F_ERROR
       SELF:Status:=TF_ERROR
ELSE
       SELF:Status:=TF_OK
ENDIF 

IF FOpen
(cFile, FO_READ) <> F_ERROR
       SELF:pHandle:= FOpen(cFile, FO_READ)
SELF:Status:=TF_ERROR
ELSE
       SELF:Status:=TF_OK
ENDIF  

The pHandle is the member of the class that is used to connect to the File handle used in the Fopen or FCreate function in the various open methods. In this article you will see only a number of code snippets, however a sample application with the full code is included at the website of the SDN. When using the TextFile class you can do this with the following code:

LOCAL oTF AS TextFile
oTF := TextFile{}
oTF:Open(“C:\Test.txt”)
IF oTF:Status = TF_OK

ENDIF
oTF:Close() 

When the class is opened for writing you can call the methods Write and WriteLine and give it the string you want to write to the text file. For reading it has the same functionality, you open the class with read rights and call the method ReadLine. See the code snippet below.

METHOD Write(cString) CLASS TextFile
FWrite(SELF:pHandle, cString)
METHOD ReadLine() CLASS TextFile
LOCAL cString AS STRING   
cString:= FReadLine(SELF:pHandle)
RETURN cString 

That is actually the source code relevant for this class. What is so special about this source code. Actually I think the usage of this class is.

Consumer classes

With this TextFile class it is possible to easily create a number of different formattes text files. The first sample is a class that generates HTML documents and uses the Textile class as functionality to handle the output

CLASS GenerateHtml
       PROTECT oTF AS textFile
       PROTECT objService AS ServiceLayer
       PROTECT strErrors AS STRING

METHOD INit CLASS GenerateHtml
SELF:oTF := TEXTFile{}
SELF:objService := ;
CreateInstance(String2Symbol("ServicesObject"))
METHOD WriteHeader(strHeader) CLASS GenerateHtml
SELF:oTF:WriteLine("

" + strHeader + "

")
SELF:oTF:WriteLine("
") 


METHOD WriteHyperlink(strName, strCaption) CLASS GenerateHTML
SELF:oTF:WriteLine("" + strCaption + "")
SELF:oTF:WriteLine("
") 

METHOD WriteHtmlHeader() CLASS GenerateHtml
SELF:oTF:WriteLine("")
SELF:oTF:WriteLine("")

SELF:oTF:WriteLine("")
SELF:oTF:WriteLine("")
SELF:oTF:WriteLine("")


METHOD WriteHeaderText(strHeader, strText) CLASS GenerateHtml
IF strText<>"NIL"
       SELF:WriteHeader2(strHeader)
       SELF:writetext(strText)   
ENDIF  

The source code gives a number of samples how to use a HTML class that generates HTML code around the text you want to use for output. The service layer object is a connection class to my database with information I want to read data from. In this sample of WriteHtmlHeader you see how you can combine HTML with data using the WriteHeader or WriteHyperLink method. For more information see the sample application.

Another example of the usage is creating source code files from a data dictionary and put it into textfiles. In the sample code you will see that it is possible to generate VB.Net source code from a dictionary. 

CLASS DLA2VBDSSourcecode INHERIT DLASourcecode 
CLASS DLASourceCode
       PROTECT objService         AS ServicesObject
       PROTECT oTF                AS TextFile
       PROTECT strDir             AS STRING
       PROTECT strType:="VO"      AS STRING

METHOD GenerateDomain CLASS DLA2VBDSSourcecode
SELF:oTF:Open(SELF:strDir + "\MyDatasetmanager.vb")
objSelect := objService:ExecuteSupplyService("LOOKUP_CLASS",;
{})
F:oTF:WriteLine("Imports Microsoft.VisualBasic")
SELF:oTF:WriteLine("Imports System.Data")
SELF:oTF:WriteLine("Namespace DLAFormfactory")
SELF:oTF:WriteLine("    Partial CLASS DlaDataSetManager")
SELF:oTF:WriteLine("Protected Sub LoadDictionary")
SELF:oTF:WriteLine("Dim objTable as DataTable")
SELF:oTF:WriteLine("Dim objColumn as DataColumn")
SELF:oTF:WriteLine("Dim objRelation as DataRelation")

DO WHILE !objSelect:eof
      …
       objSelect:Skip(1)
ENDDO

SELF:oTF:WriteLine("End Sub")
SELF:oTF:WriteLine("End Class")
SELF:oTF:WriteLine("End Namespace")
SELF:oTF:Close()

summary 

 In this short article I described a class to use for writing to text files. The functionality is fairly simple, but makes it possible to generate a number of different formats like HTML, Excel and source code for VO-prg files of DotNet source code. I personally use the last functionality a lot for code generators and the TextFile class is easy to use and can easily be extended with wrapper classes with own functionality. Please see for further information the included zip file with the source code of the samples.

Download a sample AEF here

Bert Dingemans

Commentaar van anderen:
ChristianLouboutin op 14-8-2010 om 11:22
Christian Louboutin Shoes, Christian Louboutin, Christian Louboutin Shoes, Wedding Shoes, Christian Louboutin Copyright 2010, Chemicals Chemistry via VerticalNews. Christian Louboutin Shoes, Wedding Shoes Pattinson great actorly virtue is that he wears clothes well, so it too bad he slackered-out in cargo pants here. Christian Louboutin, Christian Louboutin Shoes, Wedding Shoes, Discount Christian Louboutin, Manolo Blahnik Shoes Tyler is less revealed than telegraphed through accessories a dead brother depth, a pack-a-day habit angst, a bookstore job smart, Discount Christian Louboutin, Louboutin, Christian Louboutin Sale, Louboutin Shoes, Sale Christian Louboutin Rodita zip sandals New style Black 14 a rich, aloof, and permanently disappointed daddy Pierce Brosnan. Louboutin Sale, Herve Leger Bandage Dress, Herve Leger Dress, Herve Leger V Neck Dress, Herve Leger Bandage Dress Falling for You Love, angst, and something else is in the air in Remember Me Remember Me Herve Leger Dress, Chanel Shoes, Yves Saint Laurent Shoes, Manolo Blahnik Shoes Platform Cage Sandal 13 by Allen Coulter Summit Entertainment Opens March 12 Putatively a new romance starring Robert Pattinson, Remember Me begins like a vigilante movie Alexander Wang Shoes, Louboutin Shoes, Louboutin Sale, Louboutin, Christian Louboutin Sale, Buy Christian Louboutin A Brooklyn subway platform, a racially charged stickup girl watches her mother get shot. Christian, Christian Louboutin Discount, Christian Dior Shoes, Christian Louboutin Pumps Pattinson great actorly virtue is that he wears clothes well, so it too bad he slackered-out in cargo pants here.
Geef feedback:

CAPTCHA image
Vul de bovenstaande code hieronder in
Verzend Commentaar