My Workaround Collection

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

My Workaround Collection

Post by mk-soft »

Purebasic is my favorite development environment and I also create professional applications and tools.

Even if Purebasic has few bugs, they do occur. For these, however, there is usually a simple solution to fix until the next release via a workaround.

I cleaned up and summarized my workaround, because I can't find most of them myself.
Furthermore, the workaround are rewritten as modules, so that they can be used more easily in the module

This workaround should always be cross-platform and easy to extend.
Last edited by mk-soft on Fri Aug 24, 2018 12:34 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: My Workaround Collection

Post by mk-soft »

Workaround-560.pb

Code: Select all

;-TOP

; Workaround PB v5.60
; Version : v1.0
; Create  : 24.08.2018
; Update  : 

; -----------------------------------------------------------------------------

;- Global

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    ;TODO
    
  CompilerCase #PB_OS_Windows
    ;TODO
    
  CompilerCase #PB_OS_Linux
    ; Ubuntu Compiler bug
    ImportC "-no-pie" : EndImport
    
CompilerEndSelect

; -----------------------------------------------------------------------------

;- Module

DeclareModule __Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Declare FixResizeGadget(Gadget, x, y, width, height)
      Declare FixSetGadgetFont(Gadget, FontID)
      Declare FixAddGadgetItem(Gadget, Position, Text.s, Image=0, Flags=0)
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      Declare FixWriteData(file, *adress, length)
      
  CompilerEndSelect
  
EndDeclareModule

Module __Workaround
  
  EnableExplicit
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_MacOS
      
      ; Link https://www.purebasic.fr/english/viewtopic.php?f=24&t=71269
      ; Canvas Gadget Container Bugfix
      Procedure FixResizeGadget(Gadget, x, y, width, height)
        ResizeGadget(Gadget, x, y, width, height)
        Protected rect.NSRect, sv, container
        If GadgetType(Gadget) <> #PB_GadgetType_Canvas
          ProcedureReturn 1
        EndIf
        sv = CocoaMessage(0, GadgetID(Gadget), "subviews")
        If CocoaMessage(0, sv, "count")
          container = CocoaMessage(0, sv, "objectAtIndex:", 0)
          If container
            CocoaMessage(@rect, GadgetID(Gadget), "frame")
            rect\origin\x = 0
            rect\origin\y = 0
            CocoaMessage(0, container, "setFrame:@", @rect)
          EndIf
        EndIf
      EndProcedure
      
      ; Link http://www.purebasic.fr/english/viewtopic.php?f=24&t=68571
      Procedure FixSetGadgetFont(Gadget, FontID)
        SetGadgetFont(Gadget, #PB_Default)
        SetGadgetFont(Gadget, FontID)
      EndProcedure
      
      ; Link http://www.purebasic.fr/english/viewtopic.php?f=24&t=68494
      Procedure FixAddGadgetItem(Gadget, Position, Text.s, Image=0, Flags=0)
        Protected font
        AddGadgetItem(Gadget, Position, Text, Image, Flags)
        If GadgetType(Gadget) = #PB_GadgetType_Editor
          font = GetGadgetFont(Gadget)
          If font
            SetGadgetFont(Gadget, font)
          EndIf
        EndIf
      EndProcedure
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      
      ; Link http://www.purebasic.fr/english/viewtopic.php?f=23&t=68630
      Procedure FixWriteData(file, *adress, length) ; Workaround for Linux PB560
        Protected write_data_pointer=Loc(file)
        Protected writen_data=WriteData(file, *adress, length)
        FileSeek(file, write_data_pointer+writen_data)
        ProcedureReturn writen_data
      EndProcedure
      
  CompilerEndSelect
  
EndModule

; -----------------------------------------------------------------------------

;- Macro

DeclareModule Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Macro ResizeGadget(Gadget, x, y, width, height)
        __Workaround::FixResizeGadget(Gadget, x, y, width, height)
      EndMacro
      
      Macro SetGadgetFont(Gadget, FontID)
        __Workaround::FixSetGadgetFont(Gadget, FontID)
      EndMacro
      
      Macro AddGadgetItem(Gadget, Position, Text, Image=0, Flags=0)
        __Workaround::FixAddGadgetItem(Gadget, Position, Text, Image, Flags)
      EndMacro
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      Macro WriteData(File, Buffer, Size)
        __Workaround::FixWriteData(File, Buffer, Size)
      EndMacro
      
  CompilerEndSelect
EndDeclareModule

Module Workaround
EndModule

; -----------------------------------------------------------------------------
[/size]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: My Workaround Collection

Post by mk-soft »

Workaround-562.pb

Code: Select all

;-TOP

; Workaround PB v5.62
; Version : v1.0
; Create  : 24.08.2018
; Update  : 

; -----------------------------------------------------------------------------

;- Global

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    ;TODO
    
  CompilerCase #PB_OS_Windows
    ;TODO
    
  CompilerCase #PB_OS_Linux
    ; Ubuntu compiler bug
    ImportC "-no-pie" : EndImport
    
CompilerEndSelect

; -----------------------------------------------------------------------------

;- Module

DeclareModule __Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Declare FixResizeGadget(Gadget, x, y, width, height)
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      ;TODO
  CompilerEndSelect
  
EndDeclareModule

Module __Workaround
  
  EnableExplicit
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_MacOS
      
      ; Link https://www.purebasic.fr/english/viewtopic.php?f=24&t=71269
      ; Canvas Gadget Container Bugfix
      Procedure FixResizeGadget(Gadget, x, y, width, height)
        ResizeGadget(Gadget, x, y, width, height)
        Protected rect.NSRect, sv, container
        If GadgetType(Gadget) <> #PB_GadgetType_Canvas
          ProcedureReturn 1
        EndIf
        sv = CocoaMessage(0, GadgetID(Gadget), "subviews")
        If CocoaMessage(0, sv, "count")
          container = CocoaMessage(0, sv, "objectAtIndex:", 0)
          If container
            CocoaMessage(@rect, GadgetID(Gadget), "frame")
            rect\origin\x = 0
            rect\origin\y = 0
            CocoaMessage(0, container, "setFrame:@", @rect)
          EndIf
        EndIf
      EndProcedure
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      ;TODO
  CompilerEndSelect
  
EndModule

; -----------------------------------------------------------------------------

;- Macro

DeclareModule Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Macro ResizeGadget(Gadget, x, y, width, height)
        __Workaround::FixResizeGadget(Gadget, x, y, width, height)
      EndMacro
    CompilerCase #PB_OS_Windows
      ;TODO
    CompilerCase #PB_OS_Linux
      ;TODO
  CompilerEndSelect
EndDeclareModule

Module Workaround
EndModule

; -----------------------------------------------------------------------------
[/size]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: My Workaround Collection

Post by mk-soft »

Base for extended workaround

Workaround-Empty.pb

Code: Select all

;-TOP

; Workaround PB Empty
; Version : v1.0
; Create  : 24.08.2018
; Update  : 

; -----------------------------------------------------------------------------

;- Global

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    ;TODO
    
  CompilerCase #PB_OS_Windows
    ;TODO
    
  CompilerCase #PB_OS_Linux
    ;TODO
    
CompilerEndSelect

; -----------------------------------------------------------------------------

;- Module

DeclareModule __Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      ;TODO
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      ;TODO
  CompilerEndSelect
  
EndDeclareModule

Module __Workaround
  
  EnableExplicit
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_MacOS
      ;TODO
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      ;TODO
  CompilerEndSelect
  
EndModule

; -----------------------------------------------------------------------------

;- Macro

DeclareModule Workaround
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      ;TODO
      
    CompilerCase #PB_OS_Windows
      ;TODO
      
    CompilerCase #PB_OS_Linux
      ;TODO
      
  CompilerEndSelect
EndDeclareModule

Module Workaround
EndModule

; -----------------------------------------------------------------------------
[/size]
Last edited by mk-soft on Fri Aug 24, 2018 12:55 pm, edited 3 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: My Workaround Collection

Post by RSBasic »

Good idea Image

At home I also have a few workarounds.

1 workaround for FTP text conversion:

Code: Select all

Procedure.s AsciiToUTF8(String$)
  Protected Buffer$
 
  Buffer$ = Space(Len(String$))
  PokeS(@Buffer$, String$, -1, #PB_Ascii)
  String$ = PeekS(@Buffer$, -1, #PB_UTF8)
 
  ProcedureReturn String$
EndProcedure
Or with my library: viewtopic.php?f=27&t=70380
Image
Image
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: My Workaround Collection

Post by Mijikai »

Thanks this is useful :)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: My Workaround Collection

Post by mk-soft »

Thanks,

My goal is to have a workaround that can always be changed with the next release of purebasic without changing the other code.

I've dropped an empty workaround so that I can add tested code more easily,
or use a separate workaround for certain libraries if needed. For example WorkaroundFTP.pb
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: My Workaround Collection

Post by Sicro »

You could additionally check the constant #PB_Compiler_Version, so that the correct workarounds are used for each PB version:

Code: Select all

CompilerSelect #PB_Compiler_Version
  CompilerCase 560 : XIncludeFile "Workarounds-560.pbi"
  CompilerCase 562 : XIncludeFile "Workarounds-562.pbi"
  Default          : XIncludeFile "Workarounds-All.pbi"
CompilerEndSelect
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: My Workaround Collection

Post by RSBasic »

Workaround for FileSize():
FileSize() (FindFirstFile_()) in Windows supports wildcards. :?
But I don't want that, because I want to know exactly with the given file name whether the file with the exact file name exists.
Solution code:

Code: Select all

Procedure FileSizeEx(FileName$)
  Protected p
  
  FileName$ = ReplaceString(FileName$, "*", "_")
  FileName$ = ReplaceString(FileName$, "?", "_")
  FileName$ = ReplaceString(FileName$, "<", "_")
  FileName$ = ReplaceString(FileName$, ">", "_")
  FileName$ = ReplaceString(FileName$, Chr(34), "_")
  
  If FileSize(FileName$) <> -2
    p = Len(FileName$)
    While p >= 1 And FindString("/\", Mid(FileName$, p, 1)) <> 0
      p-1
    Wend
    If Len(FileName$) <> Len(Left(FileName$, p))
      FileName$ + "_"
    EndIf
  EndIf
  
  ProcedureReturn FileSize(FileName$)
EndProcedure

Macro FileSize(FileName)
  FileSizeEx(FileName)
EndMacro
Image
Image
Post Reply