\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}{long}\ str2l\_range(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str,\ \textcolor{keywordtype}{long}\ min,\ \textcolor{keywordtype}{long}\ max,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)\ \{}
\DoxyCodeLine{00004\ \ \ \ \ \textcolor{keywordtype}{char}\ *endptr\ =\ NULL;}
\DoxyCodeLine{00005\ \ \ \ \ \textcolor{keywordtype}{long}\ val\ =\ strtol(str,\ \&endptr,\ 10);}
\DoxyCodeLine{00006\ \ \ \ \ \textcolor{keywordflow}{if}(endptr\ ==\ str)\ \{}
\DoxyCodeLine{00007\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ Not\ specified.\(\backslash\)n"{}},\ name);}
\DoxyCodeLine{00008\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00009\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(*endptr\ !=\ \textcolor{charliteral}{'\(\backslash\)0'})\ \{}
\DoxyCodeLine{00010\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ \`{}\%s'\ is\ not\ integer.\(\backslash\)n"{}},\ name,\ str);}
\DoxyCodeLine{00011\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00012\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(val\ <\ min\ ||\ max\ <\ val)\ \{}
\DoxyCodeLine{00013\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(max\ ==\ LONG\_MAX)\ \{}
\DoxyCodeLine{00014\ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ \%s\ out\ of\ range:\ \%ld\ -\/\(\backslash\)n"{}},\ str,\ name,\ min);}
\DoxyCodeLine{00015\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00016\ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ \%s\ out\ of\ range:\ \%ld\ -\/\ \%ld\(\backslash\)n"{}},\ str,\ name,\ min,\ max);}
\DoxyCodeLine{00017\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00018\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00019\ \ \ \ \ \}}
\DoxyCodeLine{00020\ \ \ \ \ \textcolor{keywordflow}{return}\ val;}
\DoxyCodeLine{00021\ \}}
\DoxyCodeLine{00022\ }
\DoxyCodeLine{00023\ \textcolor{keywordtype}{void}\ *malloc\_chk(\textcolor{keywordtype}{size\_t}\ size,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00024\ \{}
\DoxyCodeLine{00025\ \ \ \ \ \textcolor{keywordtype}{void}\ *p\ =\ NULL;}
\DoxyCodeLine{00026\ }
\DoxyCodeLine{00027\ \ \ \ \ \textcolor{keywordflow}{if}((p\ =\ malloc(size))\ ==\ NULL)\ \{}
\DoxyCodeLine{00028\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ cannot\ allocate\ memory\(\backslash\)n"{}},\ tag);}
\DoxyCodeLine{00029\ \ \ \ \ \ \ \ \ exit(1);}
\DoxyCodeLine{00030\ \ \ \ \ \}}
\DoxyCodeLine{00031\ \ \ \ \ \textcolor{keywordflow}{return}\ memset(p,\ 0,\ size);}
\DoxyCodeLine{00032\ \}}
\DoxyCodeLine{00033\ }
\DoxyCodeLine{00034\ \textcolor{keywordtype}{void}\ *calloc\_chk(\textcolor{keywordtype}{size\_t}\ nmemb,\ \textcolor{keywordtype}{size\_t}\ size,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00035\ \{}
\DoxyCodeLine{00036\ \ \ \ \ \textcolor{keywordtype}{void}\ *p\ =\ NULL;}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \ \ \ \ \textcolor{keywordflow}{if}((p\ =\ calloc(nmemb,\ size))\ ==\ NULL)\ \{}
\DoxyCodeLine{00039\ \ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}\%s:\ cannot\ allocate\ memory\(\backslash\)n"{}},\ tag);}
\DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ exit(1);}
\DoxyCodeLine{00041\ \ \ \ \ \}}
\DoxyCodeLine{00042\ \ \ \ \ \textcolor{keywordflow}{return}\ p;}
\DoxyCodeLine{00043\ \}}
\DoxyCodeLine{00044\ }
\DoxyCodeLine{00045\ \textcolor{keywordtype}{char}\ *strdup\_chk(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tag)}
\DoxyCodeLine{00046\ \{}
\DoxyCodeLine{00047\ \ \ \ \ assert(s\ !=\ NULL);}
\DoxyCodeLine{00048\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ malloc\_chk(strlen(s)\ +\ 1,\ tag);}
\DoxyCodeLine{00049\ \ \ \ \ strcpy(t,\ s);}
\DoxyCodeLine{00050\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00051\ \}}
\DoxyCodeLine{00052\ }
\DoxyCodeLine{00053\ \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{00054\ \{}
\DoxyCodeLine{00055\ \ \ \ \ assert(s\ !=\ NULL);}
\DoxyCodeLine{00056\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ NULL;}
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \ \ \ \ \textcolor{keywordflow}{if}(len\ <\ strlen(s))\ \{}
\DoxyCodeLine{00059\ \ \ \ \ \ \ \ \ t\ =\ malloc\_chk(len\ +\ 1,\ tag);}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ strncpy(t,\ s,\ len);}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ t[len]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00062\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ t\ =\ strdup\_chk(s,\ tag);}
\DoxyCodeLine{00064\ \ \ \ \ \}}
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00066\ \}}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00068\ \textcolor{keywordtype}{void}\ strip\_end(\textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00069\ \{}
\DoxyCodeLine{00070\ \ \ \ \ \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{00071\ \ \ \ \ \ \ \ \ s[i]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00072\ \ \ \ \ \}}
\DoxyCodeLine{00073\ \}}
\DoxyCodeLine{00074\ }
\DoxyCodeLine{00075\ \textcolor{keywordtype}{void}\ strip\_casl2\_comment(\textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00076\ \{}
\DoxyCodeLine{00077\ \ \ \ \ \textcolor{keywordtype}{bool}\ quoting\ =\ \textcolor{keyword}{false};}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i\ =\ 0;\ s[i];\ i++)\ \{}
\DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」で囲まれた文字列の場合。「''」は無視\ */}}
\DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \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{00082\ \ \ \ \ \ \ \ \ \ \ \ \ quoting\ =\ !quoting;}
\DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」で囲まれた文字列でない場合、文字列末尾の「;」以降を削除\ */}}
\DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(quoting\ ==\ \textcolor{keyword}{false}\ \&\&\ s[i]\ ==\ \textcolor{charliteral}{';'})\ \{}
\DoxyCodeLine{00085\ \ \ \ \ \ \ \ \ \ \ \ \ s[i]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00086\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00087\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00088\ \ \ \ \ \}}
\DoxyCodeLine{00089\ \}}
\DoxyCodeLine{00090\ }
\DoxyCodeLine{00091\ \textcolor{keywordtype}{char}\ *strrev(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00092\ \{}
\DoxyCodeLine{00093\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ strdup\_chk(s,\ \textcolor{stringliteral}{"{}strrev.t"{}});}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordtype}{int}\ l\ =\ strlen(t);}
\DoxyCodeLine{00095\ }
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ l-\/1-\/i;\ i++)\ \{}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ tmp\ =\ t[i];}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ t[i]\ =\ t[l-\/1-\/i];}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ t[l-\/1-\/i]\ =\ tmp;}
\DoxyCodeLine{00100\ \ \ \ \ \}}
\DoxyCodeLine{00101\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00102\ \}}
\end{DoxyCode}