Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

Script to remove unnecessary blank lines in C++ implementation files in a folder

I have a folder structure like this:

----src/
        ----foo.cpp
        ----bar.cpp

foo.cpp is thus:

#pragma once

#include "foo.h"


void FOO::func1(){


    printf("Foo Func1
");


}

void func2(){

    printf("Func2
");
}

I want to rewrite this file where there is exactly one empty line between function implementations, and before and after one. That is, within any function implementation and outside of function implementations, I want all empty lines to be removed. I want foo.cpp to be transformed thus:

#pragma once
#include "foo.h"

void FOO::func1(){
    printf("Foo Func1
");
}

void func2(){
    printf("Func2
");
}

bar.cpp is likewise. Is it possible to write and run a script/batch (linux or windows is fine) that does this on all files in a given folder /src/ in this case.

Other detail: My current way of doing this is to open up each file in vim, use { and } to navigate between vim paragraphs that takes me to the next empty line and dd delete that line if it is to be removed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:your files"
SET "destdir=u:your results"


FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%*.cpp" '
 ) DO >"%destdir%\%%a" (
 FOR /f "tokens=1*delims=]" %%c IN ('find /n /v "" ^<"%sourcedir%\%%a"') DO (
  FOR /f %%t IN ("%%d") DO (
   ECHO %%d
   FOR %%m IN (} #include) DO IF /i "%%t"=="%%m" echo/
  )
 )
)

GOTO :EOF

The %%a loop assigns each .cpp filename in turn to %%a.

The %%c loop reads each line from the file and prefixes the line with a line number in square brackets using the find utility (note : Microsoft's cmd utility find) the resultant line is then tokenised, [number going to %%c and remainder of line after ] to %%d.

The %%t loop finds the first token on the line (if it exists) then regurgitates the line and adds a blank line if the first token was (any of the strings in the list processed by %%m) - I added /i to the if to make the match case-insensitive.

As a check, I'd use

fc /w "sourcedirectoryname*.cpp" "destinationdirectoryname*.cpp"

which should execute a file-compare between the original and processed files, disregarding whitespace.

The source directory and destination directory must be different, otherwise batch will attempt to overwrite the very file it's reading. No good will come of that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...