Removing comments in FreeRADIUS files

Share your advanced PureBasic knowledge/code with the community.
ROUMANET
User
User
Posts: 12
Joined: Thu Aug 26, 2004 9:18 pm
Location: Isere (France)

Removing comments in FreeRADIUS files

Post by ROUMANET »

Code updated For 5.20+

I was boring to compare very long files for just few commands so I've write a PB Code to remove # commented lines. This program shows how to use two files at the same time (one in read only and the other in write) and string comparison. You're free to use it like you want :)

Code: Select all

; programme de suppression de lignes de commentaires
Declare.s EnleveTab(ligne.s)


NomFichier$ = OpenFileRequester("Ouvrir un fichier avec commentaires", "C:\", "*.*", 0)
If NomFichier$ > ""
  ; on a un fichier a traiter
  Chemin$ = GetPathPart(NomFichier$)
  Extension$ = GetExtensionPart(NomFichier$)
  Fichier$ = RemoveString(GetFilePart(NomFichier$), Extension$)
  NewFichier$ = Left(Fichier$, Len(Fichier$)-1)+"_NoComment."+Extension$
  Debug Fichier$+"  |  "+Extension$+"   ==>  "+NewFichier$
  If ReadFile(0,NomFichier$)
    OpenFile(1,Chemin$+NewFichier$)
    
    While Eof(0)=0
      ligne$ = ReadString(0)
      Debug "==>"+ligne$
      
      If Left(EnleveTab(ligne$),1) = "#"   ; on enlève les espaces devant et on vérifie si c'est un commentaire
        ; c'est un commentaire
      ElseIf ligne$>""
        ; ce n'est pas un commentaire : on écrit la ligne en conservant son formatage
        
        WriteStringN(1,ligne$)
        
        Debug "<=="+ligne$
      EndIf
    Wend
  EndIf
  CloseFile(0)
  CloseFile(1)
EndIf

Procedure.s EnleveTab(ligne$)
  Repeat
    a$ = Left(ligne$,1)
    If a$ = " " Or a$ = Chr(9)
      ligne$ = Right(ligne$, Len(ligne$)-1)
    EndIf
  Until a$ <> " " And a$ <> Chr(9)
  ProcedureReturn ligne$
EndProcedure
Sorry for french comments but the code should be easy to understand :wink: