%%
%% This is file `csvtools.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% csvtools.dtx  (with options: `package')
%% Copyright (C) 2000 Nicola Talbot, all rights reserved.
%% If you modify this file, you must change its name first.
%% You are NOT ALLOWED to distribute this file alone. You are NOT
%% ALLOWED to take money for the distribution or use of either this
%% file or a changed version, except for a nominal charge for copying
%% etc.
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%\CheckSum{833}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csvtools}[2003/12/18 v1.0]
\RequirePackage{ifthen}
\RequirePackage{longtable}
\def\lop#1\to#2{\expandafter\lopoff#1\lopoff#1#2}
\long\def\lopoff,#1,#2\lopoff#3#4{\def#4{#1}\def#3{,#2}}
\newread\csvfile
\newif\ifmore
\newcount\c@field
\newcounter{csvlinenum}
\toksdef\ta=0 \toksdef\tb=2
\newcommand{\field}[1]{\csname @field\romannumeral#1\endcsname}
\newcommand{\insertbyname}[1]{\csname insert#1\endcsname}
\newcommand{\trim}[1]{\def\@trmstr{}\expandafter\@trim#1\end\edef#1{\@trmstr}}
\def\@trim#1 \end{\def\@trmstr{#1}}
\newcommand{\@sapplyCSVfile}[3][1]{%
\IfFileExists{#2}{%
\openin\csvfile=#2
\ifeof\csvfile\morefalse\else\moretrue\fi

\ifmore
{\c@csvlinenum=1\relax
\global\c@csvrownumber=0\relax
 \loop
 \read\csvfile to\csvline
 \advance\c@csvlinenum by 1\relax
 \ifnum\c@csvlinenum>#1\relax
   \trim{\csvline}
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname @field\romannumeral\c@field\endcsname{\the\tb}
   }
   \ifthenelse{\not\equal{\csvline}{\par} \and \not\equal{\csvline}{}}{%
   \refstepcounter{csvrownumber}\relax
   #3}{}
 \fi
 \ifeof\csvfile\morefalse\else\moretrue\fi
 \ifmore
 \repeat}
 \closein\csvfile
\fi
}{\PackageError{csvtools}{Can't find file '#2'}{}}
}
\newcommand{\@applyCSVfile}[3][2]{%
\ifnum#1<2
   \PackageError{csvtools}{Header line required}{The optional argument to `\protect\applyCSVfile'
needs to be > 1.  The header line should be on line 1}
\else
\IfFileExists{#2}{%
\openin\csvfile=#2
\ifeof\csvfile\morefalse\else\moretrue\fi
\ifmore
\global\c@csvlinenum=1\relax
\global\c@csvrownumber=0\relax
 {\loop
 \read\csvfile to\csvline
 \ifnum\c@csvlinenum=1\relax
   \ifthenelse{\not\equal{\csvline}{\par} \and \not\equal{\csvline}{}}{%
   \trim{\csvline}
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \advance\c@field by 1\relax
   \tb=\expandafter{\param}
   %\expandafter\xdef\csname insert\the\tb\endcsname{\field{\the\c@field}}
   \expandafter\xdef\csname @fieldlabel\romannumeral\c@field\endcsname{\the\tb}
   }
   }{\PackageError{csvtools}{Header line missing in file #2}{Header line required on line 1}}
 \fi
 \advance\c@csvlinenum by 1\relax
 \ifnum\c@csvlinenum>#1\relax
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname @field\romannumeral\c@field\endcsname{\the\tb}
   \edef\@fieldlabel{\csname @fieldlabel\romannumeral\c@field\endcsname}
   \ifthenelse{\equal{\@fieldlabel}{}
        \TE@or \equal{\@fieldlabel}{penalties}
        \TE@or \equal{\@fieldlabel}{byname}}{\PackageError{csvtools}{%
       \string\insert\@fieldlabel \space already defined}{%
       You can't have the label '\@fieldlabel' in your header row.}}{}\relax
   \expandafter\xdef\csname insert\@fieldlabel\endcsname{\the\tb}
   }
   \ifthenelse{\not\equal{\csvline}{\par} \and \not\equal{\csvline}{}}{%
   \global\advance\c@csvrownumber by 1\relax
   #3}{}
 \fi
 \ifeof\csvfile\morefalse\else\moretrue\fi
 \ifmore
 \repeat}
 \closein\csvfile
\fi
}{\PackageError{csvtools}{Can't find file '#2'}{}}
\fi
}
\newcommand{\applyCSVfile}{\@ifstar\@sapplyCSVfile\@applyCSVfile}
\newcount\maxlines
\newcount\csvlastbutone
\newcounter{csvrownumber}
\newcommand{\CSVtotabular}[5]{%
\openin\csvfile=#1
\c@csvlinenum=0\relax
 \loop
 \advance\c@csvlinenum by 1\relax
 \read\csvfile to\csvline
 \expandafter\xdef\csname @csvline\romannumeral\c@csvlinenum\endcsname{\csvline}
 \ifthenelse{\not\equal{\csvline}{\par}}{\trim{\csvline}}{}
 \ifnum\c@csvlinenum=1\relax
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname insert\the\tb\endcsname{\noexpand\field{\the\c@field}}
   }
 \fi
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname @l\romannumeral\c@csvlinenum @field\romannumeral\c@field\endcsname{\the\tb}
   }
 \ifeof\csvfile\morefalse\else\moretrue\fi
 \ifmore
 \repeat
\closein\csvfile
{\def\field##1{\csname @l\romannumeral\c@csvlinenum @field\romannumeral##1\endcsname}
\def\@r@wh{\begin{tabular}{#2}#3}
\def\@r@w{#4}
\def\@r@wl{#5\end{tabular}}
\maxlines=\c@csvlinenum
\advance\maxlines by -1\relax
\csvlastbutone=\maxlines
\advance\csvlastbutone by -1\relax
\c@csvlinenum=1\relax
\setcounter{csvrownumber}{0}\relax
\whiledo{\not{\c@csvlinenum>\maxlines}}{%
\ifthenelse{\expandafter\equal{\csname @csvline\romannumeral\c@csvlinenum\endcsname}{\par}}{\relax}{%
\ifnum\c@csvlinenum=1\relax
\@r@wh
\else
\refstepcounter{csvrownumber}%
\ifnum\c@csvlinenum=\maxlines\@r@wl\else\@r@w\fi
\fi}%
\global\advance\c@csvlinenum by 1\relax
}\relax
}}
\newcommand{\CSVtolongtable}[5]{%
\openin\csvfile=#1
\c@csvlinenum=0\relax
 \loop
 \advance\c@csvlinenum by 1\relax
 \read\csvfile to\csvline
 \expandafter\xdef\csname @csvline\romannumeral\c@csvlinenum\endcsname{\csvline}
 \ifthenelse{\not\equal{\csvline}{\par}}{\trim{\csvline}}{}
 \ifnum\c@csvlinenum=1\relax
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname insert\the\tb\endcsname{\noexpand\field{\the\c@field}}
   }
 \fi
   \tb=\expandafter{\csvline}
   \edef\@csvlin@{,\the\tb,}
   \c@field = 0\relax
   \whiledo{\not\equal{\@csvlin@}{,\par,} \and \not\equal{\@csvlin@}{,,} \and \not\equal{\@csvlin@}{,}}{%
   \lop\@csvlin@\to\param
   \tb=\expandafter{\param}
   \advance\c@field by 1\relax
   \expandafter\xdef\csname @l\romannumeral\c@csvlinenum @field\romannumeral\c@field\endcsname{\the\tb}
   }
 \ifeof\csvfile\morefalse\else\moretrue\fi
 \ifmore
 \repeat
\closein\csvfile
{\def\field##1{\csname @l\romannumeral\c@csvlinenum @field\romannumeral##1\endcsname}
\def\@r@wh{\begin{longtable}{#2}#3}
\def\@r@w{#4}
\def\@r@wl{#5\end{longtable}}
\maxlines=\c@csvlinenum
\advance\maxlines by -1\relax
\csvlastbutone=\maxlines
\advance\csvlastbutone by -1\relax
\c@csvlinenum=1\relax
\setcounter{csvrownumber}{0}\relax
\whiledo{\not{\c@csvlinenum>\maxlines}}{%
\ifthenelse{\expandafter\equal{\csname @csvline\romannumeral\c@csvlinenum\endcsname}{\par}}{\relax}{%
\ifnum\c@csvlinenum=1\relax
\@r@wh
\else
\refstepcounter{csvrownumber}%
\ifnum\c@csvlinenum=\maxlines\@r@wl\else\@r@w\fi
\fi}%
\global\advance\c@csvlinenum by 1\relax
}\relax
}}
\newcommand{\ifnextrowlast}[2]{\ifnum\c@csvlinenum=\csvlastbutone#1\else#2\fi}
\newcommand{\csvSaveEntry}[2][csvrownumber]{%
\edef\@entry{\insertbyname{#2}}%
\expandafter\xdef\csname #2\romannumeral\value{#1}\endcsname{\@entry}}

\newcommand{\csvGetEntry}[2]{%
\csname #2\romannumeral\value{#1}\endcsname}
\endinput
%%
%% End of file `csvtools.sty'.
