Trouble with a procedure... anybody wanna help? :)

Just starting out? Need help? Post your questions and find answers here.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Trouble with a procedure... anybody wanna help? :)

Post by Kale »

This code doesn't seem to work properly, the first attachment file is read and correctly encoded then the others all fail somewhere. I've looked at this code for 2 hours now and im stumped. Basically it takes a 'Filename' from the 'ATTACHMENTDETAILS' structure and Base64 encodes it then stores a pointer to the encoded string in the '*Base64String' offset and the length of this string in the 'LengthOfBase64String' offset. But after the first file there seems to be no string at the *Base64String pointer?

Code: Select all

;===========================================================================
;-CONSTANTS
;===========================================================================

#MAXIMUM_ATTACHMENTS = 10

;===========================================================================
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS
;===========================================================================

Structure ATTACHMENTDETAILS
    Filename.s
    Base64String.l
    LengthOfBase64String.l
EndStructure

Structure MAILDETAILS
    RecipientEmailAddress.s
    SenderName.s
    SenderEmailAddress.s
    SMTPServer.s
    Subject.s
    Message.s
    Attachments.ATTACHMENTDETAILS[#MAXIMUM_ATTACHMENTS]
    SendAttachments.b
EndStructure

;===========================================================================
;-PROCEDURES
;===========================================================================

;Encode the attachments
Procedure.b EncodeAttachments(*MailDetails.MAILDETAILS)
    Debug "=============== ENCODING ATTACHMENTS ==============="
    Protected InputBufferLength.l
    Protected OutputBufferLength.l
    For x.l = 0 To #MAXIMUM_ATTACHMENTS - 1
        If *MailDetails\Attachments[x]\Filename <> ""
            If ReadFile(0, *MailDetails\Attachments[x]\Filename)
                InputBufferLength = Lof()
                If AllocateMemory(#MAXIMUM_ATTACHMENTS, InputBufferLength, 0)
                    UseFile(0)
                    ReadData(UseMemory(#MAXIMUM_ATTACHMENTS), InputBufferLength)
                    CloseFile(0)

                    OutputBufferLength = InputBufferLength + InputBufferLength/3 + 2
                    If OutputBufferLength < 64 : OutputBufferLength = 64 : EndIf
                    If AllocateMemory(x, OutputBufferLength, 0)
                        Base64Encoder(UseMemory(#MAXIMUM_ATTACHMENTS), InputBufferLength, UseMemory(x), OutputBufferLength)
                        *MailDetails\Attachments[x]\Base64String = UseMemory(x)
                        
                        *MailDetails\Attachments[x]\LengthOfBase64String = OutputBufferLength
                        Debug "Encoded: " + *MailDetails\Attachments[x]\Filename
                        Debug "Peeked String: " + PeekS(UseMemory(#MAXIMUM_ATTACHMENTS))
                        Debug "*Base64String: " + Str(*MailDetails\Attachments[x]\Base64String)
                        Debug "Base64String: " + PeekS(*MailDetails\Attachments[x]\Base64String, *MailDetails\Attachments[x]\LengthOfBase64String)
                        Debug "Base64Length: " + Str(*MailDetails\Attachments[x]\LengthOfBase64String)
                        Debug ""
                    Else
                        Debug "ERROR: Unable to allocate memory for Base64 encoding of: " + *MailDetails\Attachments[x]\Filename
                        ProcedureReturn 0
                    EndIf
                Else
                    Debug "ERROR: Unable to allocate memory to store original of: " + *MailDetails\Attachments[x]\Filename
                    ProcedureReturn 0
                EndIf
            Else
                Debug "ERROR: Unable to read file: " + *MailDetails\Attachments[x]\Filename
                ProcedureReturn 0
            EndIf
        EndIf
    Next x
    ProcedureReturn 1
EndProcedure

;===========================================================================
;-TESTING
;===========================================================================

Account.MAILDETAILS

; You can use your own files here

Account\Attachments[0]\Filename = "D:\__PureBasic__\mails.ico"
Account\Attachments[1]\Filename = "D:\__PureBasic__\Mercury.gif"
Account\Attachments[2]\Filename = "D:\__PureBasic__\mercury.jpg"
Account\Attachments[3]\Filename = "D:\__PureBasic__\Text.cfg"
Account\SendAttachments = #TRUE

EncodeAttachments(@Account)
Edit: Changed '*Base64String.l' to 'Base64String.l' in the ATTACHMENTDETAILS structure. It still doesn't make any difference though :(
Last edited by Kale on Fri Sep 26, 2003 12:22 pm, edited 1 time in total.
--Kale

Image
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

At a quick glance the "*Base64String.l" in the ATTACHMENTDETAILS structure and it's usage elsewhere looks a bit iffy. Eg, further on you have...

*MailDetails\Attachments[x]\Base64String

...which suggests you may have intended just "Base64String.l" in your structure.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

yeah but if i use:

Code: Select all

*MailDetails\Attachments[x]\*Base64String
i get a syntax error, this:

Code: Select all

*MailDetails\Attachments[x]\Base64String
seems to work ok, regardless if i change the *Base64String.l to Base64String.l in the ATTACHMENTDETAILS structure. Is this a bug?
--Kale

Image
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Works OK for me. Here is the code with my bit of debuging dropped on the end. I haven't changed your function or structures at all.

Code: Select all

;=========================================================================== 
;-CONSTANTS 
;=========================================================================== 

#MAXIMUM_ATTACHMENTS = 10 

;=========================================================================== 
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS 
;=========================================================================== 

Structure ATTACHMENTDETAILS 
    Filename.s 
    Base64String.l 
    LengthOfBase64String.l 
EndStructure 

Structure MAILDETAILS 
    RecipientEmailAddress.s 
    SenderName.s 
    SenderEmailAddress.s 
    SMTPServer.s 
    Subject.s 
    Message.s 
    Attachments.ATTACHMENTDETAILS[#MAXIMUM_ATTACHMENTS] 
    SendAttachments.b 
EndStructure 

;=========================================================================== 
;-PROCEDURES 
;=========================================================================== 

;Encode the attachments 
Procedure.b EncodeAttachments(*MailDetails.MAILDETAILS) 
    Debug "=============== ENCODING ATTACHMENTS ===============" 
    Protected InputBufferLength.l 
    Protected OutputBufferLength.l 
    For x.l = 0 To #MAXIMUM_ATTACHMENTS - 1 
        If *MailDetails\Attachments[x]\Filename <> "" 
            If ReadFile(0, *MailDetails\Attachments[x]\Filename) 
                InputBufferLength = Lof() 
                                If AllocateMemory(#MAXIMUM_ATTACHMENTS, InputBufferLength, 0) 
                    UseFile(0) 
                    ReadData(UseMemory(#MAXIMUM_ATTACHMENTS), InputBufferLength) 
                    CloseFile(0) 
;
                    OutputBufferLength = InputBufferLength + InputBufferLength/3 + 2 
                    If OutputBufferLength < 64 : OutputBufferLength = 64 : EndIf 
                    If AllocateMemory(x, OutputBufferLength, 0) 
                        Base64Encoder(UseMemory(#MAXIMUM_ATTACHMENTS), InputBufferLength, UseMemory(x), OutputBufferLength) 
                        *MailDetails\Attachments[x]\Base64String = UseMemory(x) 
                        
                        *MailDetails\Attachments[x]\LengthOfBase64String = OutputBufferLength 
                        Debug "Encoded: " + *MailDetails\Attachments[x]\Filename 
                        Debug "Peeked String: " + PeekS(UseMemory(#MAXIMUM_ATTACHMENTS)) 
                        Debug "*Base64String: " + Str(*MailDetails\Attachments[x]\Base64String) 
                        Debug "Base64String: " + PeekS(*MailDetails\Attachments[x]\Base64String, *MailDetails\Attachments[x]\LengthOfBase64String) 
                        Debug "Base64Length: " + Str(*MailDetails\Attachments[x]\LengthOfBase64String) 
                        Debug "" 
                    Else 
                        Debug "ERROR: Unable to allocate memory for Base64 encoding of: " + *MailDetails\Attachments[x]\Filename 
                        ProcedureReturn 0 
                    EndIf 
                Else 
                    Debug "ERROR: Unable to allocate memory to store original of: " + *MailDetails\Attachments[x]\Filename 
                    ProcedureReturn 0 
                EndIf 
            Else 
                Debug "ERROR: Unable to read file: " + *MailDetails\Attachments[x]\Filename 
                ProcedureReturn 0 
            EndIf 
        EndIf 
    Next x 
    ProcedureReturn 1 
EndProcedure 

;=========================================================================== 
;-TESTING 
;=========================================================================== 

Account.MAILDETAILS 

; You can use your own files here 

Account\Attachments[0]\Filename = "D:\gedbs\PureBasic\Examples\Sources\Font.pb" 
Account\Attachments[1]\Filename = "D:\gedbs\PureBasic\Examples\Sources\File.pb" 
Account\Attachments[2]\Filename = "D:\ListParam.exe" 
Account\SendAttachments = #TRUE 

EncodeAttachments(@Account) 

Procedure DebugAttachment(*Attachment.ATTACHMENTDETAILS)
  fn.s = *Attachment\Filename
  ps = *Attachment\Base64String
  sl = *Attachment\LengthOfBase64String
    Debug "Filename: " + fn
    Debug "Base64String: "  + Str(ps)
    Debug "LengthOfBase64String: " + Str(sl)
    Debug "Encoded String: " + PeekS(ps, sl)
    Debug "------------------------------------------------"
EndProcedure

DebugAttachment(@Account\Attachments[0])
DebugAttachment(@Account\Attachments[1])
DebugAttachment(@Account\Attachments[2])
Post Reply