Wrapper for a code-beautifier

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
Heertsch
Posts: 28
Joined: Sat Jan 02, 2010 1:04 am

Wrapper for a code-beautifier

Post by Heertsch »

As a little "Thank You" for the answers in this forum I wrote a wrapper for a code-beautifer.
  • Target group: developers, which work with libraries
  • Purpose: backup your library with versioning, restore it and you get an index of contents of all your sub's and function's in the modules. After restore restart your IDE (=close and reopen) to see the changes.
  • Settings:
    • change the settings of sVersion as you like,
    • Library has to point to the name of the library, which is to backup
  • Further development: in vSourceLines(i) are the lines of your code. You can write your own function to change these lines (eg. to indent lines) and store it back to vSourceLines(i) (Do not restore the running module: it crashes IDE)
Here is the code: quick and dirty.
In the header of the code you see the result of a restoreModules (after a previous run of backupModules)
enjoy (but don't blame me, if it sets your machine on fire Image )
Andreas

Code: Select all

'Index of Contents ================================== (start)
' 00012              sub Main
' 00022       Public Sub backupModules()
' 00056       Public Sub restoreModules()
' 00099 Private Function DetectSubOrFunction$(sLine$,iLineNr%)
' 00124  public Function getHomePath() as string
'Index of Contents ================================== (end)

global const sVersion="V 0.0 Build 0" 'change this for your needs (and use sVersion in your Help=>Info window) 
const Library$="Basal" 'use here your Library name
const SavePath="/SaveModules/" 'this is the directory relative to your doc, which contains the modules after backup

const Header="'Index of Contents =================================="

sub Main
  backupModules
end sub

'======================================
'backup Modules from Library 
'and generate an index of contents as header
'======================================
Public Sub backupModules()
  Dim vModuleNames as any, vModule as any,i%,j%,hFile%,sPath$,sContents$,vSourceLines
  const HeaderStart$=Header + " (start)" 'Start Header Line
  const HeaderEnd$=Header + " (end)"     'End Header Line
  sPath=getHomePath + SavePath +sVersion +"/"
  with ThisComponent.getLibraryContainer.getByName(Library).ModuleContainer
    vModuleNames=.getElementNames
    for i=0 to uBound(vModuleNames)
      vModule=.getByName(vModuleNames(i))
      hFile=FreeFile
      vSourceLines=split(vModule.Source,chr(10))
      sContents=HeaderStart  + chr(10)
      for j=0 to Ubound(vSourceLines)
        sContents=sContents + DetectSubOrFunction(vSourceLines(j),j) 'make index of contents
      next j
      j=instr(vModule.Source, HeaderEnd)
      if j=0 then ' there is no index
      	sContents=sContents + HeaderEnd + chr(10) + chr(10) + vModule.Source
      else 'suppress old index
        sContents=sContents + mid(vModule.Source,j)
      endif
      open sPath+VModuleNames(i)+".bas" for output as hFile
      print #hFile, sContents
      close #hFile
    next i
  end with
  msgbox sVersion +" saved",0,"EndoTherm IDE"
end sub

'===================================
'Import Modules (saved by backupModules())
'===================================
Public Sub restoreModules()
  Dim vModuleNames as any, sModuleSavedNames$(),i%,hFile%,sPath$,sLine$,sSource$,sModuleName$,sHeader$
  Dim oProp(4) as new com.sun.star.beans.PropertyValue
  sPath=getHomePath + SavePath + sVersion + "/"
  sModuleName=Dir(sPath)
  i%=-1
  do while sModuleName<>""
  	i=i+1
  	Redim preserve sModuleSavedNames(i)
  	sModuleSavedNames(i)=sModuleName
  	sModuleName=Dir
  loop
  with 	BasicLibraries.GetByName(Library) 'ThisComponent.getLibraryContainer.getByName(Library).ModuleContainer
    on error goto ErrHandler
    for i=0 to uBound(sModuleSavedNames)
      sModuleName=left(sModuleSavedNames(i),len(sModuleSavedNames(i))-4)
      hFile=FreeFile
      sSource=""
      open sPath+sModuleSavedNames(i) for input as hFile
      do while not EOF(hFile)
        line input #hFile, sLine
        sSource=sSource + sLine + chr(10)
      loop
      close #hFile
      if instr(sSource,"Sub restoreModules()")=0 then 'skip this running Module 
        if .hasByName(sModuleName) then
          .replaceByName(sModuleName,sSource)
        else
          .insertByName(sModuleName,sSource)
        endif
      endif
    next i
  end with
  if i=0 then msgbox ("no files imported") else msgbox("Done: Restart IDE to see the changes")
  exit sub
ErrHandler:
  msgbox Err & " in Line "& Erl & ": "&Error,0
  stop
end sub

'==================================
'Detect sub or function
'==================================
Private Function DetectSubOrFunction$(sLine$,iLineNr%)
  Dim iPos%,sBeginLine$,iSpace%
  sLine=" "+sLine
  DetectSubOrFunction=""
  iPos=instr(LCase(sLine)," sub ")
  if iPos=0 then
    iPos=instr(LCase(sLine)," function ")
	if iPos=0 then exit function  
	iSpace=8
  endif
  if iSpace=0 then iSpace=13
  sBeginLine=left(sLine,iPos+1)
  if instr(sBeginLine," end ")>0 then exit function
  if instr(sBeginLine,"'")>0 then exit function
  if instr(sBeginLine," rem ")>0 then exit function
  if instr(sBeginLine," exit ")>0 then exit function
  if instr(sBeginLine,"""")>0 then exit function
  if instr(sBeginLine," private ")>0 then iSpace=iSpace-8
  if instr(sBeginLine," public ")>0 then iSpace=iSpace-7
  DetectSubOrFunction="' "&Format(iLineNr,"00000")& space(iSpace) + sLine & chr(10)
end function

'==========================================
'return HomePath
'==========================================
public Function getHomePath() as string
  dim vPath,vPathComps
  vPath=ThisComponent.geturl()  'use directory of thisComponent as Registry Path
  vPathComps=split(vPath,"/")    
  vPath=left(vPath,Len(vPath)-Len(vPathComps(UBound(vPathComps)))) 'cutoff filename
  getHomePath=vPath
end function
openOffice 3.2 on Windows 7 / Mac OSC 10.5 / Ubuntu 10.4
Post Reply