\chapter{eina\+\_\+stringshare\+\_\+example\+\_\+01\+\_\+page}
\hypertarget{eina_stringshare_example_01_page}{}\label{eina_stringshare_example_01_page}\index{eina\_stringshare\_example\_01\_page@{eina\_stringshare\_example\_01\_page}}


Like all examples we start by including Eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}

\end{DoxyCodeInclude}
 Here we declare some variables and initialize eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{char}\ **argv\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str,\ *str2;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *prologe\ =\ \textcolor{stringliteral}{"{}The\ Cylons\ were\ created\ by\ man.\ They\ rebelled.\ They\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}evolved."{}};}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *prologe2\ =\ \textcolor{stringliteral}{"{}\%d\ Cylon\ models.\ \%d\ are\ known.\ \%d\ live\ in\ secret.\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}\%d\ will\ be\ revealed."{}};}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *prologe3\ =\ \textcolor{stringliteral}{"{}There\ are\ many\ copies.\ And\ they\ have\ a\ plan."{}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}

\end{DoxyCodeInclude}
 We start the substantive part of the example by showing how to make a part of a string shared and how to get the length of a shared string\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ str\ =\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga1901668fffca5f06ace9b150715dfc76}{eina\_stringshare\_add\_length}}(prologe,\ 31);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}length:\ \%d\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga8c4fd1a1b3edb40eca0ef80bb41c58bd}{eina\_stringshare\_strlen}}(str));}

\end{DoxyCodeInclude}
As we add shared strings we also need to delete them when done using them\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(str);}

\end{DoxyCodeInclude}
 There are many ways of creating shared strings including an equivalent to sprintf\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ str\ =\ eina\_stringshare\_printf(prologe2,\ 12,\ 7,\ 4,\ 1);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(str);}

\end{DoxyCodeInclude}
 An equivalent to snprintf\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ str\ =\ eina\_stringshare\_nprintf(45,\ \textcolor{stringliteral}{"{}\%s"{}},\ prologe3);}

\end{DoxyCodeInclude}
 But the simplest way of creating a shared string is through \doxylink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\+\_\+stringshare\+\_\+add()}\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str);}

\end{DoxyCodeInclude}
 Sometimes you already have a pointer to a shared string and want to use it, so to make sure the provider of the pointer won\textquotesingle{}t free it while you\textquotesingle{}re using it you can increase the shared string\textquotesingle{}s ref count\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ str2\ =\ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(prologe3);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str2);}

\end{DoxyCodeInclude}
 Whenever you have a pointer to a shared string and would like to change it\textquotesingle{}s value you should use eina\+\_\+stringshare\+\_\+replace()\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga8c841e078a37b556e0bf91825f3df174}{eina\_stringshare\_ref}}(str2);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(str2);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str2);}

\end{DoxyCodeInclude}
\begin{DoxyWarning}{Warning}
{\bfseries{Don\textquotesingle{}t}} use \doxylink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\+\_\+stringshare\+\_\+del()} followed by eina\+\_\+share\+\_\+common\+\_\+add(), under some circumstances you might end up deleting a shared string some other piece of code is using.
\end{DoxyWarning}
We created str but haven\textquotesingle{}t deleted it yet, and while we called \doxylink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\+\_\+stringshare\+\_\+del()} on str2, we created it and then increased the ref count so it\textquotesingle{}s still alive\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga9e32a137ce8c4cd5f6ec3dbde3dbedd8}{eina\_stringshare\_replace}}(\&str,\ prologe);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(str);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(str2);}

\end{DoxyCodeInclude}
 You can see the full source code \doxysectlink{eina_stringshare_example_01}{here}{0}. 