Include file tool for IDE

Share your advanced PureBasic knowledge/code with the community.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 751
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Include file tool for IDE

Post by spikey »

I wrote this tool to automate the process of creating include directives in projects for my ever growing library of bits and pieces and, seeing as there was some discussion on this subject in another thread, I thought I would share it.

For source code (*.pb or *.pbi) it creates an IncludePath directive followed by an IncludeFile directive for each selected file, like this:-

IncludePath "C:\PureBasic Projects\Common\"
IncludeFile "cmnDate.pbi"
IncludeFile "cmnCursor.pbi"


For image files (*.bmp, *.ico, *.jpg, *.png, *.tga, *.tif) it creates a Use...ImageDecoder section, if needed; an Enumeration section; a CatchImage section and a labelled Data/IncludeBinary section, like this:-

UsePNGImageDecoder()

Enumeration
#Imgfolder_tablepng
#ImgFolderCbmp
EndEnumeration

CatchImage(#Imgfolder_tablepng, ?Image_folder_tablepng)
CatchImage(#ImgFolderCbmp, ?Image_FolderCbmp)

IncludePath "C:\PureBasic Projects\Graphics\"
DataSection
Image_folder_tablepng:
IncludeBinary "folder_table.png"
Image_FolderCbmp:
IncludeBinary "FolderC.bmp"
EndDataSection


I hope you find it useful, :)

Spikey.

Code: Select all

;- Version Control
; 1.00.00 18/09/2010 Created.
; 1.00.01 01/10/2010 Added Catch and Enumeration sections for images.

;- Compiler Directives
EnableExplicit

;- Variables
NewList lstFileList.S() 
Define.B bytFlagFile, bytFlagBin, bytFlagDecoder, bytFlagJPEG, bytFlagPNG, bytFlagTIFF, bytFlagTGA
Define.L lngPattern, lngPBWindow, lngPBProcess, lngScintilla, lngResult, lngLength
Define.S strFullPath, strFileName, strPattern, strExtension, strPath, strText, strFileLabel
Define.S strIncFile, strIncBin, strEnumBin, strCatchBin, strUseBin
Define.S strPBWindow, strPBProcess, strScintilla, strMessage
Define *strText

;- Implementation
lngResult = ExamineEnvironmentVariables()

; Obtain the handle to the IDE window.
strPBWindow = GetEnvironmentVariable("PB_Tool_MainWindow")
lngPBWindow = Val(strPBWindow)

; Obtain the handle to scintilla control.
strScintilla = GetEnvironmentVariable("PB_TOOL_Scintilla")

If strScintilla 
  ; Got it.
  lngScintilla = Val(strScintilla)
  
Else
  ; Didn't get it.
  MessageRequester("Include File Error", "Couldn't obtain a handle to the editor.", #PB_MessageRequester_Ok)  
  End 1
  
EndIf

; Obtain a handle to the owner process.
lngResult = GetWindowThreadProcessId_(lngScintilla, @lngPBProcess)

If lngResult
  
  lngPBProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, lngPBProcess)
  
  If lngPBProcess = #Null
    ; Couldn't access the process.
    MessageRequester("Include File Error", "Couldn't access the editor process.", #PB_MessageRequester_Ok)  
    End 3
  EndIf
  
Else
  ; No process ID for IDE.
  MessageRequester("Include File Error", "Couldn't obtain a process ID for the editor.", #PB_MessageRequester_Ok)  
  End 2
  
EndIf

; Set the search patterns ("|" as separator)
strPattern = "PureBasic Source (*.pb, *.pbi)|*.pb; *.pbi|Images (*.bmp, *.ico, *.jpg, *.png, *.tga, *.tif)|*.bmp;*.ico;*.jpg;*.png;*.tga;*.tif|All files (*.*)|*.*"

; Set the default pattern.
lngPattern = 0

; Now show a file requester.
strFullPath = OpenFileRequester("Choose file(s) to include...", "", strPattern, lngPattern, #PB_Requester_MultiSelection)

Repeat 
  AddElement(lstFileList()) 
  lstFileList() = strFullPath
  strFullPath = NextSelectedFileName() 
Until strFullPath = "" 

If ListSize(lstFileList()) > 0 
  
  SelectElement(lstFileList(), 0)
  strFullPath = lstFileList()
  strPath = GetPathPart(strFullPath)
  strIncFile = "IncludePath " + Chr(34) + strPath + Chr(34) + Chr(13)
  strIncBin = "IncludePath " + Chr(34) + strPath + Chr(34) + Chr(13) + "DataSection" + Chr(13)
  strEnumBin = "Enumeration" + Chr(13)
  
  ForEach lstFileList()
    
    ; Get the details of the list element.
    strFullPath = lstFileList()
    strExtension = GetExtensionPart(strFullPath)
    strFileName = GetFilePart(strFullPath)
    strPath = GetPathPart(strFullPath)
    
    Select LCase(strExtension)
        
      Case "jpeg", "jpe", "jpg"
        bytFlagDecoder = 1
        bytFlagJPEG = 1
        
      Case "png"
        bytFlagDecoder = 1
        bytFlagPNG = 1
        
      Case "tif", "tiff"
        bytFlagDecoder = 1
        bytFlagTIFF = 1
        
      Case "tga"
        bytFlagDecoder = 1
        bytFlagTGA = 1
        
    EndSelect
    
    ; Add to correct section.
    Select LCase(strExtension)
        
      Case "pb", "pbi"
        bytFlagFile = 1
        strIncFile + "IncludeFile " + Chr(34) + strFileName + Chr(34) + Chr(13)
        
      Case "bmp", "ico", "jpeg", "jpe", "jpg", "png", "tga", "tif", "tiff"
        bytFlagBin = 1
        
        strFileLabel = ReplaceString(strFileName, ".", "")
        strFileLabel = ReplaceString(strFileLabel, " ", "")
        
        strEnumBin + "  #Img" + strFileLabel + Chr(13)
        
        strCatchBin + "CatchImage(#Img" + strFileLabel + ", ?Image_" + strFileLabel + ")" + Chr(13)
        
        strIncBin + "  Image_" + strFileLabel + ":" + Chr(13)
        strIncBin + "  IncludeBinary " + Chr(34) + strFileName + Chr(34) + Chr(13)
        
    EndSelect
    
  Next lstFileList()
  
  ; Build the text.
  strEnumBin + "EndEnumeration" + Chr(13)
  strIncBin + "EndDataSection"
  
  ; Use Decoder section.
  If bytFlagJPEG = 1
    strUseBin + "UseJPEGImageDecoder()" + Chr(13)
  EndIf
  If bytFlagPNG = 1
    strUseBin + "UsePNGImageDecoder()" + Chr(13)
  EndIf
  If bytFlagTIFF = 1
    strUseBin + "UseTIFFImageDecoder()" + Chr(13)
  EndIf
  If bytFlagTGA = 1
    strUseBin + "UseTGAImageDecoder()" + Chr(13)
  EndIf
  
  ; Include sources.
  If bytFlagFile = 1
    strText + strIncFile + Chr(13)
  EndIf
  
  ; Include binaries.
  If bytFlagBin = 1
    If bytFlagDecoder = 1
      strText + strUseBin + Chr(13)
    EndIf
    strText + strEnumBin + Chr(13) + Chr(13)
    strText + strCatchBin + Chr(13) + Chr(13)
    strText + strIncBin + Chr(13) + Chr(13)
  EndIf
  
  ; Get length of the new string.
  lngLength = Len(strText)

  ; Obtain process memory for the string.
  *strText = VirtualAllocEx_(lngPBProcess, 0, lngLength, #MEM_RESERVE | #MEM_COMMIT, #PAGE_EXECUTE_READWRITE)
  
  ; Write the string into the process memory.
  WriteProcessMemory_(lngPBProcess, *strText, @strText, lngLength, #Null)
  
  ; Instruct control to add text to source code.
  SendMessage_(lngScintilla, #SCI_INSERTTEXT, -1, *strText)
  
  ; Release the process memory.
  VirtualFreeEx_(lngPBProcess, *strText, lngLength, #MEM_RELEASE)

EndIf

End
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Include file tool for IDE

Post by idle »

That looks really useful. Thanks
Post Reply