
\begin{DoxyItemize}
\item infos = Information about ccode plugin is in keys below
\item infos/author = Markus Raab \href{mailto:elektra@libelektra.org}{\tt elektra@libelektra.\+org}
\item infos/licence = B\+SD
\item infos/provides = code
\item infos/needs =
\item infos/recommends =
\item infos/placements = postgetstorage presetstorage
\item infos/status = maintained unittest nodep libc configurable
\item infos/description = Decoding/\+Encoding engine which escapes unwanted characters.
\end{DoxyItemize}\hypertarget{md_src_plugins_ccode_README_src_plugins_ccode_README_md}{}\section{C\+Code}\label{md_src_plugins_ccode_README_src_plugins_ccode_README_md}
\subsection*{Introduction}

The {\ttfamily ccode} plugin replaces (escapes) any special characters with two characters\+:


\begin{DoxyItemize}
\item an escape character (default\+: {\ttfamily \textbackslash{}}) and
\item another character representing the escaped character (e.\+g {\ttfamily n} for newline)
\end{DoxyItemize}

before writing a {\ttfamily Key\+Set}. The plugin undoes this modification after reading a {\ttfamily Key\+Set}.

C\+Code provides a reasonable default configuration, using the usual escape sequences for C strings (e.\+g. {\ttfamily \textbackslash{}n} for newline, {\ttfamily \textbackslash{}t} for tab). You can also configure the escape character ({\ttfamily /escape}) and the mapping for special characters ({\ttfamily chars}).

\subsection*{Examples}

\subsubsection*{Default Configuration}


\begin{DoxyCode}
# Mount `tcl` storage plugin together with the required `base64` plugin.
# We use the `ccode` plugin to escape special characters.
sudo kdb mount config.tcl user/tests/ccode tcl ccode base64

# Add a key value containing newline characters
kdb set user/tests/ccode/multiline "`printf 'one\(\backslash\)ntwo\(\backslash\)nthree'`"
# By default the plugin uses `\(\backslash\)n` to escape newline characters
grep 'multiline' `kdb file user/tests/ccode` | sed 's/[[:space:]]*//'
#> multiline = one\(\backslash\)ntwo\(\backslash\)nthree

# The `ccode` plugin escapes and unescapes the data. The `tcl` plugin
# returns the unescaped values.
kdb get user/tests/ccode/multiline
#> one
#> two
#> three

# Write and read a key value containing a tab character
kdb set user/tests/ccode/tab 'Tab       Fabulous'
kdb get user/tests/ccode/tab
#> Tab  Fabulous

# The plugin also escapes special characters inside key names
kdb set 'user/tests/ccode/tab/t\(\backslash\)ta      b' 'Escaped Tabs'
grep 'tab/' `kdb file user/tests/ccode` | sed 's/[[:space:]]*//'
#> tab/t\(\backslash\)\(\backslash\)ta\(\backslash\)tb = Escaped Tabs

# Undo modifications to database
kdb rm -r user/tests/ccode
sudo kdb umount user/tests/ccode
\end{DoxyCode}


\subsubsection*{Custom Configuration}

``{\ttfamily  $<$h1$>$We use}\%{\ttfamily as escape character and map the space character (hex\+:}20{\ttfamily ) to the character}\+\_\+{\ttfamily .$<$/h1$>$ sudo kdb mount config.\+tcl user/tests/ccode tcl base64 ccode escape=}bash -\/c \textquotesingle{}printf x "\textquotesingle{}` chars/20=`bash -\/c \textquotesingle{}printf x "\+\_\+\textquotesingle{}`

kdb set user/tests/ccode/spaces \textquotesingle{}one two three\textquotesingle{}

grep \textquotesingle{}space\textquotesingle{} {\ttfamily kdb file user/tests/ccode/spaces} $\vert$ sed \textquotesingle{}s/\mbox{[}\mbox{[}\+:space\+:\mbox{]}\mbox{]}$\ast$//\textquotesingle{} \#$>$ spaces = one\+\_\+two\+\_\+three

kdb get user/tests/ccode/spaces \#$>$ one two three

\section*{Undo modifications to database}

kdb rm -\/r user/tests/ccode sudo kdb umount user/tests/ccode ```

\subsection*{Restrictions}

This method of encoding characters is not as powerful as the hexcode plugin in terms of reduction. The hexcode plugin allows reduction of the character set to \textquotesingle{}0\textquotesingle{}-\/\textquotesingle{}9\textquotesingle{}, \textquotesingle{}a\textquotesingle{}-\/\textquotesingle{}f\textquotesingle{} and one escape character. So it can represent any key value with only 17 characters. On the other hand, ccode cannot reduce the set more than by half.

So when all control characters and non-\/\+A\+S\+C\+II characters need to vanish, it cannot be done with the ccode plugin. But it is perfectly suitable to reduce by some characters. The advantages are that the size only doubles in the worst case and that it is much easier to read.

\subsection*{C}

In the C language, the following escape characters are present.


\begin{DoxyItemize}
\item {\ttfamily b}\+: backspace, hex\+: 08
\item {\ttfamily t}\+: horizontal tab, hex\+: 09
\item {\ttfamily n}\+: new line feed, hex\+: 0A
\item {\ttfamily v}\+: vertical tab, hex\+: 0B
\item {\ttfamily f}\+: form feed, hex\+: 0C
\item {\ttfamily r}\+: carriage return, hex\+: 0D
\item {\ttfamily \textbackslash{}\textbackslash{}}\+: back slash, hex\+: 5C
\item {\ttfamily \textquotesingle{}}\+: single quote, hex\+: 27
\item {\ttfamily "}\+: double quote, hex\+: 22
\item {\ttfamily 0}\+: null, hex\+: 00
\end{DoxyItemize}

This is also the default mapping.

\subsubsection*{Contract}

Add {\ttfamily ccode} to {\ttfamily infos/needs} for any plugin that you want to be filtered by ccode. 