C++
“If Microsoft ever does applications for Linux it means I’ve won.”
~ Linus Torvalds
I’ve given Visual Studio Code for Linux as prebuilt rpm-package a try. I don’t know what I expected ten minutes ago, but I am definitely under-whelmed! Another editor with syntax-highlighting, code-completion, integrated git-support and the ability to add “extensions” for all kinds of stuff. But: do I miss the trick or how do I start build binaries? (edith: possible via some “we need another build-configuration-system..”-JSON).
OK, it was fun to test, but now back to my favorite IDE* ๐ gedit & gcc on the terminal can do the same, so I don’t see any reason to use VS Code.ย Maybe for Go or one of the other languages, but not C++.
*QtCreator
aseprite: how to build @ Linux
What should i say? I was searching for a more sophisticated GIF-editor to create more smooth animation-loops. Then I saw aseprite. Binaries require “donation”, so why not build it yourself since it is OSS? Sadly there were no clear official hints on how to build on Linux.
Turns out this guide is giving the correct directions:
1 2 |
cmake .. -G "Unix Makefiles" make -j 3 |
Estimate quickly the ‘size’ of the project
Nothing in comparison to SLOCCount or other tools, but sometimes you just need a ‘value‘ ๐
1 |
find . -regex ".*\.\(h\|cpp\)" | sort | xargs wc -l |
103 ./main.cpp
90 ./map.cpp
35 ./map.h
63 ./mapitem.cpp
29 ./mapitem.h
122 ./mapprocessor.cpp
27 ./mapprocessor.h
79 ./mapreader.cpp
23 ./mapreader.h
571 total
edith 20160819: sort the result before counting for a much nicer appearance ๐
C/C++-comments
One of my heroes (Linus Torvalds) published some days ago some rant about the “correct” style of comments.
Honestly, I agree 100%! And from my POV his version c is my preferred one: for single lines and blocks of comments.
(c)
// This can be a single line. Or many. Your choice.
Had some severe discussions about this topic in past dev-teams while negotiation some “best practices/style-guide”.
Reasons:
* you can quickly comment/uncomment sections of code without worrying where the “begin” and where the “end” is
* if you used /* .. */ then “uncommenting” could result in commenting something else, because of nested comment-parts
* only drawback: comments last until the end of the line
QtScrobbler (final cut)
Just to close the topic: I finished what I planned for the forked QtScrobbler-project. I changed the workflow, so that if you did not provide correct credentials for your favourite submission-site (last.fm, libre.fm ..), then the progress-dialog will be closed and you will see immediately the error in the ui (not just log). Because before the user could wait until the cows come home – and I found this really annoying, especially when I set up QtScrobbler on a new system, mistyped or forgot to set the credentials at all.
Besides: also cleaned the code a little bit with the help of cppcheck ๐
The repository: https://github.com/marcelpetrick/QTScrobbler
All in all it was a nice experience: set up GitHub, clone the repository, work into a medium large project, try to fix an error without breaking the remaining functionality ..
I do not fully agree to the original code of conduct for style and includes. But as long as it is comprehensive ..
cppcheck pt. II
It’s quite funny if you want to use the current version of your favorite static code analysis-tool, then realize that no OS-binary is availabe, then think “why not create it yourself like you did it with older versions?” (qmake && make ..), then realize your current clang-version does not support the missing c++11-features (cbegin/cend), then you realize you are not allowed upgrade your local compiler ..
To put it in a nutshell:
1 |
homebrew install cppcheck && cd <source to check> && cppcheck --enable=all --inconclusive --std=posix . 2> cppcheck.txt |
Yes, this leaves you without the gui, but still better than nothing.
side note: I also found the cppcheck-plugin for QtCreator-project on sourceforge (new since august 2015?), but since the contributor provides just Win and Linux binaries, but neither source nor OSX-binaries, I have to wait. But looks promising.
edit 20151005:
Only fifteen hours after my request the russian mastermind behind the project built an OSX-dylib for the cppcheck-plugin and offered it to me for testing. After update from QtCreator 3.42 to 3.5, copying the plugin, activating it and setting the path to the homebrew-1.70-cppcheck, everything works! No more switching between the cppcheck-output and QtCreator, just fix the issues! *feels good*
edit 20151006:
Now it is officially available. Enjoy!
My first (?) offical fork?
I want to fix several smaller, but annoying issues with QtScrobbler. Originally hosted on sourceforge, but development was discontinued in 2013. I mean: it worked. I used it for years with my upgraded SANSA clips.
So I forked the project to github, upgraded the whole project to the current Qt-version (5.5) and fixed immediately some ‘klein-klein’. But now I am too tired …
Simple grid-layouter
How to layout a given number of thumbnails N of picW x picH into m columns and n rows in a given page (pageW x pageH) so that the “to be applied” scale is maximal? All thumbnails yield the same dimensions, reordering is not allowed, rotation neither. If the chosen scale is maximized, then the result will have the biggest possible thumbnails. I need this for the contactSheet-script, because currently it uses fixed column-sizes and I don’t want to figure this out manually each time the wanted page- or thumbnail-size or the amount of thumbnails changes.
I am aware, that this is some kind of well known bin-packing problem, but just after implementing the very first naive approach I figured out that there is maybe(?) no closed equation possible: just iterating over all possible cases. Maybe I missed something, but the current approach would look like the following. I will move this from C++/Qt to sh soon, because this will become part of contactSheet-script which I apply to every set of scanned film ๐
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 |
int const pageW(ui->pageW_LE->text().toInt()); //maybe validate later! int const pageH(ui->pageH_LE->text().toInt()); //maybe validate later! int const picW(ui->picW_LE->text().toInt()); //maybe validate later! int const picH(ui->picH_LE->text().toInt()); //maybe validate later! int const N(ui->N_LE->text().toInt()); //maybe validate later! int result[2]; double maxScale(0.0); double maxA(0.0); for(int m(1); m <= N; m++) { int const n = N / m + ((N % m != 0) ? 1 : 0); qDebug() << "############################################\nm * n: " << QString::number(m) << " * " << QString::number(n); double const scaleW = (double)pageW / (double)(m * picW); double const scaleH = (double)pageH / (double)(n * picH); qDebug() << "scales: " << QString::number(scaleW) << " * " << QString::number(scaleH); double const minScale(scaleW < scaleH ? scaleW : scaleH); double const resultW = m * minScale * picW; double const resultH = n * minScale * picH; double const A = resultW * resultH; qDebug() << "A = " << QString::number(A) << " with scale " << QString::number(minScale); //use the first found biggest result if(minScale > maxScale) { maxScale = minScale; //save for later usage maxA = A; //just for checking result[0] = m; result[1] = n; } } qDebug() << "\n\nlayout with:" << QString::number(result[0]) << "*" << QString::number(result[1]) << " with A = " << QString::number(maxA) << " and scale " << QString::number(maxScale); |
By the way: the sketch has been done with draw.io, because I stopped using Dia …
Qt: normalized signal-slot-connections
While writing my last post I remembered a tool I run at least once for every project I touch (do, finish, ..): ‘normalize’.
Originally I stumbled about it while reading Marc Mutz’ blog. The downside is: currently the original download-location is not available anymore .. I will try to fix this.
The effect behind normalised signal-slot-connections is that for the whole project every connect will use the same style. No additional whitespace, no const, no unneeded variable-names, .. perfect.
Invoke it with “normalize –modify PROJECTPATH”. Without the flag it will just show the “to be changed sections”.
Never seen any downside effects.
edit: Working download-URL added!
part II: Remove multiple whitespace-lines from a file
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 } |