' Gambas module file

' Private $hFadeN As Picture
' ' Private $hFadeW As Picture
' ' Private $hFadeS As Picture
' ' Private $hFadeE As Picture
' 
' Private Sub LoadFades()
'   
'   Dim hImage As Image
'   'Dim Y As Integer
'   Dim iColor As Integer
'   
'   iColor = Color.Desaturate(Color.TextForeground)
'   
'   'If iColor = $iFadeColor Then Return
'   
'   hImage = New Image(32, 8, Color.Transparent)
'   ' For Y = 0 To 7
'   '   hImage.FillRect(0, Y, hImage.W, 1, Color.SetAlpha(iColor, 160 + Y * 12))
'   ' Next
'   
'   $hFadeN = hImage.Picture
'   $hFadeN = hImage.Picture
'   ' hImage.RotateLeft
'   ' $hFadeW = hImage.Picture
'   ' hImage.RotateLeft
'   ' $hFadeS = hImage.Picture
'   ' hImage.RotateLeft
'   ' $hFadeE = hImage.Picture
'   
' End

Public Sub Main1()

  Dim aHtml As String[]
  Dim nCar As Integer
  Dim I As Integer

  aHtml = SplitHTML("&#x27;V&#x27; - <b><i>À écouter</i> jusqu&#x27;à la fin</b> et oups il dit quoi l&#x27;astronaute...🤔-1564611614275518478.mp4", ByRef nCar)
  
  Print nCar
  
  For I = 1 To nCar
    Print I;; HtmlLeft(aHtml, I)
  Next
  
End

Public Sub Main()

  Dim hImage As Image

  hImage = New Image(32, 32)
  Paint.Begin(hImage)
  Print Paint.ResolutionX;; Paint.ResolutionY
  Paint.End

End


Public Sub GetFileSize(iSize As Long, Optional nDigit As Integer = 1) As String

  Dim sFormat As String
  
  sFormat = "0." & String$(nDigit, "#")
  
  If iSize < 1024 Then
    Return Subst(("&1 B"), CStr(iSize))
  Else If iSize < 1048576 Then
    Return Subst(("&1 KiB"), Format(iSize / 1024, sFormat))
  Else If iSize < 1073741824 Then
    Return Subst(("&1 MiB"), Format(iSize / 1048576, sFormat))
  Else
    Return Subst(("&1 GiB"), Format(iSize / 1073741824, sFormat))
  Endif
  
End

Public Sub HtmlLeft(aHtml As String[], iPos As Integer) As String

  Dim I As Integer
  Dim sElt As String
  Dim sHtml As String
  Dim iLen As Integer

  If iPos = 0 Then Return

  For I = 0 To aHtml.Max
    sElt = aHtml[I]
    If sElt Begins "<" Then
      sHtml &= sElt
    Else If iPos > 0 Then
      If sElt Begins "&" Then
        sHtml &= sElt
        Dec iPos
      Else
        iLen = String.Len(sElt)
        If iLen <= iPos Then
          sHtml &= sElt
          iPos -= iLen
        Else
          sHtml &= String.Left(sElt, iPos)
          iPos = 0
        Endif
      Endif
    Endif
  Next
  
  Return sHtml

End

Public Sub SplitHTML(Html As String, ByRef Count As Integer) As String[]

  Dim P As Integer
  Dim iLen As Integer
  Dim C As String
  Dim sResult As String
  Dim C2 As String
  Dim aResult As String[]
  
  aResult = New String[]
  Count = 0
  iLen = String.Len(Html)
  
READ_TEXT:
  
  GoSub LOOK_CHAR
  
  If C = "<" Then Goto READ_MARKUP
  If C = "&" Then Goto READ_ENTITY
  
  GoSub READ_CHAR
  Goto READ_TEXT
  
READ_MARKUP:

  GoSub ADD_RESULT
  GoSub READ_CHAR
  
READ_MARKUP_NAME:

  GoSub READ_CHAR
  
  If C = " " Then Goto READ_MARKUP_REST
  If C = ">" Then Goto READ_MARKUP_END
  
  Goto READ_MARKUP_NAME
  
READ_MARKUP_REST:

  GoSub READ_CHAR
  
  If C = ">" Then Goto READ_MARKUP_END
  
  If C = Chr$(34) Or If C = "'" Then 
    C2 = C
    Goto READ_STRING
  Endif
  
  Goto READ_MARKUP_REST

READ_MARKUP_END:

  GoSub ADD_RESULT
  Goto READ_TEXT

READ_STRING:

  GoSub READ_CHAR
  
  If C = C2 Then Goto READ_MARKUP_REST
  
  Goto READ_STRING

READ_ENTITY:

  GoSub ADD_RESULT
  GoSub READ_CHAR

  Do
    GoSub READ_CHAR
    If Not IsDigit(C) And If Not IsLetter(C) And If C <> "#" Then Break
  Loop
  
  GoSub ADD_RESULT
  Goto READ_TEXT
  
LOOK_CHAR:

  If P >= iLen Then 
    C = ""
  Else
    C = String.Mid$(Html, P + 1, 1)
  Endif
  Return
  
READ_CHAR:

  Inc P
  If P > iLen Then
    GoSub ADD_RESULT
    Return aResult
  Endif
  C = String.Mid$(Html, P, 1)
  sResult &= C
  Return
  
ADD_RESULT:

  If sResult Then
    aResult.Add(sResult)
    If sResult Begins "&" Then
      Inc Count
    Else If sResult Not Begins "<" Then
      Count += String.Len(sResult)
    Endif
    sResult = ""
  Endif
  Return
  
End

' Public Sub URLQuote(Path As String) As String
'   
'   Dim iInd As Integer
'   Dim sRes As String
'   Dim sCar As String
'   
'   For iInd = 1 To Len(Path)
'     sCar = Mid$(Path, iInd, 1)
'     If sCar = " " Then 
'       sCar = "+"
'     Else If IsLetter(sCar) Or If IsDigit(sCar) Or If InStr("-._~,$!/", sCar) Then 
'     Else 
'       sCar = "%" & Hex$(Asc(sCar), 2)
'     Endif
'     sRes &= sCar
'   Next
' 
'   Return sRes
'   
' End
' 
' Public Sub URLUnquote(Path As String, Optional DoNotDecodePlus As Boolean) As String
'   
'   Dim iInd As Integer
'   Dim sRes As String
'   Dim sCar As String
'   
'   For iInd = 1 To Len(Path)
'     sCar = Mid$(Path, iInd, 1)
'     If sCar = "%" Then 
'       sCar = Chr$(Val("&H" & Mid$(Path, iInd + 1, 2)))
'       iInd += 2
'     Else If sCar = "+" And If Not DoNotDecodePlus Then 
'       sCar = " "
'     Endif
'     sRes &= sCar
'   Next
' 
'   Return sRes  
'   
' End

Public Sub PathToUrlList(aPath As String[]) As String

  Dim I As Integer
  
  aPath = aPath.Copy()
  For I = 0 To aPath.Max
    If aPath[I] Begins "/" Then aPath[I] = "file://" & Url(aPath[I])
  Next
  Return aPath.Join(gb.CrLf) & gb.CrLf
  
End

Public Sub UrlListToPath(sData As String) As String[]
  
  Dim sUri As String
  Dim aPaths As String[]
  
  aPaths = New String[]
  For Each sUri In Split(Replace(sData, "\n", ""), "\r", "", True)
    If sUri Begins "file://" Then
      aPaths.Add(FromUrl(Mid$(sUri, 8)))
    Else 
      aPaths.Add(sUri)
    Endif
  Next
  
  Return aPaths
  
End

Public Sub FindItem(hView As GridView, sFind As String, aText As String[])
  
  Dim I As Integer

  If Not sFind Then 
    hView.Row = -1
    Return
  Endif
  
  sFind = String.LCase(sFind)

  For I = 0 To aText.Max
    If String.LCase(aText[I]) Begins sFind Then
      hView.Rows[I].EnsureVisible
      hView.Row = I
      Break
    Endif
  Next
  
End

Public Sub MkDir(Path As String)

  Dim sElt As String
  Dim sMake As String = "/"

  If Path Begins "~/" Then Path = User.Home &/ Mid$(Path, 3)

  For Each sElt In Split(Path, "/")
    sMake &/= sElt
    If IsDir(sMake) Then Continue
    Try Mkdir sMake
  Next
  
  If Not Exist(Path) Or If Not IsDir(Path) Then Error.Raise("Cannot create directory")

End

Public Sub RmDir(Path As String, Optional Force As Boolean)

  Dim sFile As String
  Dim aDir As String[]
  
  If Len(Path) > 1 And If Right(Path) = "/" Then Path = Left(Path, -1)
  If Not Exist(Path) Then Return
  
  If Not Force Then
    If Path = "~" Or If Path = User.Home Or If Path = "/" Then Error.Raise("Removing this directory recursively is a bad idea: " & Path)
  Endif
  
  Try aDir = Dir(Path)
  If aDir Then
    For Each sFile In aDir
      If IsDir(Path &/ sFile) Then
        RmDir(Path &/ sFile)
      Else
        Try Kill Path &/ sFile
      Endif
    Next
  Endif
  
  Rmdir Path
  
End
