%%
%% This is file `newfile.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% newfile.dtx  (with options: `outin')
%% 
%% -----------------------------------------------------------------
%%   Author: Peter Wilson, Herries Press
%%   Maintainer: Will Robertson (will dot robertson at latex-project dot org)
%%   Copyright 2002--2004 Peter R. Wilson
%% 
%%   This work may be distributed and/or modified under the
%%   conditions of the LaTeX Project Public License, either
%%   version 1.3c of this license or (at your option) any
%%   later version: <http://www.latex-project.org/lppl.txt>
%% 
%%   This work has the LPPL maintenance status "maintained".
%%   The Current Maintainer of this work is Will Robertson.
%% 
%%   This work consists of the files listed in the README file.
%% -----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{newfile}[2009/09/03 v1.0c Output and input files]
\RequirePackage{verbatim}
\newcommand{\newoutputstream}[1]{%
  \@ifundefined{#1outstre@m}%
    {\expandafter\newwrite\csname #1outstre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\PackageError{newfile}{Output stream #1 is already defined}{\@ehc}}}

\newcommand{\newinputstream}[1]{%
  \@ifundefined{#1instre@m}%
    {\expandafter\newread\csname #1instre@m\endcsname
     \csname newif\expandafter\endcsname
       \csname ifstre@m#1open\endcsname
     \global\csname stre@m#1openfalse\endcsname
     \expandafter\ifx\csname atstreamopen#1\endcsname\relax
       \global\@namedef{atstreamopen#1}{}%
     \fi
     \expandafter\ifx\csname atstreamclose#1\endcsname\relax
       \global\@namedef{atstreamclose#1}{}%
     \fi
    }%
    {\PackageError{newfile}{Input stream #1 is already defined}{\@ehc}}}

\newcommand{\@ifstre@mopen}[3]{%
  \csname ifstre@m#1open\endcsname#2\else#3\fi}
\newcommand{\instre@mandopen}[2]{%
  \@ifundefined{#1instre@m}{%
    \PackageError{newfile}{#1\space is not an input stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{#2}{%
    \PackageError{newfile}{Input stream #1\space is not open}{\@ehc}}}}

\newcommand{\instre@mandclosed}[2]{%
  \@ifundefined{#1instre@m}{%
    \PackageError{newfile}{#1\space is not an input stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{%
    \PackageError{newfile}{Input stream #1\space is open}{\@ehc}}{#2}}}

\newcommand{\outstre@mandopen}[2]{%
  \@ifundefined{#1outstre@m}{%
    \PackageError{newfile}{#1\space is not an output stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{#2}{%
    \PackageError{newfile}{Output stream #1\space is not open}{\@ehc}}}}

\newcommand{\outstre@mandclosed}[2]{%
  \@ifundefined{#1outstre@m}{%
    \PackageError{newfile}{#1\space is not an output stream}{\@ehc}}%
  {\@ifstre@mopen{#1}{%
    \PackageError{newfile}{Output stream #1\space is open}{\@ehc}}{#2}}}

\newcommand{\openoutputfile}[2]{%
  \outstre@mandclosed{#2}{%
    \global\@namedef{#1@filename}{#1}%
    \if@filesw
      \immediate\openout\@nameuse{#2outstre@m}=\@nameuse{#1@filename}%
    \fi
    \global\csname stre@m#2opentrue\endcsname%
    \@nameuse{atstreamopen#2}%
  }%
}

\newcommand{\closeoutputstream}[1]{%
  \outstre@mandopen{#1}{%
    \@nameuse{atstreamclose#1}%
    \immediate\closeout\@nameuse{#1outstre@m}%
    \global\csname stre@m#1openfalse\endcsname}%
}

\newcommand{\openinputfile}[2]{%
  \IfFileExists{#1}{%                   file exists
    \instre@mandclosed{#2}{%
      \@addtofilelist{#1}%
      \global\@namedef{#1@filename}{#1}%
      \immediate\openin\@nameuse{#2instre@m}=\@nameuse{#1@filename}%
      \global\csname stre@m#2opentrue\endcsname%
      \@nameuse{atstreamopen#2}}}%
  {%                                    file not found
    \PackageError{newfile}{Can't find file #1}{\@ehc}%
  }%
}

\newcommand{\closeinputstream}[1]{%
  \instre@mandopen{#1}{%
     \@nameuse{atstreamclose#1}%
     \immediate\closein\@nameuse{#1instre@m}%
     \global\csname stre@m#1openfalse\endcsname}%
}

\def\writeverbatim#1{%
  \@bsphack
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \verbatim@startline
  \verbatim@addtoline
  \verbatim@finish
  \def\verbatim@processline{%
    \immediate\write\@nameuse{#1outstre@m}{\the\verbatim@line}}%
  \verbatim@start}
\def\endwriteverbatim{\@esphack}

\newcommand{\addtostream}[2]{%
  \@bsphack
  \outstre@mandopen{#1}{%
    {\let\protect\string
     \immediate\write\@nameuse{#1outstre@m}{#2}%
    }}%
  \@esphack
}

\newif\ifstre@mnoteof
\newcommand{\checkstre@meof}[1]{%
  \stre@mnoteoftrue\ifeof\@nameuse{#1instre@m}\stre@mnoteoffalse\fi}

\def\readstream#1{
  \instre@mandopen{#1}{%
    \loop \checkstre@meof{#1} \ifstre@mnoteof
      \read\@nameuse{#1instre@m} to\temptokstre@m
     \temptokstre@m
    \repeat
    }%
}

\def\readaline#1{
  \instre@mandopen{#1}{%
    \ifeof\@nameuse{#1instre@m}
      \PackageWarning{newfile}{No more to read from stream #1}
    \else
      \read\@nameuse{#1instre@m} to\temptokstre@m
      \temptokstre@m
    \fi
    }%
}

\def\readverbatim{\begingroup
  \@ifstar{\stre@mverbatim@input\relax}%
          {\stre@mverbatim@input{\frenchspacing\@vobeyspaces}}}

\def\stre@mverbatim@input#1#2{%
  \@ifstre@mopen{#2}%
    {\@verbatim #1\relax
     \def\verbatim@in@stream{\@nameuse{#2instre@m}}
     \verbatim@readstre@m{#2}\endtrivlist\endgroup\@doendpe}%
    {\PackageError{newfile}{Stream #2 is not open}{\@ehc}\endgroup}%
}

\def\verbatim@readstre@m#1{%
  \verbatim@startline
  \expandafter\endlinechar\expandafter\m@ne
  \expandafter\verbatim@read@file
  \expandafter\endlinechar\the\endlinechar\relax
  \verbatim@finish
}

\newcommand{\plainvstream}{%
  \def\verbatim@processline{\the\verbatim@line\par}%
}

\newcounter{streamvline}
\newcommand{\streamvnumfont}[1]{\def\stre@mvnfont{#1}}
\streamvnumfont{\footnotesize}

\newcommand{\verbatimfont}[1]{%
  \def\verbatim@font{#1%
    \hyphenchar\font\m@ne
    \let\do\do@noligs
    \verbatim@nolig@list}}
\verbatimfont{\normalfont\ttfamily}

\newcommand{\numbervstream}{%
  \setcounter{streamvline}{0}%
  \def\verbatim@processline{%
    \addtocounter{streamvline}{1}%
    \leavevmode
    {\stre@mvnfont \thestreamvline}\space
    \the\verbatim@line\par}%
}

\newcommand{\marginnumbervstream}{%
  \setcounter{streamvline}{0}%
  \def\verbatim@processline{%
    \addtocounter{streamvline}{1}%
    \leavevmode
    \llap{{\stre@mvnfont \thestreamvline} \hskip\@totalleftmargin}
    \the\verbatim@line\par}%
}

\endinput
%%
%% End of file `newfile.sty'.
