Small snippets of code that I refer to in my daily work.
These assume that you are using C++14 and boost.
... like auto s = "this is a std::string"s;
using namespace std::literals;
... like auto microseconds = 1000us;
using namespace std::chrono_literals;
#include <regex>
auto const result = regex_replace(str,std::regex("<"),"<");
#include <regex>
if (std::regex_match(string,std::regex("[a-zA-Z]*")) {
cout << "Matches\n";
}
#include <fstream>
auto ifs = std::ifstream(file);
auto const data = std::string(
std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>()
);
auto in = std::istream(...);
auto out = std::ostream(...);
out << in.rdbuf();
#include <fstream>
std::string longstr;
{
auto ifs = std::ifstream("/dev/urandom",std::ios::binary);
auto isi = std::istream_iterator<char>(ifs);
std::copy_n(isi,
10'000'000,
std::insert_iterator<std::string>(longstr,longstr.begin()));
}
#include <boost/filesystem.hpp>
boost::filesystem::create_directories(boost::filesystem::path(file).parent_path());
#include <boost/date_time/posix_time/posix_time.hpp>
auto const tm = boost::posix_time::to_tm(boost::posix_time::second_clock::local_time());
auto request = boost::network::http::client::request("http://...");
request << boost::network::header("Connection","close");
auto const result = body(boost::network::http::client().get(request));
#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
// Output (file opened for writing)
int fd = ...;
boost::iostreams::file_descriptor_sink snk(fd,boost::iostreams::close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_sink> os(snk);
os << "Hello World\n";
// Input (file opened for reading)
int fd = ...;
boost::iostreams::file_descriptor_source src(fd,boost::iostreams::close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_source> is(src);
is >> myvariable;
git config --global alias.godlog "log --graph --oneline --decorate"
Requires git 1.6.1 or later.
Add the following to ~/.gitconfig
:
[diff "odf"]
textconv=odt2txt --stdout
Add the following to the .gitattributes
file in the project root:
*.ods diff=odf
*.odt diff=odf
*.odp diff=odf
If git diff
displays the following error:
Error: Unable to connect or start own listener. Aborting.
fatal: unable to read files to diff
then type unoconv -l
and retry.
More information:
- http://www-verimag.imag.fr/~moy/opendocument/
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes ("Diffing Binary Files" section)
... that is created from a Libre Office document which is managed in git as described in the previous section, using the tag-based version number generated by git describe
. Tested with Libre Office 5 in Linux.
Preparation (once per document):
- Open the document in LibreOffice Writer
- Move cursor to where the version number is to be displayed
- Insert → Field → More fields ...
- Variables → Set variable, Name: version, Value: 0.0, Format: Text, Insert, Close
- To show the version number elsewhere: Insert → Field → More fields ... → Variables → Show variable, version, Insert, Close
- Close and save the document
- Add/commit the document to git
To convert the document into a PDF, replacing the "0.0" placeholder by the current git version number:
$ odt2pdf -o myname.pdf -F version=$(git describe --dirty --always) filename.odt
About tag-based git version numbers: https://git-scm.com/docs/git-describe
set (EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build")
find_program (CCACHE_FOUND ccache)
if (CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif (CCACHE_FOUND)
Run these manually in the shell, or put them into your ~/.profile or ~/.bash_profile.
Show the last command's exit status in the shell prompt, and show the prompt with a dark background to make it stand out better. Put the following into your .profile or .bashrc.
PS1BEFORE=$(tput sgr0)$(tput rev)$(tput setaf 4)
PS1AFTER=$(tput sgr0)
PS1='\[$PS1BEFORE\]$? [\h:\w]\[$PS1AFTER\] \$ '
PS1="\[\e]0;\h:\w\a\]$PS1"
export HISTFILE="$HOME/.bash-history"
export HISTFILESIZE=
export HISTSIZE=
Changing the history file name from the default .bash_history to something else (here, by using a dash instead of an underscore, but could be anything) prevents non-login shells (that use the default history size) from deleting our history.
Put the following into /usr/share/X11/xorg.conf.d/20-natural-scrolling.conf, then reboot:
Section "InputClass"
Identifier "Natural Scrolling"
MatchIsPointer "on"
MatchDevicePath "/dev/input/event*"
Option "VertScrollDelta" "-1"
Option "HorizScrollDelta" "-1"
Option "DialDelta" "-1"
EndSection
Source: https://kofler.info/natural-scrolling-mit-dem-mausrad/#more-1956
Access the files in your Nextcloud without syncing them to your harddisk, using Nextcloud's WebDAV interface. Doesn't require disk space to store your Nextcloud files locally. Doesn't use the Nextcloud client software.
Tested with Ubuntu 16.04. Will probably work with Owncloud or any other WebDAV-based file service.
The following examples assume that
mycloud.example.com
is your Nextcloud servermyname
is your Nextcloud user namemypassword
is your Nextcloud password
Preparation:
$ sudo apt install ca-certificates
$ sudo apt install davfs2
$ sudo mkdir /mnt/myname
$ sudo usermod -aG davfs2 $USER
Add the following line to /etc/fstab
:
https://mycloud.example.com/remote.php/webdav /mnt/myname davfs user,noauto 0 0
If you want read-only access (you can read your cloud files but not change them):
https://mycloud.example.com/remote.php/webdav /mnt/myname davfs user,noauto,ro,dir_mode=555,file_mode=444 0 0
Add the following to /etc/davfs2/secrets
:
/mnt/myname myname mypassword
Note: Every user on your Linux machine can mount your Nextcloud files, which may or may not be desired.
Finally, to mount your Nextcloud files:
$ sudo mount /mnt/myname
More information: https://wiki.ubuntuusers.de/WebDAV/
This is not limited to Linux, but works whenever Ghostscript is installed. Replace 39 and 42 with the page numbers you want to extract.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage=39 \
-dLastPage=42 \
-sOutputFile=name_of_your_output_file.pdf \
name_of_your_input_file.pdf
$ drutil tray eject
$ defaults write com.apple.desktopservices DSDontWriteNetworkStores true
Log off or reboot to activate.
To install software like a boss.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew analytics off
More information: http://brew.sh
You may like:
$ brew install htop
$ brew install tree
set profiling=1;
select ...;
show profile;
explain select ...;
*Wolfram Rösler • [email protected] • https://twitter.com/wolframroesler • https://github.com/wolframroesler
Just trying out gists. The main snippets repo is here: https://github.com/wolframroesler/snippets