EnableExplicit

LibBuild_Include(MemAlloc_FromString)

#GetOpenSavePatcher_DataSize = 32760
#GetOpenSavePatcher_DataSizeWB = #GetOpenSavePatcher_DataSize << 1

Procedure GetOpenSavePatcher_Patch(*OFNR.OPENFILENAME, hWnd.i, Filename.s, Patterns.s)
  Define.i xLong1
  Define.i xLong2
  Define.i xLong3
  
  If *OFNR
    With *OFNR
      \lCustData = 0
      If Right(Patterns, 1) = "|"
        Patterns = Left(Patterns, Len(Patterns) - 1)
        EndIf
      xLong3 = Len(Patterns)
      If xLong3
        xLong3 + 4 ; len + ||| + 0
        CompilerIf #PB_Compiler_Unicode
          xLong2 = MemAllocNC(xLong3 << 1)
          If xLong2
            \lpstrFilter = xLong2
            PokeS(xLong2, Patterns + "|||")
            xLong3 - 1
            For xLong1 = 0 To xLong3
              If PeekW(xLong2) = 124
                PokeW(xLong2, 0)
                EndIf
              xLong2 + 2
              Next
            ;\lCustData = xLong3 - 1
            EndIf
          CompilerElse
          xLong2 = MemAllocNC(xLong3)
          If xLong2
            \lpstrFilter = xLong2
            PokeS(xLong2, Patterns + "||")
            xLong3 - 1
            For xLong1 = 0 To xLong3
              If PeekB(xLong2) = 124
                PokeB(xLong2, 0)
                EndIf
              xLong2 + 1
              Next
            ;\lCustData = xLong3 - 1
            EndIf
          CompilerEndIf
        EndIf
      \hWndOwner = hWnd
      \nMaxFile = #GetOpenSavePatcher_DataSize
      \nMaxFileTitle = #GetOpenSavePatcher_DataSize
      \lStructSize = SizeOf(OPENFILENAME)
      \hInstance = hInstance
      CompilerIf #PB_Compiler_Unicode
        \lpstrFile = MemAllocNC(#GetOpenSavePatcher_DataSizeWB)
        \lpstrFileTitle = MemAllocNC(#GetOpenSavePatcher_DataSizeWB)
        CompilerElse
        \lpstrFile = MemAllocNC(#GetOpenSavePatcher_DataSize)
        \lpstrFileTitle = MemAllocNC(#GetOpenSavePatcher_DataSize)
        CompilerEndIf
      
      \lpstrInitialDir = 0
      If \lpstrFileTitle
        CompilerIf #PB_Compiler_Unicode
          PokeW(\lpstrFileTitle, 0)
          CompilerElse
          PokeB(\lpstrFileTitle, 0)
          CompilerEndIf
        EndIf
      If \lpstrFile
        CompilerIf #PB_Compiler_Unicode
          PokeW(\lpstrFile, 0)
          CompilerElse
          PokeB(\lpstrFile, 0)
          CompilerEndIf
        EndIf
      If Right(Filename, 1) = "\"
        Filename = Left(Filename, Len(Filename) - 1)
        EndIf
      If Len(Filename)
        xLong1 = GetFileAttributes_(Filename)
        ;;;If xLong1 & $10 And Not xLong1 = -1
        If ((xLong1 & $7fffffff) = $7fffffff) Or ((xLong1 & $10) = 0)
          xLong1 = InStrRevChar(Filename, 92) ; "\"
          If xLong1 And xLong1 > 3
            PokeS(\lpstrFile, Mid(Filename, xLong1 + 1))
            Filename = Left(Filename, xLong1 - 1)
            xLong1 = GetFileAttributes_(Filename)
            ;If xLong1 & $10 And Not xLong1 = -1
            If ((xLong1 & $7fffffff) <> $7fffffff) And (xLong1 & $10)
              \lpstrInitialDir = MemAlloc_FromString(Filename, Len(Filename))
              EndIf
            EndIf
          Else
          \lpstrInitialDir = MemAlloc_FromString(Filename, Len(Filename))
          EndIf
        EndIf
      EndWith
    EndIf
  EndProcedure

Procedure GetOpenSavePatcher_Free(*OFNR.OPENFILENAME)
  If *OFNR
    With *OFNR
      If \lpstrFilter
        MemFree(\lpstrFilter)
        \lpstrFilter = 0
        EndIf
      If \lpstrFile
        MemFree(\lpstrFile)
        \lpstrFile = 0
        EndIf
      If \lpstrFileTitle
        MemFree(\lpstrFileTitle)
        \lpstrFileTitle = 0
        EndIf
      If \lpstrInitialDir
        MemFree(\lpstrInitialDir)
        \lpstrInitialDir = 0
        EndIf
      EndWith
    EndIf
  EndProcedure

; IDE Options = PureBasic 5.42 LTS (Windows - x64)
; CursorPosition = 85
; FirstLine = 73
; Folding = --
; EnableUnicode
; EnableXP
; CompileSourceDirectory