Skip to content

Instantly share code, notes, and snippets.

@wolframroesler
Created May 27, 2017 11:37
Show Gist options
  • Save wolframroesler/f4800f4848fc16cc3acf5fad050c8559 to your computer and use it in GitHub Desktop.
Save wolframroesler/f4800f4848fc16cc3acf5fad050c8559 to your computer and use it in GitHub Desktop.
Code Snippets

Code Snippets

Small snippets of code that I refer to in my daily work.

Table of Contents

C++

These assume that you are using C++14 and boost.

Activate std::string literals

... like auto s = "this is a std::string"s;

using namespace std::literals;

Activate std::chrono literals

... like auto microseconds = 1000us;

using namespace std::chrono_literals;

Replace a string with regex

#include <regex>
auto const result = regex_replace(str,std::regex("<"),"&lt;");

Check if a string matches a regex

#include <regex>
if (std::regex_match(string,std::regex("[a-zA-Z]*")) {
    cout << "Matches\n";
}

Load a file into memory

#include <fstream>
auto ifs = std::ifstream(file);
auto const data = std::string(
    std::istreambuf_iterator<char>(ifs),
    std::istreambuf_iterator<char>()
);

Copy all from one stream into another

auto in = std::istream(...);
auto out = std::ostream(...);
out << in.rdbuf();

Make a long random string

#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()));
}

Create all subdirectories required for a file

#include <boost/filesystem.hpp>
boost::filesystem::create_directories(boost::filesystem::path(file).parent_path());

Get current local time as a struct tm

#include <boost/date_time/posix_time/posix_time.hpp>
auto const tm = boost::posix_time::to_tm(boost::posix_time::second_clock::local_time());

Load a URL with cpp-netlib

auto request = boost::network::http::client::request("http://...");
request << boost::network::header("Connection","close");
auto const result = body(boost::network::http::client().get(request));

Convert a file descriptor into an I/O stream

#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

Create the “git godlog” command

git config --global alias.godlog "log --graph --oneline --decorate"

Manage Libre Office files in git

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:

Put git version number into PDF

... 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):

  1. Open the document in LibreOffice Writer
  2. Move cursor to where the version number is to be displayed
  3. Insert → Field → More fields ...
  4. Variables → Set variable, Name: version, Value: 0.0, Format: Text, Insert, Close
  5. To show the version number elsewhere: Insert → Field → More fields ... → Variables → Show variable, version, Insert, Close
  6. Close and save the document
  7. 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

cmake

Same output directory for all sub-projects

set (EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build")

Use compiler cache (ccache)

find_program (CCACHE_FOUND ccache)
if (CCACHE_FOUND)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif (CCACHE_FOUND)

bash

Run these manually in the shell, or put them into your ~/.profile or ~/.bash_profile.

Shell Prompt

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\] \$ '

Screenshot showing bash prompt

Host Name And Work Directory In Terminal Window Title

PS1="\[\e]0;\h:\w\a\]$PS1"

Unlimited History

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.

Linux

Natural Scrolling

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

Mount Nextcloud

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 server
  • myname is your Nextcloud user name
  • mypasswordis 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/

Extract Pages From PDF File

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

macOS

Eject CD/DVD

$ drutil tray eject

Don't Create .DS_Store Files

$ defaults write com.apple.desktopservices DSDontWriteNetworkStores true

Log off or reboot to activate.

Install Package Manager

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

MySQL/MariaDB

SQL Profiling

set profiling=1;
select ...;
show profile;

Show Explain Plan

explain select ...;

*Wolfram Rösler • wolfram@roesler-ac.dehttps://twitter.com/wolframroeslerhttps://github.com/wolframroesler

@wolframroesler
Copy link
Author

Just trying out gists. The main snippets repo is here: https://github.com/wolframroesler/snippets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment