clean project file (dirt simple)

Share your advanced PureBasic knowledge/code with the community.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

clean project file (dirt simple)

Post by jassing »

I often delete files (or rename them) and then I get a lot of "do you want to find.." dialogs. I needed something to quickly clean up a project (PBP) file (easily delete, or find files) bypassing the somewhat more tedious IDE interface...

(If someone has a way to convert a path to relative for linux, please update the code)

Code: Select all

EnableExplicit
CompilerIf #PB_Compiler_Unicode ;- maximum path length
  #PATH_MAX = $7FFF
CompilerElse
  #PATH_MAX = #MAX_PATH
CompilerEndIf

Global fileCount, deleteCount, fixCount

Procedure.s makeRelative(path.s,file.s) 
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Protected relativePath.s = Space(#PATH_MAX)
      If Not PathRelativePathTo_(@relativePath,path,0,file,#FILE_ATTRIBUTE_DIRECTORY)
        relativePath=file
      EndIf 
    CompilerDefault
      relativePath=file
  CompilerEndSelect
  ProcedureReturn relativePath
EndProcedure 

Procedure processXMLnode(*node, level=0)
  Protected *child, fileName.s, nodesChanged
  If XMLNodeType(*node) = #PB_XML_Normal
    If GetXMLNodeName(*node)="file"
      If ExamineXMLAttributes(*node)
        While NextXMLAttribute(*node)
          If XMLAttributeName(*node)="name"
            If FileSize(XMLAttributeValue(*node))=-1
              fileName=XMLAttributeValue(*node)
              Debug "MISSING: "+fileName
              fileName = OpenFileRequester("Locate Missng file?",fileName,"PB Source|*.pb;*.pbi",0)
              If FileSize(fileName)>0
                SetXMLAttribute(*node,"name",makeRelative(GetCurrentDirectory(),filename))
                If XMLStatus(0) <> #PB_XML_Success
                  MessageRequester("Failed to update", "Error: " + XMLError(0) + #CRLF$+" Line: "+Str(XMLErrorLine(0)) + " Col: " + Str(XMLErrorPosition(0)))
                Else
                  fixCount+1
                EndIf 
              Else 
                DeleteXMLNode(*node)
                If XMLStatus(0) <> #PB_XML_Success
                  MessageRequester("Failed to delete", "Error: " + XMLError(0) + #CRLF$+" Line: "+Str(XMLErrorLine(0)) + " Col: " + Str(XMLErrorPosition(0)))
                Else
                  deleteCount+1
                EndIf 
              EndIf 
              nodesChanged+1
            Else
              ; Debug XMLAttributeValue(*node)
              ; Debug GetXMLNodeText(*node)
              fileCount+1
            EndIf
          EndIf 
        Wend
      EndIf
    EndIf 
    *child = ChildXMLNode(*node)
    While *child <> 0
      nodesChanged+processXMLnode(*child, level + 1)
      *child = NextXMLNode(*child)
    Wend
  EndIf
  ProcedureReturn nodesChanged
EndProcedure

Procedure DeleteMissingFiles( pbProjectFile.s )
  Protected *mainNode, bSuccess, bSave
  If LoadXML(0, pbProjectFile)
    If XMLStatus(0) <> #PB_XML_Success
      MessageRequester("Error proessing project file", "Error: " + XMLError(0) + #CRLF$+"File: " + pbProjectFile+" Line: "+Str(XMLErrorLine(0)) + " Col: " + Str(XMLErrorPosition(0)))
      bSuccess=#False 
    Else 
      *mainNode = MainXMLNode(0)
      If *mainNode
        bSave=Bool(processXMLnode(*mainNode))
      EndIf
    EndIf
    If bSave
      SaveXML(0,pbProjectFile)
    EndIf 
    FreeXML(0)
    bSuccess=#True 
  EndIf 
  ProcedureReturn bSuccess
EndProcedure

Procedure main()
  Protected.s curDir,
           pbProjectFile.s = OpenFileRequester("PureBasic Project File", "", "PB Project Files|*.pbp|All files (*.*)|*.*", 0)
  If pbProjectFile <> "" And FileSize(pbProjectFile)>0
    If MessageRequester("Removing missing files","Is this project file closed?"+#CRLF$+pbProjectFile,#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes
      curDir = GetCurrentDirectory()
      SetCurrentDirectory(GetPathPart(pbProjectFile))
      If DeleteMissingFiles( pbProjectFile )
        MessageRequester("Result","File: "+pbProjectFile+#CRLF$+
                                  "Invalid Files: "      + Str(deleteCount+fixCount)  + #CRLF$ +
                                  "Removed Files: "      + Str(deleteCount)           + #CRLF$ +
                                  "adjusted Files: "     + Str(fixCount)              + #CRLF$ +
                                  "Project File Count: " + Str(fileCount),
                         #PB_MessageRequester_Info )
      EndIf 
      SetCurrentDirectory(curDir)
    Else
      MessageRequester("Files NOT removed","Project file must e closed in the IDE, otherwise changes will be lost",#PB_MessageRequester_Error)
    EndIf 
  EndIf
EndProcedure

main()