\doxysection{cmem.\+c}
\label{cmem_8c_source}\index{src/cmem.c@{src/cmem.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{preprocessor}{\#include\ "{}cmem.h"{}}}
\DoxyCodeLine{00002\ }
\DoxyCodeLine{00003\ \textcolor{keywordtype}{void}\ *malloc\_chk(\textcolor{keywordtype}{size\_t}\ size,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00004\ \{}
\DoxyCodeLine{00005\ \ \ \ \ \textcolor{keywordtype}{void}\ *p\ =\ NULL;}
\DoxyCodeLine{00006\ }
\DoxyCodeLine{00007\ \ \ \ \ \textcolor{keywordflow}{if}((p\ =\ malloc(size))\ ==\ NULL)\ \{}
\DoxyCodeLine{00008\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ cannot\ allocate\ memory\(\backslash\)n"{}},\ tag);}
\DoxyCodeLine{00009\ \ \ \ \ \ \ \ \ exit(1);}
\DoxyCodeLine{00010\ \ \ \ \ \}}
\DoxyCodeLine{00011\ \ \ \ \ \textcolor{keywordflow}{return}\ memset(p,\ 0,\ size);}
\DoxyCodeLine{00012\ \}}
\DoxyCodeLine{00013\ }
\DoxyCodeLine{00014\ \textcolor{keywordtype}{void}\ *calloc\_chk(\textcolor{keywordtype}{size\_t}\ nmemb,\ \textcolor{keywordtype}{size\_t}\ size,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00015\ \{}
\DoxyCodeLine{00016\ \ \ \ \ \textcolor{keywordtype}{void}\ *p\ =\ NULL;}
\DoxyCodeLine{00017\ }
\DoxyCodeLine{00018\ \ \ \ \ \textcolor{keywordflow}{if}((p\ =\ calloc(nmemb,\ size))\ ==\ NULL)\ \{}
\DoxyCodeLine{00019\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ cannot\ allocate\ memory\(\backslash\)n"{}},\ tag);}
\DoxyCodeLine{00020\ \ \ \ \ \ \ \ \ exit(1);}
\DoxyCodeLine{00021\ \ \ \ \ \}}
\DoxyCodeLine{00022\ \ \ \ \ \textcolor{keywordflow}{return}\ p;}
\DoxyCodeLine{00023\ \}}
\DoxyCodeLine{00024\ }
\DoxyCodeLine{00025\ \textcolor{keywordtype}{char}\ *strdup\_chk(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00026\ \{}
\DoxyCodeLine{00027\ \ \ \ \ assert(s\ !=\ NULL);}
\DoxyCodeLine{00028\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ malloc\_chk(strlen(s)\ +\ 1,\ tag);}
\DoxyCodeLine{00029\ \ \ \ \ strcpy(t,\ s);}
\DoxyCodeLine{00030\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00031\ \}}
\DoxyCodeLine{00032\ }
\DoxyCodeLine{00033\ \textcolor{keywordtype}{char}\ *strndup\_chk(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s,\ \textcolor{keywordtype}{size\_t}\ len,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00034\ \{}
\DoxyCodeLine{00035\ \ \ \ \ assert(s\ !=\ NULL);}
\DoxyCodeLine{00036\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ NULL;}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \ \ \ \ \textcolor{keywordflow}{if}(len\ <\ strlen(s))\ \{}
\DoxyCodeLine{00039\ \ \ \ \ \ \ \ \ t\ =\ malloc\_chk(len\ +\ 1,\ tag);}
\DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ strncpy(t,\ s,\ len);}
\DoxyCodeLine{00041\ \ \ \ \ \ \ \ \ t[len]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00042\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ t\ =\ strdup\_chk(s,\ tag);}
\DoxyCodeLine{00044\ \ \ \ \ \}}
\DoxyCodeLine{00045\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00046\ \}}
\DoxyCodeLine{00047\ }
\DoxyCodeLine{00048\ \textcolor{keywordtype}{void}\ strip\_end(\textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00049\ \{}
\DoxyCodeLine{00050\ \ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i\ =\ strlen(s)\ -\/\ 1;\ i\ >\ 0\ \&\&\ (s[i]\ ==\ \textcolor{charliteral}{'\(\backslash\)n'}\ ||\ s[i]\ ==\ \textcolor{charliteral}{'\(\backslash\)r'}\ ||\ s[i]\ ==\ \textcolor{charliteral}{'\ '}\ ||\ s[i]\ ==\ \textcolor{charliteral}{'\(\backslash\)t'});\ i-\/-\/)\ \{}
\DoxyCodeLine{00051\ \ \ \ \ \ \ \ \ s[i]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00052\ \ \ \ \ \}}
\DoxyCodeLine{00053\ \}}
\DoxyCodeLine{00054\ }
\DoxyCodeLine{00055\ \textcolor{keywordtype}{void}\ strip\_casl2\_comment(\textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00056\ \{}
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{keywordtype}{bool}\ quoting\ =\ \textcolor{keyword}{false};}
\DoxyCodeLine{00058\ }
\DoxyCodeLine{00059\ \ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i\ =\ 0;\ s[i];\ i++)\ \{}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」で囲まれた文字列の場合。「''」は無視\ */}}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(s[i]\ ==\ \textcolor{charliteral}{'\(\backslash\)''}\ \&\&\ s[i+1]\ !=\ \textcolor{charliteral}{'\(\backslash\)''}\ \&\&\ (quoting\ ==\ \textcolor{keyword}{false}\ ||\ s[i-\/1]\ !=\ \textcolor{charliteral}{'\(\backslash\)''}))\ \{}
\DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \ \ \ \ quoting\ =\ !quoting;}
\DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」で囲まれた文字列でない場合、文字列末尾の「;」以降を削除\ */}}
\DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(quoting\ ==\ \textcolor{keyword}{false}\ \&\&\ s[i]\ ==\ \textcolor{charliteral}{';'})\ \{}
\DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \ \ \ \ s[i]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00068\ \ \ \ \ \}}
\DoxyCodeLine{00069\ \}}
\DoxyCodeLine{00070\ }
\DoxyCodeLine{00071\ \textcolor{keywordtype}{char}\ *strrev(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00072\ \{}
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ strdup\_chk(s,\ \textcolor{stringliteral}{"{}strrev.t"{}});}
\DoxyCodeLine{00074\ \ \ \ \ \textcolor{keywordtype}{int}\ l\ =\ strlen(t);}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ l-\/1-\/i;\ i++)\ \{}
\DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ tmp\ =\ t[i];}
\DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ t[i]\ =\ t[l-\/1-\/i];}
\DoxyCodeLine{00079\ \ \ \ \ \ \ \ \ t[l-\/1-\/i]\ =\ tmp;}
\DoxyCodeLine{00080\ \ \ \ \ \}}
\DoxyCodeLine{00081\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00082\ \}}
\end{DoxyCode}