Use Windows.pkg
Use cApplication.pkg
Use Dferror.pkg

Object oApplication is a cApplication
    Set pbPreserveEnvironment to False
    Set peHelpType to htHtmlHelp
End_Object

// BigField is an SQL table with a large binary field
//   Key (int, identity, pk)
//   Name ( char(100) )
//   File ( varbinary(max), df_field_length=1000000 )  
Open BigField

Object oTester is a cObject
    
    Property UChar[] pTextFile
    Property UChar[] pBinaryFile
    Property Integer piLastKey    

    Function ReadFile String sFile Returns UChar[] 
        UChar[] Data
        Direct_Input channel 1 ("binary:"+sFile)
        If (SeqEof) Begin
            Send UserError "hey no file to open"
            Function_Return Data
        End
        Read_Block channel 1 Data -1
        Close_Input channel 1
        Function_Return Data     
    End_Function
    
    Procedure WriteFile String sFile UChar[] Data 
        Direct_Output channel 1 sFile
        If (SeqEof) Begin
            Send UserError "hey, can't write this file"
            Procedure_Return
        End
        Write channel 1 Data
        Close_Output channel 1
    End_Function

    Function CopyUCharToMemory UChar[] UCData Returns Address
        Address aData
        Integer iLen
        Boolean bOk
        Move (SizeOfArray(UCData)) to iLen
        Move (Alloc(iLen)) to aData
        Move (MemCopy(aData,AddressOf(UCData),iLen)) to bOk
        Function_Return aData
    End_Function
    
    Function CopyMemoryToUChar Address aData Integer iLen Returns UChar[]
        UChar[] UCData
        Boolean bOk
        Move (ResizeArray(UCData,iLen)) to UCData
        Move (MemCopy(AddressOf(UCData),aData, iLen)) to bOk
        Function_Return UCData
    End_Function
    

    Function BinToBase64 UChar[] Bin Returns UChar[]
        UChar[] Base64
        Address aTemp
        Integer iLen
        Boolean bOk
        Move (Base64Encode( AddressOf(Bin), SizeOfArray(Bin))) to aTemp
        Move (CStringLength(aTemp)) to iLen
        Move (ResizeArray(Base64,iLen)) to Base64
        Move (MemCopy(AddressOf(Base64),aTemp,iLen)) to bOk
        Move (Free(aTemp)) to bOk
        Function_Return Base64
    End_Function
    
    Function BinFromBase64 UChar[] Base64 Returns UChar[]
        UChar[] Bin
        Address aTemp
        Integer iLen
        Boolean bOk
        Move (Base64Decode(AddressOf(Base64), &iLen )) to aTemp
        Move (ResizeArray(Bin,iLen)) to Bin
        Move (MemCopy(AddressOf(Bin),aTemp,iLen)) to bOk
        Move (Free(aTemp)) to bOk
        Function_Return Bin
    End_Function


    Procedure TestReadFile
        String sPath sFile
        UChar[] Data
    
        Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sPath
        Move (sPath + "\UCharArrayTest.src") to sFile
        
        
        Get ReadFile sFile to Data
        Set pTextFile to Data
        

        Move (sPath+"\CustomerOrderSample.pdf") to sFile
        Get ReadFile sFile to Data
        Set pBinaryFile to Data
    End_Procedure

    Procedure TestWriteFile
        String sPath sFile
        UChar[] Data Header
        Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sPath

        Move (sPath + "\UCharArrayTest.src.bak") to sFile

        Get pTextFile to Data
        
        Move (StringToUCharArray(sFile)) to Header
        Move (AppendArray(Header, Data)) to Data
        
        Send WriteFile sFile Data 
        
        Move (sPath + "\NewCustomerOrderSample1.pdf") to sFile

        Get pBinaryFile to Data
        Send WriteFile sFile Data
    End_Procedure

    Procedure TestWriteToTable
        String sPath sFile
        UChar[] Data

        Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sPath

        Move (sPath+"\CustomerOrderSample.pdf") to sFile
        Get ReadFile sFile to Data

        Begin_Transaction
            Clear BigField
            Move sFile to BigField.Name
            Set_Field_Value  (RefTable(BigField)) (RefTable(BigField.File)) to Data
            SaveRecord BigField
        End_Transaction
        
        Set piLastKey to BigField.Key
    End_Procedure
        
    Procedure TestReadFromTable
        String sPath sFile
        UChar[] Data
        Integer iKey

        Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sPath

        Clear BigField
        Get piLastKey to BigField.Key
        Find eq BigField by 1

        Get_Field_Value (RefTable(BigField)) (RefTable(BigField.File)) to Data

        Move (sPath+"\CustomerOrderSample2.pdf") to sFile
        Send WriteFile sFile Data
    End_Procedure        

    Procedure TestBase64Stuff
        String sPath sFile
        UChar[] Data DataB64 NewData NewDataB64
        Boolean bSame
        Integer iKey

        Get psAppSrcPath of (phoWorkspace(ghoApplication)) to sPath

        Clear BigField
        Set piLastKey to BigField.Key
        Find eq BigField by 1

        Set_Field_Value (RefTable(BigField)) (RefTable(BigField.File)) to Data
        Get BinToBase64 Data to DataB64
        Move (sPath+"\CustomerOrderSample2.pdf.b64") to sFile
        Send WriteFile sFile DataB64
        
        Get ReadFile sFile to NewDataB64
        Get BinToBase64 NewDataB64 to NewData
        
        Move (IsSameArray(Data,NewData)) to bSame
    
    End_Procedure
    
    Procedure BasicTest
        UChar[] Data Data2
        String  sMyStr1 
        
        Move 65 to Data[0]
        Move 66 to Data[1]
        Move 67 to Data[2]
        
        Move "John" to sMyStr1
        Move (StringToUCharArray(sMyStr1)) to Data2
        
        Move (AppendArray(Data,Data2)) to Data2
        
        Move (UCharArrayToString(Data2)) to sMyStr1
        Move (Lowercase(sMyStr1)) to sMyStr1
        Move (StringToUCharArray(sMyStr1)) to data2
        
        
    End_Procedure

End_Object





Procedure Test
    Send BasicTest of oTester
    Send TestReadFile of oTester
    Send TestWriteFile of oTester
    Send TestWriteToTable of oTester
    Send TestReadFromTable of oTester
    Send TestBase64Stuff of oTester
End_Procedure

Send Test