\doxysection{word.\+c}
\label{word_8c_source}\index{src/word.c@{src/word.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00002\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00003\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ <ctype.h>}}
\DoxyCodeLine{00006\ }
\DoxyCodeLine{00007\ \textcolor{preprocessor}{\#include\ "{}word.h"{}}}
\DoxyCodeLine{00008\ \textcolor{preprocessor}{\#include\ "{}cerr.h"{}}}
\DoxyCodeLine{00009\ }
\DoxyCodeLine{00017\ WORD\ n2word(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str);}
\DoxyCodeLine{00018\ }
\DoxyCodeLine{00026\ WORD\ h2word(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str);}
\DoxyCodeLine{00027\ }
\DoxyCodeLine{00028\ WORD\ n2word(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str)}
\DoxyCodeLine{00029\ \{}
\DoxyCodeLine{00030\ \ \ \ \ assert(isdigit(str[0])\ ||\ str[0]\ ==\ \textcolor{charliteral}{'-\/'});}
\DoxyCodeLine{00031\ }
\DoxyCodeLine{00032\ \ \ \ \ \textcolor{keywordtype}{char}\ *check\ =\ NULL;}
\DoxyCodeLine{00033\ \ \ \ \ \textcolor{keywordtype}{int}\ n;}
\DoxyCodeLine{00034\ \ \ \ \ \textcolor{comment}{/*\ WORD値に変換\ */}}
\DoxyCodeLine{00035\ \ \ \ \ n\ =\ strtol(str,\ \&check,\ 10);}
\DoxyCodeLine{00036\ \ \ \ \ \textcolor{keywordflow}{if}(check[0])\ \{}
\DoxyCodeLine{00037\ \ \ \ \ \ \ \ \ setcerr(114,\ str);\ \ \ \ \textcolor{comment}{/*\ not\ integer\ */}}
\DoxyCodeLine{00038\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0x0;}
\DoxyCodeLine{00039\ \ \ \ \ \}}
\DoxyCodeLine{00040\ \ \ \ \ \textcolor{comment}{/*\ nが-\/32768から32767の範囲にないときは、その下位16ビットを格納\ */}}
\DoxyCodeLine{00041\ \ \ \ \ \textcolor{keywordflow}{if}(n\ <\ -\/32768\ ||\ n\ >\ 32767)\ \{}
\DoxyCodeLine{00042\ \ \ \ \ \ \ \ \ n\ \&=\ 0xFFFF;}
\DoxyCodeLine{00043\ \ \ \ \ \}}
\DoxyCodeLine{00044\ \ \ \ \ \textcolor{keywordflow}{return}\ (WORD)n;}
\DoxyCodeLine{00045\ \}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ WORD\ h2word(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str)}
\DoxyCodeLine{00048\ \{}
\DoxyCodeLine{00049\ \ \ \ \ assert(str[0]\ ==\ \textcolor{charliteral}{'\#'});}
\DoxyCodeLine{00050\ }
\DoxyCodeLine{00051\ \ \ \ \ WORD\ w\ =\ 0;}
\DoxyCodeLine{00052\ \ \ \ \ \textcolor{keywordtype}{char}\ *check\ =\ NULL;}
\DoxyCodeLine{00053\ \ \ \ \ str++;}
\DoxyCodeLine{00054\ \ \ \ \ \textcolor{keywordflow}{if}(*str\ ==\ \textcolor{charliteral}{'-\/'}\ ||\ strlen(str)\ >\ 4)\ \{}
\DoxyCodeLine{00055\ \ \ \ \ \ \ \ \ setcerr(116,\ str-\/1);\ \ \ \ \textcolor{comment}{/*\ out\ of\ hex\ range\ */}}
\DoxyCodeLine{00056\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0x0;}
\DoxyCodeLine{00057\ \ \ \ \ \}}
\DoxyCodeLine{00058\ \ \ \ \ \textcolor{comment}{/*\ WORD値に変換\ */}}
\DoxyCodeLine{00059\ \ \ \ \ w\ =\ (WORD)strtol(str,\ \&check,\ 16);}
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{keywordflow}{if}(check[0])\ \{}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ setcerr(115,\ str-\/1);\ \ \ \ \textcolor{comment}{/*\ not\ hex\ */}}
\DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0x0;}
\DoxyCodeLine{00063\ \ \ \ \ \}}
\DoxyCodeLine{00064\ \ \ \ \ \textcolor{keywordflow}{return}\ w;}
\DoxyCodeLine{00065\ \}}
\DoxyCodeLine{00066\ }
\DoxyCodeLine{00070\ \textcolor{keyword}{static}\ CERR\ cerr\_word[]\ =\ \{}
\DoxyCodeLine{00071\ \ \ \ \ \{\ 114,\ \textcolor{stringliteral}{"{}not\ integer"{}}\ \},}
\DoxyCodeLine{00072\ \ \ \ \ \{\ 115,\ \textcolor{stringliteral}{"{}not\ hex"{}}\ \},}
\DoxyCodeLine{00073\ \ \ \ \ \{\ 116,\ \textcolor{stringliteral}{"{}out\ of\ hex\ range"{}}\ \},}
\DoxyCodeLine{00074\ \};}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \textcolor{comment}{/*\ word.hで定義された関数群\ */}}
\DoxyCodeLine{00077\ \textcolor{keywordtype}{void}\ addcerrlist\_word()}
\DoxyCodeLine{00078\ \{}
\DoxyCodeLine{00079\ \ \ \ \ addcerrlist(ARRAYSIZE(cerr\_word),\ cerr\_word);}
\DoxyCodeLine{00080\ \}}
\DoxyCodeLine{00081\ }
\DoxyCodeLine{00082\ WORD\ nh2word(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str)}
\DoxyCodeLine{00083\ \{}
\DoxyCodeLine{00084\ \ \ \ \ WORD\ w;}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \ \ \ \ assert(\textcolor{keyword}{sizeof}(WORD)\ *\ 8\ ==\ 16);\ \textcolor{comment}{/*\ WORD型のサイズが16ビットであることを確認\ */}}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordflow}{if}(str\ ==\ NULL)\ \{}
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0x0;}
\DoxyCodeLine{00089\ \ \ \ \ \}}
\DoxyCodeLine{00090\ \ \ \ \ \textcolor{keywordflow}{if}(!isdigit(*str)\ \&\&\ *str\ !=\ \textcolor{charliteral}{'-\/'}\ \&\&\ *str\ !=\ \textcolor{charliteral}{'\#'})\ \{}
\DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ setcerr(114,\ str);\ \ \ \ \textcolor{comment}{/*\ not\ integer\ */}}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0x0;}
\DoxyCodeLine{00093\ \ \ \ \ \}}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordflow}{if}(*str\ ==\ \textcolor{charliteral}{'\#'})\ \{}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ w\ =\ h2word(str);}
\DoxyCodeLine{00096\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ w\ =\ n2word(str);}
\DoxyCodeLine{00098\ \ \ \ \ \}}
\DoxyCodeLine{00099\ \ \ \ \ \textcolor{keywordflow}{return}\ w;}
\DoxyCodeLine{00100\ \}}
\DoxyCodeLine{00101\ }
\DoxyCodeLine{00102\ \textcolor{keywordtype}{char}\ *word2n(WORD\ word)}
\DoxyCodeLine{00103\ \{}
\DoxyCodeLine{00104\ \ \ \ \ \textcolor{keyword}{enum}\ \{}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ MAXLEN\ =\ 5,\ \ \ \ \ \ \ \ \textcolor{comment}{/*\ WORD値を10進数で表したときの最大けた数\ */}}
\DoxyCodeLine{00106\ \ \ \ \ \};}
\DoxyCodeLine{00107\ \ \ \ \ \textcolor{keywordtype}{char}\ *s\ =\ malloc\_chk(MAXLEN\ +\ 1,\ \textcolor{stringliteral}{"{}word2n.n"{}});}
\DoxyCodeLine{00108\ \ \ \ \ \textcolor{keywordtype}{char}\ *t\ =\ NULL;}
\DoxyCodeLine{00109\ \ \ \ \ \textcolor{keywordtype}{int}\ d\ =\ 0;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ けた数\ */}}
\DoxyCodeLine{00110\ }
\DoxyCodeLine{00111\ \ \ \ \ \textcolor{keywordflow}{do}\{}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ s[d++]\ =\ word\ \%\ 10\ +\ \textcolor{charliteral}{'0'};}
\DoxyCodeLine{00113\ \ \ \ \ \}\ \textcolor{keywordflow}{while}((word\ /=\ 10)\ >\ 0);}
\DoxyCodeLine{00114\ \ \ \ \ s[d]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00115\ \ \ \ \ t\ =\ strrev(s);}
\DoxyCodeLine{00116\ \ \ \ \ FREE(s);}
\DoxyCodeLine{00117\ \ \ \ \ \textcolor{keywordflow}{return}\ t;}
\DoxyCodeLine{00118\ \}}
\DoxyCodeLine{00119\ }
\DoxyCodeLine{00120\ \textcolor{keywordtype}{char}\ *word2bit(\textcolor{keyword}{const}\ WORD\ word)}
\DoxyCodeLine{00121\ \{}
\DoxyCodeLine{00122\ \ \ \ \ \textcolor{keyword}{enum}\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ MAXLEN\ =\ 16,\ \ \ \ \ \ \ \ \textcolor{comment}{/*\ WORD値を2進数で表したときの最大桁数\ */}}
\DoxyCodeLine{00124\ \ \ \ \ \};}
\DoxyCodeLine{00125\ \ \ \ \ WORD\ mask\ =\ 0x8000;}
\DoxyCodeLine{00126\ \ \ \ \ \textcolor{keywordtype}{char}\ *bit\ =\ NULL;}
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordtype}{int}\ i\ =\ 0;}
\DoxyCodeLine{00128\ }
\DoxyCodeLine{00129\ \ \ \ \ bit\ =\ malloc\_chk(MAXLEN\ +\ 1,\ \textcolor{stringliteral}{"{}word2bit.bit"{}});}
\DoxyCodeLine{00130\ \ \ \ \ \textcolor{keywordflow}{do}\ \{}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ bit[i++]\ =\ (word\ \&\ mask)\ ?\ \textcolor{charliteral}{'1'}\ :\ \textcolor{charliteral}{'0'};}
\DoxyCodeLine{00132\ \ \ \ \ \}\ \textcolor{keywordflow}{while}((mask\ >>=\ 1)\ >\ 0);}
\DoxyCodeLine{00133\ \ \ \ \ bit[i]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{00134\ \ \ \ \ \textcolor{keywordflow}{return}\ bit;}
\DoxyCodeLine{00135\ \}}
\DoxyCodeLine{00136\ }
\DoxyCodeLine{00137\ \textcolor{keywordtype}{void}\ print\_dumpword(WORD\ word,\ \textcolor{keywordtype}{bool}\ logicalmode)}
\DoxyCodeLine{00138\ \{}
\DoxyCodeLine{00139\ \ \ \ \ \textcolor{keywordtype}{char}\ *bit\ =\ word2bit(word);}
\DoxyCodeLine{00140\ }
\DoxyCodeLine{00141\ \ \ \ \ \textcolor{keywordflow}{if}(logicalmode\ ==\ \textcolor{keyword}{true})\ \{}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\%6u"{}},\ word);}
\DoxyCodeLine{00143\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\%6d"{}},\ (\textcolor{keywordtype}{signed}\ \textcolor{keywordtype}{short})word);}
\DoxyCodeLine{00145\ \ \ \ \ \}}
\DoxyCodeLine{00146\ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\ =\ \#\%04X\ =\ \%s"{}},\ word,\ bit);}
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{comment}{/*\ 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示\ */}}
\DoxyCodeLine{00148\ \ \ \ \ \textcolor{keywordflow}{if}(word\ >=\ 0x20\ \&\&\ word\ <=\ 0x7E)\ \{}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\ =\ \(\backslash\)'\%c\(\backslash\)'"{}},\ word);}
\DoxyCodeLine{00150\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(word\ ==\ 0xA)\ \{}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\ =\ \(\backslash\)'\(\backslash\)\(\backslash\)n\(\backslash\)'"{}});}
\DoxyCodeLine{00152\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}(word\ ==\ \textcolor{charliteral}{'\(\backslash\)t'})\ \{}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ fprintf(stdout,\ \textcolor{stringliteral}{"{}\ =\ \(\backslash\)'\(\backslash\)\(\backslash\)t\(\backslash\)'"{}});}
\DoxyCodeLine{00154\ \ \ \ \ \}}
\DoxyCodeLine{00155\ \ \ \ \ FREE(bit);}
\DoxyCodeLine{00156\ \}}
\end{DoxyCode}