%%
%% This is file `csvmerge.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% csvmerge.dtx  (with options: `package')
%% This is a generated file. Copyright (C) 2019 by Laurence R Taylor
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3 of this license or (at your option) any later
%% version.  The latest version of this license is in:
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\ProvidesPackage{csvmerge}
      [2019/07/17 v1.0 merges TeX code with csv data]

\RequirePackage{stringstrings}
\RequirePackage{trimspaces}
\RequirePackage{etoolbox}

\DeclareOption*{\xdef\@LRT@CO{\CurrentOption}}
\ProcessOptions\relax

\makeatletter

\newcommand{\@LRT@mergeFields}[1]{%
\gdef\@LRT@csv{}%
\gdef\@LRT@tex{}%
\gdef\@LRT@tmp{}%
\setcounter{\@LRT@macroPrefix Row}{2}\relax%
\gdef\@LRT@csvBlankLine{}%
\@LRT@getFilePaths#1=@%
\if@LRT@empty{\@LRT@csv}{\@LRT@missingArgumentMessage{csv}}{}%
\if@LRT@empty{\@LRT@tex}{\@LRT@missingArgumentMessage{tex}}{}%
\if@LRT@empty{\@LRT@tmp}%
{\global\let\@LRT@Input\@LRT@streaming}%
{\global\let\@LRT@Input\@LRT@storage}%
\if@LRT@empty{\@LRT@tmp}{}{%
\immediate\openout\@lrt@tmpFile=\@LRT@tmp\relax}%
\@LRT@mergeData%
\if@LRT@empty{\@LRT@tmp}{}{\immediate\closeout\@lrt@tmpFile\relax}%
}

\gdef\@LRT@macroPrefix{@LRT@@}

\def\@LRT@Field#1{%
\csname\@LRT@macroPrefix#1\endcsname%
}

\long\gdef\@LRT@ifFieldEmpty#1#2#3{\ifbool{\@LRT@macroPrefix#1}{#2}{#3}}

{{%
\gdef\@LRT@setDelimitersCommaQuote{
\gdef\@LRT@fieldDelimiterBack{,}
\gdef\@LRT@getFieldDelimited##1,{\@LRT@getFirstArg{##1}}
\gdef\@LRT@textDelimiterFront{"}
\gdef\@LRT@getTextDelimited"##1",{\@LRT@getFirstArg{##1}}
\gdef\@LRT@blankFieldItem{,}
\gdef\@LRT@catcodes{}
}%
}}

{{\catcode`\^^I12\relax%
\gdef\@LRT@setDelimitersTabQuote{
\gdef\@LRT@fieldDelimiterBack{^^I}
\gdef\@LRT@getFieldDelimited##1^^I{\@LRT@getFirstArg{##1}}
\gdef\@LRT@textDelimiterFront{"}
\gdef\@LRT@getTextDelimited"##1"^^I{\@LRT@getFirstArg{##1}}
\gdef\@LRT@blankFieldItem{}
\gdef\@LRT@catcodes{\catcode`\^^I12\relax}
}%
}}

\expandafter\gdef\csname1238LRTLRTsvbneLRT\endcsname{@LRT@}
\makeatother
\expandafter\gdef\csname\csname1238LRTLRTsvbneLRT\endcsname
makeMePublic\endcsname\@LRT@#1\becomes#2{\global\letcs{#2}{@LRT@#1}}%
\makeatletter

\xdef\@LRT@Row{\noexpand\number\noexpand\value{\@LRT@macroPrefix Row}}

\newcommand{\if@LRT@empty}[3]{\ifthenelse{\equal{#1}{}}{#2}{#3}}

\gdef\@LRT@fieldName#1{%
\caselower[q]{#1}%
\convertchar[q]{\thestring}{ }{}%
\convertchar[q]{\thestring}{,}{}%
\xdef\@LRT@TheString{\thestring}%
}

\gdef\@LRT@getFilePaths#1=#2@{%
\xdef\@LRT@hashKey{\trim@spaces{#1}}%
\if@LRT@empty{\@LRT@hashKey}{}{
\def\@LRT@test{tex}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
{\def\@LRT@test{csv}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
{\def\@LRT@test{tmp}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
{\@LRT@unknownArgumentMessage{#1}}}}%
\@LRT@extractKeyValue#2@%
\expandafter\@LRT@getFilePaths\@LRT@doWeStillHaveStuffToDo @}}

\def\@LRT@extractKeyValue#1#2@{%
\edef\@LRT@hashValue{\trim@spaces{#1}}%
\xdef\@LRT@doWeStillHaveStuffToDo{\trim@spaces{#2}}%
\expandafter\xdef\csname @LRT@\@LRT@hashKey\endcsname{\@LRT@hashValue}%
}

\newcommand{\@LRT@SecondifFieldEmpty}[3]{%
\edef\@LRT@fieldValue{#1}%
\ifx\@LRT@fieldValue\@empty\relax{#2}\else{#3}\fi\relax}

\gdef\@LRT@getCommaFieldDelimited#1,{\@LRT@getFirstArg{#1}}

\newcounter{\@LRT@macroPrefix Row}

\newread\@lrt@csvFile%
\newread\@lrt@texFile%
\newwrite\@lrt@tmpFile%

\gdef\@LRT@justProcessedMessage{%
\immediate\typeout{}%
\immediate\typeout{Just processed row %
\@LRT@Row\space of the data file.}%
\immediate\typeout{}%
\stepcounter{\@LRT@macroPrefix Row}\relax%
}

\gdef\@LRT@failedToOpenMessage#1{%
\immediate\typeout{}%
\immediate\typeout{File #1 failed to open.}%
\immediate\typeout{}\fi%
\end{document}%
}

\gdef\@LRT@NoMoreDataMessage{%
\immediate\typeout{No more data.}%
\immediate\typeout{}%
}

\gdef\@LRT@missingArgumentMessage#1{
\immediate\typeout{}%
\immediate\typeout{I was never given a path to the #1 file.}%
\immediate\typeout{}%
\end{document}%
}

\gdef\@LRT@unknownArgumentMessage#1{
\immediate\typeout{}%
\immediate\typeout{I do not recognize an argument of type #1.}%
\immediate\typeout{}%
}

\gdef\@LRT@streaming{\input{\@LRT@tex}}

\gdef\@LRT@catcodeMagic{%
\catcode`\^^M9\relax%
\catcode`\#12\relax
\catcode`\$12\relax
\catcode`\&12\relax
\catcode`\^12\relax
\catcode`\_12\relax
\catcode`\%12\relax
}

\gdef\@LRT@texNotAtEOF#1{%
\ifeof\@lrt@texFile{\global\booltrue{@LRT@texEOF}}\else{#1}\fi%
}

\gdef\@LRT@storage{{%
\@LRT@catcodeMagic%
\openin\@lrt@texFile=\@LRT@tex\relax%
\@LRT@texNotAtEOF{\global\boolfalse{@LRT@texEOF}}%
\unlessboolexpr{bool{@LRT@texEOF}}{%
\read\@lrt@texFile to \@LRT@preItem%
\@LRT@texNotAtEOF{%
\immediate\write\@lrt@tmpFile{\@LRT@preItem}\relax}%
}{}%
\immediate\closein\@lrt@texFile%
}}

\gdef\@LRT@dataLineIterator{%
\expandafter\@ifnextchar\@LRT@textDelimiterFront%
{\@LRT@getTextDelimited}{\@LRT@getFieldDelimited}}

\gdef\@LRT@getFirstArg#1{\gdef\@LRT@firstArg{#1}\@LRT@getSecondArg}
\gdef\@LRT@getSecondArg#1@{\gdef\@LRT@restArg{#1}}

\gdef\@LRT@getLOFirstArg#1{\gdef\@LRT@ffirstArg{#1}\@LRT@getLOSecondArg}
\gdef\@LRT@getLOSecondArg#1@{\gdef\@LRT@frestArg{#1}}

\gdef\@LRT@fieldNameIterator#1,{\@LRT@getLOFirstArg{#1}}

\providebool{@LRT@iterator}
\gdef\@LRT@setFirstLineAndBlankLine#1{%
\gdef\@lrt@firstline{}%
\gdef\@LRT@csvBlankLine{}
\edef\@LRT@restArg{#1\@LRT@fieldDelimiterBack}
\booltrue{@LRT@iterator}%
\whileboolexpr{bool{@LRT@iterator}}{%
\expandafter\@LRT@dataLineIterator\@LRT@restArg @%
\@LRT@fieldName{\@LRT@firstArg}%
\xdef\@lrt@firstline{\@lrt@firstline\@LRT@TheString,}%
\if@LRT@empty{\@LRT@restArg}%
{\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
\ifboolexpr{bool{@LRT@iterator}}%
{\xdef\@LRT@csvBlankLine{\@LRT@csvBlankLine\@LRT@blankFieldItem}}{}%
}{}%
}

\gdef\@LRT@getAndProcessNextLine{%
\unlessboolexpr{bool{@LRT@csvEOF}}{%
{{%
\@LRT@catcodeMagic%
\@LRT@catcodes
\read\@lrt@csvFile to\@LRT@x@nextLine%
\ifeof\@lrt@csvFile\relax{\global\booltrue{@LRT@csvEOF}}\else%
{\global\boolfalse{@LRT@csvEOF}}\fi%
\xdef\@LRT@nextLine{\@LRT@x@nextLine}%
}}%
{{%
\if@LRT@empty{\@LRT@nextLine}%
{\global\let\@LRT@next\@LRT@exitGetAndProcessNextLine\relax}%
{\ifx\@LRT@csvBlankLine\@LRT@nextLine\relax%
{\global\let\@LRT@next\@LRT@exitGetAndProcessNextLine\relax}\else%
{\global\let\@LRT@next\@LRT@repeatGetAndProcessNextLine\relax}\fi}%
\@LRT@next%
}}%
}{}%
}

\gdef\@LRT@exitGetAndProcessNextLine{%
\closein\@lrt@csvFile\relax\global\booltrue{@LRT@csvEOF}}

\gdef\@LRT@repeatGetAndProcessNextLine{%
\@LRT@processNextLine{\@LRT@nextLine\@LRT@fieldDelimiterBack}}

\gdef\@LRT@setMacrosForOneEntry#1#2{%
\expandafter\xdef\csname\@LRT@macroPrefix#1\endcsname{#2}%
\@LRT@SecondifFieldEmpty{#2}%
{\global\booltrue{\@LRT@macroPrefix#1}}%
{\global\boolfalse{\@LRT@macroPrefix#1}}%
}

\gdef\@LRT@processNextLine#1{%
\edef\@LRT@restArg{#1}%
\edef\@LRT@frestArg{\@lrt@firstline}%
\booltrue{@LRT@iterator}%
\whileboolexpr{bool{@LRT@iterator}}{%
\expandafter\@LRT@fieldNameIterator\@LRT@frestArg @%
\edef\@LRT@restArgA{\@LRT@restArg\@LRT@fieldDelimiterBack}
\expandafter\@LRT@dataLineIterator\@LRT@restArgA @%
\@LRT@setMacrosForOneEntry{\@LRT@ffirstArg}{\@LRT@firstArg}%
\if@LRT@empty{\@LRT@frestArg}%
{\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
}{}%
\@LRT@Input\relax%
\@LRT@justProcessedMessage%
}

\gdef\@LRT@mergeData{%
\providebool{@LRT@texEOF}%
\immediate\openin\@lrt@csvFile=\@LRT@csv\relax%
\providebool{@LRT@csvEOF}%
\boolfalse{@LRT@csvEOF}%
\ifeof\@lrt@csvFile\@LRT@failedToOpenMessage{\@LRT@csv}\fi%
{{\@LRT@catcodes
\read\@lrt@csvFile to\@lrt@firstlineX%
\@LRT@setFirstLineAndBlankLine{\@lrt@firstlineX}
}}%
\booltrue{@LRT@iterator}
\edef\@LRT@restArg{\@lrt@firstline}%
\whileboolexpr{bool{@LRT@iterator}}{%
\expandafter\@LRT@getCommaFieldDelimited\@LRT@restArg @%
\providebool{\@LRT@macroPrefix\@LRT@firstArg}%
\if@LRT@empty{\@LRT@restArg}%
{\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
}{}%
\@LRT@getAndProcessNextLine%
\@LRT@NoMoreDataMessage%
}

\gdef\@LRT@weirdOption#1{\@ifundefined{@LRT@CO}%
{\global\csletcs{#1}{@LRT@#1}}{{\global\csletcs{\@LRT@CO #1}{@LRT@#1}}}%
}

\@LRT@weirdOption{mergeFields}%
\@LRT@weirdOption{Field}%
\@LRT@weirdOption{ifFieldEmpty}%
\@LRT@weirdOption{setDelimitersCommaQuote}%
\@LRT@weirdOption{setDelimitersTabQuote}%
\@LRT@weirdOption{makeMePublic}%

\@LRT@setDelimitersCommaQuote

\makeatother
\endinput
%%
%% End of file `csvmerge.sty'.
