' Gambas module file

Public Sub GetFileSize(iSize As Long) As String
  
  If iSize < 1000 Then
    Return Subst(("&1 B"), CStr(iSize))
  Else If iSize < 1000000 Then
    Return Subst(("&1 KiB"), Format(iSize / 1024, "0.#"))
  Else If iSize < 1000000000 Then
    Return Subst(("&1 MiB"), Format(iSize / 1048576, "0.#"))
  Else
    Return Subst(("&1 GiB"), Format(iSize / 1073741824, "0.#"))
  Endif
  
End

Public Sub CompressForGmail(sPath As String) As String

  Dim sDest As String
  Dim hFile As File
  Dim sBase64 As String
  Dim I As Integer
  
  sDest = sPath & ".gmail.txt"
  hFile = Open sDest For Output Create 
  Print #hFile, "<salt FCKGM-0001-"; Hex$(Rand(0, 65535), 4); "-"; Hex$(Rand(0, 65535), 4); "-"; Hex$(Rand(0, 65535), 4); ">"
  sBase64 = Base64(File.Load(sPath))
  For I = 1 To Len(sBase64) Step 76
    Print #hFile, Mid$(sBase64, I, 76)
  Next
  Print #hFile, "</salt>"
  Close #hFile
  
  Try Kill sPath
  Return sDest
  
End

Public Sub UncompressForGmail(sPath As String)

  Dim aData As String[]
  Dim sDest As String
  
  aData = Split(File.Load(sPath), "\n")
  If Not aData.Last Then aData.Remove(aData.Max)
  If aData[0] Not Like "<salt FCKGM-0001-*>" Or If aData.Last <> "</salt>" Then Error.Raise(("Incorrect format"))
  aData.Remove(0)
  aData.Remove(aData.Max)
  
  sDest = sPath & ".tmp"
  File.Save(sDest, FromBase64(aData.Join("")))
  Try Kill sPath
  
  If sPath Ends ".gmail.txt" Then
    Move sDest To Left(sPath, -10)
  Else
    Move sDest To sPath
  Endif
  
End

Public Sub TrimCurlyBracket(sStr As String) As String
  
  If sStr Begins "{" And If sStr Ends "}" Then Return Mid$(sStr, 2, -1)
  Return sStr
  
End

Public Sub Main()

  Dim A As Float
  
  Print Chr$(34);
  For A = 0 To 6
    If A > 0 Then Print ";";
    Print CStr(Round(Cos(Pi(2) * A / 6), -10)); ","; CStr(Round(Sin(Pi(2) * A / 6), -10)); 
  Next
  Print Chr$(34)
  
  Print "[";
  For A = 0 To 6
    If A > 0 Then Print ",";
    Print CStr(Round(Cos(Pi(2) * A / 6), -10)); ","; CStr(Round(Sin(Pi(2) * A / 6), -10));
  Next
  Print "]"
  
End

