IDE tool: check code on load

Share your advanced PureBasic knowledge/code with the community.
dige
Addict
Addict
Posts: 1404
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

IDE tool: check code on load

Post by dige »

Maybe someone can use it or improve it.

After loading a source code into the IDE, this tool shows when it was last modified and with which compiler.

I recently continued working on a project and didn't realize that I had opened an old version due to a saved file link.
And I would like to know with which compiler the project was last compiled, because I often have to switch the IDE between x86 and x64.

To use it: add the compiled file as a IDE tool. Argument “%FILE” and trigger “Source code loaded”

Code: Select all

; PB IDE Tool - CheckCodeOnLoad
; by Dige 01/2025
; Get the meta info from the code to be loaded

Global G_Color.i = #Green

Procedure.s GetStringfromDate(dtg.i)
  Protected diff.i, result.s
  
  Protected y = 24*3600*365
  Protected m = 24*3600*31
  Protected d = 24*3600
  Protected h = 3600
  Protected i = 60
  
  diff = date() - dtg
  
  If diff > y
    result = Strf(diff / y, 1) + " year"
    
  ElseIf diff > m
    result = Strf(diff / m, 1) + " month"
    
  ElseIf diff > d
    result = Strf(diff / d, 1) + " day"
    
   ElseIf diff > h
     result = Strf(diff / h, 1) + " hour"
     
   ElseIf diff > i
     result = Strf(diff / i, 1) + " min"
  
     
   Else
     result = Str(diff) + "sec"
   EndIf
   
   result = FormatDate("%dd.%mm.%yyyy %hh:%ii:%ss", dtg) + "    [" + result + " ago]"
   
   ProcedureReturn result
EndProcedure  


Procedure.s ParseInInformations(file.s)

  Protected fileid.i, dtg_modify.i, dtg_access.i, dtg_create.i, txt.s, ff, n, i
  Protected NewList code.s()
  
  if FileSize(file) <= 0
    End
  endif
  
  dtg_create = GetFileDate(file, #PB_Date_Created)
  dtg_access = GetFileDate(file, #PB_Date_Accessed)
  dtg_modify = GetFileDate(file, #PB_Date_Modified)
  
  
  fileid = ReadFile(#PB_Any, file, #PB_File_SharedRead|#PB_File_SharedWrite)
  
  If fileid
    
    ff = ReadStringFormat(fileid)
    ; Einlesen aller Zeilen die mit ; beginnen
    While Not Eof(fileid)
      
      txt = ReadString(fileid, ff)
      If Mid(txt, 1, 1) = ";"
        AddElement(code())
        code() = txt
      Endif  
    Wend
    
    
    LastElement(code())
    txt = ""
    n   = ListSize(code())
    ; Rückwärts alle IDE Settings einlesen
    For i = 1 to n
      txt = Mid(code(), 2) + #CRLF$ + txt
      
      If FindString(code(), "; IDE Options = ", 0, #PB_String_NoCase)
        
        If (FindString(code(), "x86", 0, #PB_String_NoCase) And #PB_Compiler_32Bit <> 1) or (FindString(code(), "x64", 0, #PB_String_NoCase) And #PB_Compiler_64Bit <> 1)
          ; IDE Options = PureBasic 6.20 Beta 2 (Windows - x86)
          txt = "⚠ COMPILER CHANGED! ⚠" + #CRLF$ + #CRLF$ + txt
          G_Color = #Red
        EndIf
        i = n + 1
      EndIf
      PreviousElement(code())
    Next
    
    CloseFile(fileid)
  Endif
  
  txt = GetFilePart(file) + #CRLF$ + #CRLF$ +
        "Modify : " + GetStringfromDate (dtg_modify) + #CRLF$ +
        "Create : " + GetStringfromDate (dtg_create) + #CRLF$ +
        "Access : " + GetStringfromDate (dtg_access) + #CRLF$ +
        #CRLF$ + txt
  ProcedureReturn txt
EndProcedure

Procedure UpdateWindow()
  ResizeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
EndProcedure

Define.s file = ProgramParameter()
Define timeout = date() + 5
Define MouseX.i, MouseY.i
Define WindowRect.RECT
Define x, y
Define flags

If ExamineDesktops()
  x = DesktopWidth(0)/2 - 200
  y = DesktopHeight(0) - 280
EndIf

; file = "C:\Temp\x64Test.pb"

OpenWindow(0, x, y, 400, 200, GetFilePart(file), #PB_Window_NoActivate | #PB_Window_SizeGadget | #PB_Window_SystemMenu)

SetWindowColor(0, #Black)
TextGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, ParseInInformations(file))
SetGadgetColor(0, #PB_Gadget_BackColor, #Black)
SetGadgetColor(0, #PB_Gadget_FrontColor, G_Color)
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)


Repeat

  Select WaitWindowEvent(200)
    Case #PB_Event_CloseWindow
      Break
      
    Default
      
      MouseX = DesktopMouseX()
      MouseY = DesktopMouseY()
      
      If GetWindowRect_(WindowID(0), @WindowRect)
        If MouseX >= WindowRect\left And MouseX <= WindowRect\right And MouseY >= WindowRect\top And MouseY <= WindowRect\bottom
          SetWindowTitle(0, file + " [wait]")
          flags = #True
        Else
          flags = #Null
          If timeout < date()
            Break
          EndIf
        EndIf
      EndIf
      
      If not flags
        SetWindowTitle(0, GetFilePart(file) + " [" + Str(date() - timeout) + "]")
      Endif    
  
  EndSelect
ForEver  
"Daddy, I'll run faster, then it is not so far..."
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: IDE tool: check code on load

Post by Axolotl »

Hi dige,
thanks for sharing.
I think it's a good idea to know and make visible what the last compiler used and its setting was.

Not tested, yet.
Some thoughts, about the tool.
1) What about the Option: "Save settings to "
“The end of the source file” is only one option; the information can also be saved elsewhere.
2) Without "GetWindowRect_()" it could be platform-independent (as far as I can see) and you did the "ExamineDesktops()" already.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: IDE tool: check code on load

Post by skywalk »

If you are concerned with version control, definitely run fossil or git on your development PCs.
I rarely need to run more than check-in or check-out commands. And some merges when I try new features that break or improve my app.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply