%%
%% This package 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 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is Lars Madsen (daleif@imf.au.dk).
%%

% HISTORY
% 20060620 (daleif) package released
% 20060620 (daleif) a few \expandafter's could be saved, thanks MH for
%                   that comment
% 20060620 (daleif) removed dependency of calc package

\ProvidesPackage{digiconfigs}[2006/06/20 v0.5 by Lars 'daleif' Madsen]
\RequirePackage{amsmath}

\def\SCS@circ{o}
\def\SCS@bullet{.}
\def\SCS@blank{b}
\def\SCS@cdot{c}
\def\SCS@stop{X}
\newcommand\setCircleChar[1]{\def\SCS@circ{#1}}
\newcommand\setBulletChar[1]{\def\SCS@bullet{#1}}
\newcommand\setBlankChar[1]{\def\SCS@blank{#1}}
\newcommand\setCdotChar[1]{\def\SCS@cdot{#1}}


\def\SCS@circ@symbol{\circ}
\def\SCS@bullet@symbol{\bullet}
\def\SCS@cdot@symbol{\cdot}
\newcommand\setCircleSymbol[1]{\def\SCS@circ@symbol{#1}}
\newcommand\setBulletSymbol[1]{\def\SCS@bullet@symbol{#1}}
\newcommand\setCdotSymbol[1]{\def\SCS@cdot@symbol{#1}}


%\def\SCS@matrix@env{bmatrix}
%\newcommand\setMatrixEnv[1]{\def\SCS@matrix@env{#1}}

\def\SCS@matrix@start{\begin{pmatrix}}
\def\SCS@matrix@end{\end{pmatrix}}
\newcommand\setMatrixStart[1]{\def\SCS@matrix@start{#1}}
\newcommand\setMatrixEnd[1]{\def\SCS@matrix@end{#1}}



\newif\ifSCS@notdone
\newtoks\SCS@input
\newtoks\SCS@output
\newcounter{SCS@numtokens}
\newcounter{SCS@cols}
\newcounter{SCS@rows}

\def\SCS@token@Pop#1(into:)#2{%
  \edef\act{\noexpand\SCS@token@SplitOff\the#1%
    (head:)\noexpand#2(tail:)\noexpand#1}%
  \act}
\def\SCS@token@SplitOff#1#2(head:)#3(tail:)#4{\def#3{#1}#4={#2}}

\def\SCS@xsplit#1x#2\delim{\def\SCS@maxrows{#1}\def\SCS@maxcols{#2}}
\def\SCS@parseX#1x#2\END{
  \ifx\\#2\\% => ingen x
    \def\SCS@maxrows{#1}\def\SCS@maxcols{#1}
  \else
    \def\SCS@maxrows{#1} \SCS@parseXc #2 \fi}
\def\SCS@parseXc#1x{\def\SCS@maxcols{#1}}
\def\SCS@ParseArg#1{\SCS@parseX #1x\END}


\newcommand\SCS@config[1]{% #1 er en tokenlist
  \ifnum\value{SCS@numtokens}<\SCS@maxtokens%
    \SCS@token@Pop#1(into:)\first% tag f�rste token af, husk at ' ' ignoreres
    \ifx\first\SCS@circ%
      % hvis \first = \Mo (o) tilf�jes \circ
      \stepcounter{SCS@numtokens}%
      \stepcounter{SCS@cols}%
      \expandafter\addto@hook\expandafter\SCS@output\expandafter{\SCS@circ@symbol}%
    \else%
      \ifx\first\SCS@bullet%
        % hvis \first = \Mb (.) tilf�jes \bullet
        \stepcounter{SCS@numtokens}%
        \stepcounter{SCS@cols}%
        \expandafter\addto@hook\expandafter\SCS@output\expandafter{\SCS@bullet@symbol}%
      \else%
        \ifx\first\SCS@cdot%
          \stepcounter{SCS@numtokens}%
          \stepcounter{SCS@cols}%
          \expandafter\addto@hook\expandafter\SCS@output\expandafter{\SCS@cdot@symbol}%
        \else%
          \ifx\first\SCS@blank%
            % hvis \first = \Mbl (b for blank) skal der ikke inds�ttes
            % noget kun evt &-tegn 
            \stepcounter{SCS@numtokens}%
            \stepcounter{SCS@cols}%
            \relax%
          \else %
            % resten ignoreres
            \SCS@notdonefalse%
          \fi%
        \fi%
      \fi%
    \fi%
    \ifSCS@notdone%
      \ifnum\value{SCS@cols}<\SCS@maxcols%
        % tilf�j en & mellem punkterne i en r�kke
        \addto@hook\SCS@output{&}%
      \else%
        % cols=\maxcols => ny linie
        \setcounter{SCS@cols}{0}%
        \ifnum\value{SCS@rows}<\SCS@maxrows%
          \addto@hook\SCS@output{\\}%
        \fi%
      \stepcounter{SCS@rows}%
      \fi%
    \fi%
    \SCS@notdonetrue%
    \ifx\first\SCS@stop%
      \PackageWarning{stereoconfigs}{%
        String too short, expected \SCS@maxtokens\space
        chars but only found \theSCS@numtokens^^J
      }{}%
      \relax%
    \else%
      \SCS@config{#1}%
    \fi%
   \fi%
}

\def\SCS@default@size{4}
\newcommand\setDefaultMatrixSize[1]{\def\SCS@default@size{4}}

\newcommand\dconfig[2][\SCS@default@size]{%
  \begingroup%
  \SCS@ParseArg{#1}%
%  \setcounter{SCS@rows}{\SCS@maxrows * \SCS@maxcols}%
  \setcounter{SCS@rows}{\SCS@maxrows}
  \multiply \c@SCS@rows by \SCS@maxcols
  \edef\SCS@maxtokens{\theSCS@rows}%
  \setcounter{SCS@cols}{0}%
  \setcounter{SCS@rows}{0}%
  \setcounter{SCS@numtokens}{0}%
  \SCS@notdonetrue%
  \SCS@input\expandafter{#2X}%
  \SCS@config\SCS@input%
  \SCS@matrix@start%
    \the\SCS@output%
  \SCS@matrix@end%
  \endgroup%
}