Class: cSeqFileHelper

Properties  Events  Methods    Index of Classes

A helper class, assists in reading and writing sequential files in various formats - binary and character (OEM, ANSI, UTF-8, UTF-16)

Hierarchy

cObject
---cSeqFileHelper

Library: Common (Windows and Web Application) Class Library

Package: cSeqFileHelper.pkg

Description

A helper class, which assists in reading and writing sequential files in various formats - binary and character (OEM, ANSI, UTF-8, UTF-16)

The cSeqFileHelper class allows you to create an object or sub-class that can handle reading and writing sequential files in various formats. You can use this to read and write binary files and you can use this to read and write character files in various encoding formats. This will often be used in conjunction with the cCharTranslate class.

Binary Files


The methods ReadBinFileToBuffer and WriteBinFileFromBuffer can read and write binary files from and to a memory buffer. When working with memory buffers, it is your eventual responsibility to dispose of any allocated memory.

Character Files


The methods ReadFileToVariantStr and WriteFileFromVariantStr can be used to read and write character files from and to a variant string. You may specify the character encoding format of these. This provides a mechanism for reading and writing OEM, ANSI, UTF-8 and UTF-16 files. These files use the Variant data type as variable. Using Variant means you do not need to worry about disposing of the data and the data will be converted from the passed format and stored as Unicode within the Variant. Refer to cCharTranslate for a complete description of using Variant strings for this purpose.

Sample

This example shows how this class can be used to read in a binary pdf file, convert it to a base64 encoded string and save it as a character file. This is demonstrated in BinaryReadTest. In BinaryWriteTest, it shows how to read this base64 character file, decode it to binary and save it as a new pdf file.

Use Windows.pkg
Use cApplication.pkg
Use cCharTranslate.pkg
Use cSeqFileHelper.pkg

// This requires that the following file is placed in AppSrc
// TestIn.pdf - a binary PDF file

Object oApplication1 is a cApplication
End_Object

Object oCharTranslate is a cCharTranslate
End_Object

Object oFileHelper is a cSeqFileHelper
End_Object

Procedure BinaryReadTest
    Variant vBase64
    String sName sNameBase64 sAppSrc
    Integer iBinSize iVoid
    Address aPdf
    
    Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sAppSrc

    // read in a binary file to aPdf of size iBinSize
    Move (sAppSrc +"\TestIn.pdf") to sName
    Get ReadBinFileToBuffer of oFileHelper sName (&iBinSize)to aPdf
    
    // convert file to base64 encoded variant string
    Get Base64EncodeToVariantStr of oCharTranslate aPdf iBinSize to vBase64

    Move (Free(aPdf)) to iVoid

    // save the base64 file as base64
    Move (sAppSrc +"\TestBase64.txt") to sNameBase64
    // note with base64 encoding it doesn't matter what the code page is
    Send WriteFileFromVariantStr of oFileHelper sNameBase64 CP_OEMCP vBase64  
End_Procedure

Procedure BinaryWriteTest
    Variant vBase64
    String sName sNameBase64 sAppSrc
    Integer iBinSize iVoid
    Address aPdf
    
    Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sAppSrc

    // read the base64 file to variant string
    Move (sAppSrc +"\TestBase64.txt") to sNameBase64
    Get ReadFileToVariantStr of oFileHelper sNameBase64 CP_OEMCP to vBase64
    
    // convert file from base64 encoded variant to buffer
    Get Base64DecodeFromVariantStr of oCharTranslate vBase64 (&iBinSize) to aPdf
    
    // write to binary file
    Move (sAppSrc +"\TestOut.pdf") to sName
    Send WriteBinFileFromBuffer of oFileHelper sName aPdf iBinSize
    Move (Free(aPdf)) to iVoid
End_Procedure

Send BinaryReadTest
Send BinaryWriteTest

Note that in this example, the character encoding for the character read and write could have been OEM, ANSI or UTF-8 because base64 encoding is the same for all of these character formats.