DOpus4-ToB64.rexx
/*
 *  $VER: DOpus4-ToB64 1.0 (1 Jul 2015) by Wizardry and Steamworks
 *
 *      © 2015 Wizardry and Steamworks
 *
 *  PROGRAMNAME:
 *      DOpus4-ToB64
 *
 *  FUNCTION:
 *      Encodes a file to Base64 and copies the resulting file to the other pane.
 *
 *  USAGE:
 *      ARexx command DOpus4-ToB64.rexx (from DOpus)
 *
 *  $HISTORY:
 *
 *  1 Jul 2015 : 1.0 : initial release
 *
 */
 
/*------- Configuration Variables (change these to suit your setup) --------*/
ChunkSize = 36         /* The chunk size to write: must be a multiple of 6. */
/*--------------------------------------------------------------------------*/
 
Opus = Address()    /* Get the DOpus address. */
Options RESULTS     /* Request results. */
Address value Opus  /* Use the DOpus address. */
 
Busy On             /* Set the busy pointer. */
 
Status 3                    /* Get this window. */
ThisWindow = RESULT
ThatWindow = ~ThisWindow    /* That window is not this window. */
 
/* Get this path. */
Status 13 ThisWindow
ThisPath = RESULT
If ThisPath = 'RESULT' Then
    Do
        TopText "No path in this window!"
        Busy Off
        Exit
    End
 
/* Get that path. */
Status 13 ThatWindow
ThatPath = RESULT
If ThatPath = 'RESULT' Then
    Do
        TopText "No path in that window!"
        Busy Off
        Exit
    End
 
GetSelectedAll '*' ThisWindow  /* Get all the selected items. */
Items = RESULT
If Items = 'RESULT' Then                /* If no items in this window then bail. */
    Do
        TopText "No items selected in this window!"
        Busy Off
        Exit
    End
 
/* Normalize, heh... */
Items = Translate(Translate(Items, '/', ' '), ' ', '*')
 
Count = Words(Items)    /* Get the number of selected items. */
If n <= 0 Then          /* Check if no items were selected. */
    Do                  /* If no items were selected, */
        Busy Off        /* turn off the busy pointer. */
        Exit            /* and terminate. */
    End
 
Do i = 1 To Count
    Name = Translate(Word(Items, i), ' ', '/')
    ScrollToShow Name
    If wasFileToBase64(ThisPath||Name, ThatPath||Name||'.b64', ChunkSize, 'TopText "Writing Base64 Characters ["Count"]"') ~= 1 Then
        Do
            Notify "fail..."
            TopText "Failed to convert file: "Name
            Iterate i
        End
    TopText "Converted file: "Name
    Call wasDOpus4DeselectEntry(Name)
    Rescan ThatWindow
End
 
If n ~= 1 Then Rescan ThatWindow
 
TopText "<(^.-)> There you go!"
 
Busy Off    /* Turn off the busy pointer. */
Exit        /* Terminate. */
 
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
wasFileToBase64: procedure /* Encodes a file to a Base64 file.           */
    Parse ARG Ins,Ous,Chunk,Lambda
 
    If Chunk // 6 ~= 0 Then Return 0
 
    If Open('ins', Ins, 'READ') ~= 1 | Open('ous', Ous, 'WRITE') ~= 1 Then 
        Return 0
 
    Count = 0
    Do While ~Eof('ins')
        Ins = ReadCh('ins', Chunk)
        Segment = wasBase64Encode(Ins)
 
        Length = Length(Segment)
        Interpret Lambda
        Do i = 1 To Length
            If WriteCh('ous', SubStr(Segment, i, 1)) ~= 1 Then 
                Do
                    Close('ins')
                    Close('ous')
                    Return 0
                End
            Count = Count + 1
            If Count // 76 = 0 Then
                If WriteCh('ous', D2C(13)||D2C(10)) ~= 2 Then
                    Do
                        Close('ins')
                        Close('ous')
                        Return 0
                    End
        End
    End
 
    Close('ins')
 
    If WriteCh('ous', D2C(13)||D2C(10)) ~= 2 Then
        Do
            Close('ous')
            Return 0
        End
 
    Close('ous')
 
Return 1
 
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
wasBase64Encode: procedure /* Encodes a string into Base64.              */
    Parse ARG String
 
    If Length(String) = 0 Then Return ''
 
    Pad = ""
    Do While Length(String) // 3 ~= 0
        String = String||X2C(0)
        Pad = Pad||'='
    End
 
    Count = Length(String)
    BinaryString = ''
    Do i = 1 To Count
        BinaryString = BinaryString||C2B(SubStr(String, i, 1))
    End
 
    b64 =,
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
 
    Count = Length(BinaryString)
    Result = ''
    Do i = 1 To Count By 6
        Result = Result||,
            SubStr(b64, C2D(B2C("00"SubStr(BinaryString, i, 6))) + 1, 1)
    End
 
Return SubStr(Result, 1, Length(Result) - Length(Pad))||Pad
 
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
wasDOpus4DeselectEntry: procedure   /* Deselect a selected entry.        */
    Parse ARG Item
 
    GetAll '*'
    AllItems = RESULT
    AllItems = Translate(Translate(AllItems, '/', ' '), ' ', '*')
 
    n = Words(AllItems)
 
    Do i = 1 To n
        If Item = Translate(Word(AllItems, i), ' ', '/') Then
            Do
                SelectEntry i - 1 ||' '|| 0 ||' '|| 1
                Return
            End
    End
 
Return