%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DataBase processing macros, version 0.91, December 2000
% Copyright Victor Eijkhout 2000
% file name: DB_process.tex
%
% Author:
% Victor Eijkhout
% Department of Computer Science
% University of Tennessee, Knoxville TN 37996
%
% victor@eijkhout.net
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the GNU General Public License, write to the 
% Free Software Foundation, Inc.,
% 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
% or find it on the net, for instance at
% http://www.gnu.org/copyleft/gpl.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Use this file as follows:
%
% \input DB_process
% \def\DBPprintline#1#2#3#4#5#6#7#8#9%
%     { < do whatever you want with the argument > }
% \DBPtabfile{ <input file> }  or  \DBPcommafile{ <input file> }
%
% The user macro \DBPprintline is applied to the max 9 arguments
% of each database line. You need to define the macro with 9 arguments;
% if the database has less than 9 fields, empty arguments are passed.
%
% The input records are counted, and the counter is available as \DBPno.
% To print this, you have to do \number\DBPno or somesuch.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% revision history:
% 0.9 first release, December 1998
% 0.91 documentation update, use repeat.tex for loop macro,
%      copyright notice, better handling of 'fragile' material
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%
%% prevent multiple loading of this file
%%
\expandafter\ifx\csname DBPfile\endcsname\relax
  \message{Loading DataBase Processing macros ... }%
\else
  \message{DataBase Processing macros already loaded ... }\endinput \fi

%%
%% Allocations
%%
\newread\DBPfile \newcount\DBPitem \newtoks\DBPtoks
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\def\cscsarg#1#2{\csname#1\expandafter\endcsname\csname#2\endcsname}
\cscsarg{newtoks}{DBPtoks1}
\cscsarg{newtoks}{DBPtoks2}
\cscsarg{newtoks}{DBPtoks3}
\cscsarg{newtoks}{DBPtoks4}
\cscsarg{newtoks}{DBPtoks5}
\cscsarg{newtoks}{DBPtoks6}
\cscsarg{newtoks}{DBPtoks7}
\cscsarg{newtoks}{DBPtoks8}
\cscsarg{newtoks}{DBPtoks9}

%%
%% Load auxiliary files: loop macro
%%
\input repeat.tex

%%
%% a default printline macro
%%
\def\DBPprintline#1#2#3#4#5#6#7#8#9{\number\DBPno: #1\par}

%%
%% general parsing macros
%%
\def\DBPparsefile{\def\DBPtestc{\par}%
    \repeat \for{DBPno} \do {\read\DBPfile to \DBPinput
      \ifx\DBPinput\DBPtestc \expandafter\breakrepeat \fi
      \DBPitem=1\relax \DBPtoks=\expandafter{\DBPinput}%
      \edef\DBPtemp{\noexpand\DBPparseline\the\DBPtoks
                    \DBPdelim DBP\DBPdelim}\DBPtemp
    }}
\def\DBPline#1#2{%
    \def\DBPtesta{#1}\def\DBPtestb{DBP}%
    \ifx\DBPtesta\DBPtestb
        \edef\DBPtemp{\noexpand\DBPprintline
            {\csarg\the{DBPtoks1}}%
            {\csarg\the{DBPtoks2}}%
            {\csarg\the{DBPtoks3}}%
            {\csarg\the{DBPtoks4}}%
            {\csarg\the{DBPtoks5}}%
            {\csarg\the{DBPtoks6}}%
            {\csarg\the{DBPtoks7}}%
            {\csarg\the{DBPtoks8}}%
            {\csarg\the{DBPtoks9}}%
            }\DBPtemp
        \csarg\relax{DBPtoks1}{}%
        \csarg\relax{DBPtoks2}{}%
        \csarg\relax{DBPtoks3}{}%
        \csarg\relax{DBPtoks4}{}%
        \csarg\relax{DBPtoks5}{}%
        \csarg\relax{DBPtoks6}{}%
        \csarg\relax{DBPtoks7}{}%
        \csarg\relax{DBPtoks8}{}%
        \csarg\relax{DBPtoks9}{}%
    \else \ifnum\DBPitem<10\relax
            \csname DBPtoks\number\DBPitem\endcsname={#1}%
            \else \message{DB Processor: can not handle >9 items/line}%
            \fi
          \advance\DBPitem by 1
          \expandafter#2\fi}

%%
%% macros for comma-delimited
%%
\def\DBPcommafile#1{%
    \openin\DBPfile=#1\relax
    \let\DBPparseline=\DBPcommaline
    \def\DBPdelim{,}%
    \DBPparsefile}
\def\DBPcommaline#1,{\DBPline{#1}\DBPcommaline}

%%
%% macros for tab-delimited
%%
\begingroup \catcode9=12
\gdef\DBPtabfile#1{%
    \openin\DBPfile=#1\relax
    \begingroup
      \catcode9=12 \let\DBPparseline=\DBPtabline
      \def\DBPdelim{^^I}%
      \DBPparsefile
    \endgroup}
\gdef\DBPtabline#1^^I{\DBPline{#1}\DBPtabline}
\endgroup

\endinput
