%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DataBase processing macros, version 0.9, December 1998
%
% 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.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\expandafter\ifx\csname DBPfile\endcsname\relax
  \message{Loading DataBase Processing macros ... }%
\else
  \message{DataBase Processing macros already loaded ... }\endinput \fi

\newread\DBPfile \newcount\DBPno \newcount\DBPitem

\long\def\DBloop#1\repeat{\long\def \DBbody {#1}\DBiterate}
\def\DBiterate{\let\DBnextloop\relax
    \DBbody \let\DBnextloop\DBiterate\fi \DBnextloop}

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

%% general parsing macros
\def\DBPparsefile{\DBPno=1
    \DBloop \read\DBPfile to \DBPinput
      \def\DBPtesta{\par}%
      \ifx\DBPinput\DBPtesta \else
          \DBPitem=1
          \edef\DBPtemp{\noexpand\DBPparseline\DBPinput
                        \DBPdelim DBP\DBPdelim}\DBPtemp
          \advance\DBPno by 1
      \repeat}
\def\DBPline#1#2{%
    \def\DBPtesta{#1}\def\DBPtestb{DBP}%
    \ifx\DBPtesta\DBPtestb
        \edef\DBPtemp{\noexpand\DBPprintline
            {\csname DBP1\endcsname}{\csname DBP2\endcsname}{\csname DBP3\endcsname}%
            {\csname DBP4\endcsname}{\csname DBP5\endcsname}{\csname DBP6\endcsname}%
            {\csname DBP7\endcsname}{\csname DBP8\endcsname}{\csname DBP9\endcsname}%
            }\DBPtemp
    \else \ifnum\DBPitem<10
            \expandafter\def\csname DBP\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
