%%
%% This is file `csvtools.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% csvtools.dtx  (with options: `csvtools.sty,package')
%% Copyright (C) 2007 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         \~}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csvtools}[2007/07/03 v1.24 (NLCT)]
\RequirePackage{ifthen}
\RequirePackage{longtable}
\newcommand*{\@csvseparator}{,}
\newcommand*{\setcsvseparator}[1]{%
\renewcommand*{\@csvseparator}{#1}%
\construct@lopoff{#1}%
\@construct@qlopoff{#1}%
\@construct@lopoff{#1}}
\newcommand*{\extractentry}[2]{%
\expandafter\csvlopoff#1\to{#1}{#2}}
\edef\construct@lopoff#1{%
\noexpand\long\noexpand\def\noexpand\csvlopoff#1##1##2\noexpand\to##3##4{%
\noexpand\ifx"##1\noexpand\relax
\noexpand\@csv@qlopoff#1##1##2\noexpand\to##3##4\relax
\noexpand\else
\noexpand\@csv@lopoff#1##1##2\noexpand\to##3##4\relax
\noexpand\fi
}}
\edef\@construct@qlopoff#1{%
\noexpand\long\noexpand\def\noexpand\@csv@qlopoff#1"##1"#1##2\noexpand\to##3##4{%
\noexpand\def##4{##1}\noexpand\def##3{#1##2}}}
\edef\@construct@lopoff#1{%
\noexpand\long\noexpand\def\noexpand\@csv@lopoff#1##1#1##2\noexpand\to##3##4{%
\noexpand\def##4{##1}\noexpand\def##3{#1##2}}}
\setcsvseparator{,}
\newread\csvfile
\newif\ifmore
\newcount\c@field
\newcount\c@maxfields
\newcounter{csvlinenum}
\newcount\maxlines
\newcount\csvlastbutone
\newcounter{csvrownumber}
\toksdef\csv@tb=2
\newcommand{\field}[1]{\csname @field\romannumeral#1\endcsname}
\newcommand{\setcsvfieldlabel}[2]{%
\expandafter\def\csname @fieldlabel\romannumeral#1\endcsname{#2}}
\newcommand{\insertbyname}[1]{\csname insert#1\endcsname}
\newcommand{\trim}[1]{\def\@trmstr{}%
\def\csv@trmarg{#1}%
\if\par\csv@trmarg
\else
\expandafter\@trim#1\end
\let#1\@trmstr
\fi
}
\long\def\@trim#1 \end{\def\@trmstr{#1}}
\newcommand{\applyCSVfile}{\@ifstar\@sapplyCSVfile\@applyCSVfile}
\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}%
\csv@tb=\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field = 0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator} \and
\not\equal{\@csvlin@}{\@csvseparator\@csvseparator} \and
\not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter\xdef\csname @field\romannumeral\c@field\endcsname{%
\the\csv@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\relax
\PackageError{csvtools}{Header line required}{The optional
argument to `\string\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
\global\c@maxfields=0\relax
{\loop
\read\csvfile to\csvline
\trim{\csvline}%
\ifnum\c@csvlinenum=1\relax
\ifthenelse{\not\equal{\csvline}{\par} \and
\not\equal{\csvline}{}}{%
\csv@tb=\expandafter{\csvline}\relax
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field=0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\advance\c@field by 1\relax
\csv@tb=\expandafter{\param}\relax
\expandafter
\xdef\csname @fieldlabel\romannumeral\c@field\endcsname{\the\csv@tb}%
}%
\c@maxfields=\c@field
}{%
\PackageError{csvtools}{Header line missing in file #2}{Header
line required on line 1}}%
\fi
\c@field=0\relax
\whiledo{\c@field < \c@maxfields}{%
\advance\c@field by 1\relax
\expandafter\let\csname @field\romannumeral\c@field\endcsname\relax%
\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.}}{}%
\expandafter\let\csname insert\@fieldlabel\endcsname\relax%
}%
\advance\c@csvlinenum by 1\relax
\ifnum\c@csvlinenum>#1\relax
\csv@tb=\expandafter{\csvline}\relax
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field=0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}\relax
\advance\c@field by 1\relax
\expandafter
\xdef\csname @field\romannumeral\c@field\endcsname{\the\csv@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.}}{}%
\expandafter\xdef\csname insert\@fieldlabel\endcsname{\the\csv@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{\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
\csv@tb=\expandafter{\csvline}
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field = 0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}%
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}%
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter\xdef\csname insert\the\csv@tb\endcsname{%
\noexpand\field{\the\c@field}}%
}%
\fi
\csv@tb=\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field = 0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}
\advance\c@field by 1\relax
\expandafter
\xdef\csname @l\romannumeral\c@csvlinenum @field\romannumeral\c@field\endcsname{\the\csv@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
\csv@tb=\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field = 0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter
\xdef\csname insert\the\csv@tb\endcsname{%
\noexpand\field{\the\c@field}}%
}%
\fi
\csv@tb=\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\csv@tb\@csvseparator}%
\c@field = 0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator\par\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator\@csvseparator}
\and \not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\csv@tb=\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter
\xdef\csname @l\romannumeral\c@csvlinenum @field\romannumeral\c@field\endcsname{\the\csv@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]{%
\@ifnextchar[{\@csvSaveEntry{#1}{#2}}{%
\@csvSaveEntry{#1}{#2}[\insertbyname{#2}]}%
}
\def\@csvSaveEntry#1#2[#3]{%
\edef\@csv@tmp{\insertbyname{#2}}%
\if\relax\@csv@tmp\relax
\edef\@entry{#3}%
\else
\edef\@entry{\insertbyname{#2}}%
\fi
\expandafter
\xdef\csname #2\romannumeral\value{#1}\endcsname{\@entry}}
\newcommand{\csvGetEntry}[2]{%
\csname #2\romannumeral\value{#1}\endcsname}
\endinput
%%
%% End of file `csvtools.sty'.
