' Gambas module file

Public Enum ENC_QUOTED, ENC_BASE64, ENC_7BITS

Public Sub Format(iEncoding As Integer) As String
  
  Select Case iEncoding
    Case ENC_QUOTED
      Return "quoted-printable"
    Case ENC_7BITS
      Return "7bit"
    Case ENC_BASE64
      Return "base64"
  End Select
  
End

Public Sub PrintQuoted(hStream As Stream, sStr As String, bDebug As Boolean)

  Dim I As Integer
  Dim W As Integer
  Dim sCar As String
  Dim sLastCar As String

  For I = 1 To Len(sStr)
    
    sLastCar = sCar
    sCar = Mid$(sStr, I, 1)
    
    Select Case Asc(sCar)
      
      Case 9, 32
        If W = 0 Then 
          Print #hStream, "="; Hex$(Asc(sCar), 2);
          If bDebug Then Error "="; Hex$(Asc(sCar), 2);
          W += 3
        Else
          Print #hStream, sCar;
          If bDebug Then Error sCar;
          Inc W
        Endif
        
      Case 61
        Print #hStream, "=3D";
        If bDebug Then Error "=3D";
        W += 3
        
      ' Case 10 'newline
      '   
      '   If I > 1 And If Asc(sLastCar) <= 32 Then 
      '     Print #hStream, "="
      '     If bDebug Then Error "="
      '   Endif
      '   Print #hStream
      '   If bDebug Then Error
      '   W = 0
        
      Default
        
        If Asc(sCar) < 33 Or If Asc(sCar) > 126 Then
          Print #hStream, "="; Hex$(Asc(sCar), 2);
          If bDebug Then Error "="; Hex$(Asc(sCar), 2);
          W += 3
        Else If sCar = "." And If W = 0 Then
          Print #hStream, "=2E";
          If bDebug Then Error "=2E";
          W += 3
        Else
          Print #hStream, sCar;
          If bDebug Then Error sCar;
          Inc W
        Endif
      
    End Select
    
    If W >= 72 Then
      Print #hStream, "="
      If bDebug Then Error "="
      W = 0
    Endif
    
  Next

  Print #hStream
  If bDebug Then Error

  Flush #hStream

End

Public Sub PrintQuotedHeader(hStream As Stream, sHeader As String, sStr As String, bAddress As Boolean, bDebug As Boolean)

  Dim I As Integer
  Dim sCar As String
  'Dim J As Integer
  'Dim sEncode As String
  Dim W As Integer
  Dim bEncode As Boolean
  Dim bAddressMode As Boolean
  Dim bEncoded As Boolean
  

  ' Try sStr = Conv(sStr, "utf-8", "iso-8859-1")
  ' If Not Error Then
  '   sHeader &= ": =?iso-8859-1?q?"
  ' Else
  ' sHeader &= ": =?utf-8?q?"
  ' Endif
  
  sHeader &= ": "
  Print #hStream, sHeader;
  If bDebug Then Error sHeader;
  W = Len(sHeader)

  For I = 1 To Len(sStr)
  
    If bAddress Then
      If Mid$(sStr, I, 2) = " <" Then 
        GoSub CLOSE_ENCODED
        bAddressMode = True
      Endif
    Endif
    
    sCar = Mid$(sStr, I, 1)
    
    If bAddressMode And If IsAscii(sCar) Then
      
      If (W + Len(sCar)) > 72 Then
        Print #hStream, "="
        If bDebug Then Error "="
        Print #hStream, " ";
        If bDebug Then Error " ";
        W = 1
      Endif
      
    Else
      
      bEncode = False
      If sCar = " " Then
        sCar = "_"
      Else If Asc(sCar) < 33 Or If Asc(sCar) > 126 Or If sCar = "_" Or If sCar = "=" Or If sCar = "?" Then
        bEncode = True
      Endif
      
      If bEncode Then sCar = "=" & Hex$(Asc(sCar), 2)
      
      If (W + Len(sCar)) >= 62 Then
        GoSub CLOSE_ENCODED
        Print #hStream
        Print #hStream, " ";
        If bDebug Then 
          Error
          Error " ";
        Endif
        W = 1
      Endif
      
      If Not bEncoded Then
        bEncoded = True
        Print #hStream, "=?utf-8?q?";
        If bDebug Then Error "=?utf-8?q?";
        W += Len("=?utf-8?q?")
      Endif

    Endif
    
    Print #hStream, sCar;
    If bDebug Then Error sCar;
    W += Len(sCar)
    
    If bAddress Then
      If I > 1 And If Mid$(sStr, I - 1, 2) = ">," Then bAddressMode = False
    Endif
    
  Next
  
  If bEncoded Then
    Print #hStream, "?="
    If bDebug Then Error "?="
  Else
    Print #hStream
    If bDebug Then Error 
  Endif
  
  Flush #hStream
  Return
  
CLOSE_ENCODED:

  If bEncoded Then
    Print #hStream, "?=";
    If bDebug Then Error "?=";
    W += Len("?=")
    bEncoded = False
  Endif
  Return

End


Public Sub PrintBase64(hStream As Stream, sStr As String, bDebug As Boolean)

  Dim I As Integer
  
  For I = 1 To Len(sStr) Step 54
    Print #hStream, Base64(Mid$(sStr, I, 54))
    If bDebug Then Error Base64(Mid$(sStr, I, 54))
  Next
  
End
