Written by marcelpetrick
on May 7, 2015
I was so annoyed that the simple idea with sed did not worked like I wanted, that I wrote a short full-fledged Qt-appp for it. Code follows. Just compile the main.cpp. Usage is also explained in the header of the file.
I know it’s like to break a butterfly on a wheel, but I need this functionality since I don’t want to do such a boring task like code-styling by myself ..
Update: first version had a small glitch! Found after applying it to some bigger files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
//! @brief lessWhite should remove all multiple empty lines (newline, whitespace, tabs, spaces, ...) from the given input. //! Based on the standard POSIX channels stdin, stdout, stderr. //! usage like: $ ./lessWhite < input.txt > output.txt //! terminal output will look like: "removed 67 lines!" //! @author mail@marcelpetrick.it //! @version 0.02 (bugfix for "output had one additional newline at the end") //! //! history: 0.01: initial state #include <QTextStream> #include <QString> int main(int argc, char *argv[]) { Q_UNUSED(argc); Q_UNUSED(argv); QTextStream stream(stdin); QString output; //the combined stuff: no checks for overflow QString line; bool lastWasEmpty(false); int removedLines(0); bool isVeryFirstLine(true); do { line = stream.readLine(); bool const currentIsEmpty(line.trimmed().isEmpty()); if(currentIsEmpty && lastWasEmpty) { removedLines++; //do nothing } else { if(!isVeryFirstLine) //prevent the bug with the last line == double newline { output.append("\n"); } else { isVeryFirstLine = false; //reset now } output.append(line); } lastWasEmpty = currentIsEmpty; } while(!line.isNull()); //output QTextStream cout(stdout); cout << output; //print to stdout QTextStream cerr(stderr); cerr << "removed " << removedLines << " lines!\n"; //print to stderr } |
Leave a Reply
You must be logged in to post a comment.