%%
%% This is file `l3io.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3io.dtx  (with options: `package')
%% 
%% EXPERIMENTAL CODE
%% 
%% Do not distribute this file without also distributing the
%% source files specified above.
%% 
%% Do not distribute a modified version of this file.
%% 
%% File: l3io.dtx Copyright (C) 1990-2011 The LaTeX3 Project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "expl3 bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX3 Project.
%%
%% -----------------------------------------------------------------------
\RequirePackage{l3names}
\GetIdInfo$Id: l3io.dtx 2595 2011-08-16 00:58:34Z bruno $
  {L3 Experimental input-output operations}
\ProvidesExplPackage
  {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
\package_check_loaded_expl:
\cs_new_eq:NN \if_eof:w \tex_ifeof:D
\cs_new_eq:NN \c_iow_term_stream \c_sixteen
\cs_new_eq:NN \c_ior_term_stream \c_sixteen
\cs_new_eq:NN \c_iow_log_stream  \c_minus_one
\cs_new_eq:NN \c_ior_log_stream  \c_minus_one
\tl_const:Nn \c_iow_streams_tl
  {
    \c_zero
    \c_one
    \c_two
    \c_three
    \c_four
    \c_five
    \c_six
    \c_seven
    \c_eight
    \c_nine
    \c_ten
    \c_eleven
    \c_twelve
    \c_thirteen
    \c_fourteen
    \c_fifteen
  }
\cs_new_eq:NN \c_ior_streams_tl \c_iow_streams_tl
\prop_new:N \g_iow_streams_prop
\prop_new:N \g_ior_streams_prop
\prop_put:Nnn \g_iow_streams_prop { 0 } { LaTeX2e~reserved }
\prop_put:Nnn \g_iow_streams_prop { 1 } { LaTeX2e~reserved }
\prop_put:Nnn \g_iow_streams_prop { 2 } { LaTeX2e~reserved }
\prop_put:Nnn \g_ior_streams_prop { 0 } { LaTeX2e~reserved }
\int_new:N \l_iow_stream_int
\cs_new_eq:NN \l_ior_stream_int \l_iow_stream_int
\cs_set_eq:NN \iow_raw_new:N \newwrite
\cs_set_eq:NN \ior_raw_new:N \newread
\cs_generate_variant:Nn \ior_raw_new:N { c }
\cs_generate_variant:Nn \iow_raw_new:N { c }
\cs_new_protected_nopar:Npn \ior_open:Nn #1#2
  {
    \ior_close:N #1
    \int_set:Nn \l_ior_stream_int \c_sixteen
    \tl_map_function:NN \c_ior_streams_tl \ior_alloc_read:n
    \int_compare:nNnTF \l_ior_stream_int = \c_sixteen
      { \msg_kernel_error:nn { ior } { streams-exhausted } }
      {
        \ior_stream_alloc:N #1
        \prop_gput:NVn \g_ior_streams_prop \l_ior_stream_int {#2}
        \tex_openin:D #1#2 \scan_stop:
      }
  }
\cs_new_protected_nopar:Npn \iow_open:Nn #1#2
  {
    \iow_close:N #1
    \int_set:Nn \l_iow_stream_int \c_sixteen
    \tl_map_function:NN \c_iow_streams_tl \iow_alloc_write:n
    \int_compare:nNnTF \l_iow_stream_int = \c_sixteen
      { \msg_kernel_error:nn { iow } { streams-exhausted } }
      {
        \iow_stream_alloc:N #1
        \prop_gput:NVn \g_iow_streams_prop \l_iow_stream_int {#2}
        \tex_immediate:D \tex_openout:D #1#2 \scan_stop:
      }
  }
\cs_generate_variant:Nn \ior_open:Nn { c }
\cs_generate_variant:Nn \iow_open:Nn { c }
\cs_new_protected_nopar:Npn \iow_alloc_write:n #1
  {
    \prop_if_in:NnF \g_iow_streams_prop {#1}
      {
        \int_set:Nn \l_iow_stream_int {#1}
        \tl_map_break:
      }
  }
\cs_new_protected_nopar:Npn \ior_alloc_read:n #1
  {
    \prop_if_in:NnF \g_iow_streams_prop {#1}
      {
        \int_set:Nn \l_ior_stream_int {#1}
        \tl_map_break:
      }
  }
\cs_new_protected_nopar:Npn \iow_stream_alloc:N #1
  {
    \cs_if_exist:cTF { g_iow_ \int_use:N \l_iow_stream_int _stream }
      { \cs_gset_eq:Nc #1 { g_iow_ \int_use:N \l_iow_stream_int _stream } }
      {
        \iow_stream_alloc_aux:
        \int_compare:nNnT \l_iow_stream_int = \c_sixteen
          {
            \iow_raw_new:N \g_iow_tmp_stream
            \int_set:Nn \l_iow_stream_int { \g_iow_tmp_stream }
            \cs_gset_eq:cN
              { g_iow_ \int_use:N \l_iow_stream_int _stream }
              \g_iow_tmp_stream
          }
        \cs_gset_eq:Nc #1 { g_iow_ \int_use:N \l_iow_stream_int _stream }
      }
  }
\cs_new_protected_nopar:Npn \iow_stream_alloc_aux:
  {
    \int_incr:N \l_iow_stream_int
    \int_compare:nNnT \l_iow_stream_int < \c_sixteen
      {
         \cs_if_exist:cTF { g_iow_ \int_use:N \l_iow_stream_int _stream }
           {
             \prop_if_in:NVT \g_iow_streams_prop \l_iow_stream_int
               { \iow_stream_alloc_aux: }
           }
           { \iow_stream_alloc_aux: }
      }
  }
\cs_new_protected_nopar:Npn \ior_stream_alloc:N #1
  {
    \cs_if_exist:cTF { g_ior_ \int_use:N \l_ior_stream_int _stream }
      { \cs_gset_eq:Nc #1 { g_ior_ \int_use:N \l_ior_stream_int _stream } }
      {
        \ior_stream_alloc_aux:
        \int_compare:nNnT \l_ior_stream_int = \c_sixteen
          {
            \ior_raw_new:N \g_ior_tmp_stream
            \int_set:Nn \l_ior_stream_int { \g_ior_tmp_stream }
            \cs_gset_eq:cN
              { g_ior_ \int_use:N \l_iow_stream_int _stream }
              \g_ior_tmp_stream
          }
        \cs_gset_eq:Nc #1 { g_ior_ \int_use:N \l_ior_stream_int _stream }
      }
  }
\cs_new_protected_nopar:Npn \ior_stream_alloc_aux:
  {
    \int_incr:N \l_ior_stream_int
    \int_compare:nNnT \l_ior_stream_int < \c_sixteen
      {
         \cs_if_exist:cTF { g_ior_ \int_use:N \l_ior_stream_int _stream }
           {
             \prop_if_in:NVT \g_ior_streams_prop \l_ior_stream_int
               { \ior_stream_alloc_aux: }
           }
           { \ior_stream_alloc_aux: }
      }
  }
\cs_new_protected_nopar:Npn \ior_close:N #1
  {
    \cs_if_exist:NT #1
      {
        \int_compare:nNnF #1 = \c_minus_one
          {
            \tex_closein:D #1
            \prop_gdel:NV \g_ior_streams_prop #1
            \cs_undefine:N #1
          }
      }
  }
\cs_new_protected_nopar:Npn \iow_close:N #1
  {
    \cs_if_exist:NT #1
      {
        \int_compare:nNnF #1 = \c_minus_one
          {
            \tex_immediate:D \tex_closeout:D #1
            \prop_gdel:NV \g_iow_streams_prop #1
            \cs_undefine:N #1
          }
      }
  }
\cs_generate_variant:Nn \ior_close:N { c }
\cs_generate_variant:Nn \iow_close:N { c }
\cs_new_protected_nopar:Npn \ior_list_streams:
  {
    \prop_if_empty:NTF \g_ior_streams_prop
      {
        \iow_term:x { No~input~streams~are~open }
        \tl_show:n { }
      }
      {
        \iow_term:x { The~following~input~streams~are~in~use: }
        \tl_set:Nx \l_prop_show_tl
          { \prop_map_function:NN \g_ior_streams_prop \ior_show_aux:nn }
          \etex_showtokens:D \exp_after:wN \exp_after:wN \exp_after:wN
            { \exp_after:wN \prop_show_aux:w \l_prop_show_tl }
      }
  }
\cs_new:Npn \ior_show_aux:nn #1#2
  {
    \iow_newline: > \c_space_tl \c_space_tl
    #1 \iow_char:N
    \c_space_tl \c_space_tl => \c_space_tl \c_space_tl
    \exp_not:n {#2}
  }
\cs_new_protected_nopar:Npn \iow_list_streams:
  {
    \prop_if_empty:NTF \g_iow_streams_prop
      {
        \iow_term:x { No~output~streams~are~open }
        \tl_show:n { }
      }
      {
        \iow_term:x { The~following~output~streams~are~in~use: }
        \tl_set:Nx \l_prop_show_tl
          { \prop_map_function:NN \g_iow_streams_prop \iow_show_aux:nn }
          \etex_showtokens:D \exp_after:wN \exp_after:wN \exp_after:wN
            { \exp_after:wN \prop_show_aux:w \l_prop_show_tl }
      }
  }
\cs_new_eq:NN \iow_show_aux:nn \ior_show_aux:nn
\msg_kernel_new:nnnn { iow } { streams-exhausted }
  { Output~streams~exhausted }
  {
    TeX~can~only~open~up~to~16~output~streams~at~one~time.\\
    All~16 are currently~in~use,~and~something~wanted~to~open
    another~one.
  }
\msg_kernel_new:nnnn { ior } { streams-exhausted }
  { Input~streams~exhausted }
  {
    TeX~can~only~open~up~to~16~input~streams~at~one~time.\\
    All~16 are currently~in~use,~and~something~wanted~to~open
    another~one.
  }
\cs_new_eq:NN \iow_shipout_x:Nn \tex_write:D
\cs_generate_variant:Nn \iow_shipout_x:Nn { Nx }
\cs_new_protected_nopar:Npn \iow_shipout:Nn #1#2
  { \iow_shipout_x:Nn #1 { \exp_not:n {#2} } }
\cs_generate_variant:Nn \iow_shipout:Nn { Nx }
\cs_new_protected_nopar:Npn \iow_now:Nx { \tex_immediate:D \iow_shipout_x:Nn }
\cs_new_protected_nopar:Npn \iow_now:Nn #1#2
  { \iow_now:Nx #1 { \exp_not:n {#2} } }
\cs_set_protected_nopar:Npn \iow_log:x  { \iow_now:Nx \c_iow_log_stream  }
\cs_new_protected_nopar:Npn \iow_log:n  { \iow_now:Nn \c_iow_log_stream  }
\cs_set_protected_nopar:Npn \iow_term:x { \iow_now:Nx \c_iow_term_stream }
\cs_new_protected_nopar:Npn \iow_term:n { \iow_now:Nn \c_iow_term_stream }
\cs_new_protected_nopar:Npn \iow_now_when_avail:Nn #1
  { \cs_if_free:NTF #1 { \use_none:n } { \iow_now:Nn #1 } }
\cs_new_protected_nopar:Npn \iow_now_when_avail:Nx #1
  { \cs_if_free:NTF #1 { \use_none:n } { \iow_now:Nx #1 } }
\int_new:N  \l_iow_line_length_int
\int_set:Nn \l_iow_line_length_int { 78 }
\int_new:N \l_iow_target_length_int
\int_new:N \l_iow_current_line_int
\int_new:N \l_iow_current_word_int
\tl_new:N \l_iow_current_line_tl
\tl_new:N \l_iow_current_word_tl
\tl_new:N \l_iow_wrap_tl
\tl_new:N \l_iow_wrapped_tl
\quark_new:N \q_iow_stop
\bool_new:N \l_iow_line_start_bool
\cs_new_protected:Npn \iow_wrap:xnnnN #1#2#3#4#5
  {
    \group_begin:
      \int_set:Nn \l_iow_target_length_int { \l_iow_line_length_int - ( #3 ) }
      \int_zero:N \l_iow_current_line_int
      \tl_clear:N \l_iow_current_line_tl
      \tl_clear:N \l_iow_wrap_tl
      \bool_set_true:N \l_iow_line_start_bool
      \cs_set:Npx \\ { \c_space_tl \iow_newline: \c_space_tl }
      \cs_set_eq:NN \  \c_space_tl
      #4
      \protected@edef \l_iow_wrap_tl {#1}
      \cs_set:Npn \\ { \iow_newline: #2 }
      \use:x
        {
          \exp_not:N \iow_wrap_loop:w
          \tl_to_str:N \l_iow_wrap_tl \c_space_tl
          \exp_not:N \q_iow_stop \c_space_tl
        }
    \exp_args:NNo \group_end:
    #5 \l_iow_wrapped_tl
  }
\cs_new_protected:Npn \iow_wrap_loop:w #1 ~ %
  {
    \tl_set:Nn \l_iow_current_word_tl {#1}
    \tl_if_eq:NNTF \l_iow_current_word_tl \iow_newline:
      { \iow_wrap_newline: }
      {
        \tl_if_eq:NNTF \l_iow_current_word_tl \q_iow_stop
          { \iow_wrap_end: }
          { \iow_wrap_word: }
      }
  }
\cs_new_protected_nopar:Npn \iow_wrap_word:
  {
    \int_set:Nn \l_iow_current_word_int
      { \str_length_skip_spaces:N \l_iow_current_word_tl }
    \int_add:Nn \l_iow_current_line_int { \l_iow_current_word_int }
    \int_compare:nNnTF \l_iow_current_line_int
                     < \l_iow_target_length_int
      { \iow_wrap_word_fits: }
      { \iow_wrap_word_newline: }
    \iow_wrap_loop:w
  }
\cs_new_protected_nopar:Npn \iow_wrap_word_fits:
  {
    \bool_if:NTF \l_iow_line_start_bool
      {
        \bool_set_false:N \l_iow_line_start_bool
        \tl_set_eq:NN \l_iow_current_line_tl \l_iow_current_word_tl
      }
      {
        \tl_put_right:Nx \l_iow_current_line_tl
          { ~ \l_iow_current_word_tl }
        \int_incr:N \l_iow_current_line_int
      }
  }
\cs_new_protected_nopar:Npn \iow_wrap_word_newline:
  {
    \tl_put_right:Nx \l_iow_wrapped_tl
      { \l_iow_current_line_tl \\ }
    \int_set_eq:NN \l_iow_current_line_int \l_iow_current_word_int
    \tl_set_eq:NN  \l_iow_current_line_tl  \l_iow_current_word_tl
  }
\cs_new_protected_nopar:Npn \iow_wrap_newline:
  {
    \tl_put_right:Nx \l_iow_wrapped_tl
      { \l_iow_current_line_tl \\ }
    \int_zero:N \l_iow_current_line_int
    \tl_clear:N \l_iow_current_line_tl
    \bool_set_true:N \l_iow_line_start_bool
    \iow_wrap_loop:w
  }
\cs_new_protected_nopar:Npn \iow_wrap_end:
  {
    \tl_put_right:Nx \l_iow_wrapped_tl
      { \l_iow_current_line_tl }
  }
\cs_new_nopar:Npn \str_length_skip_spaces:N
  { \exp_args:No \str_length_skip_spaces:n }
\cs_new:Npn \str_length_skip_spaces:n #1
  {
    \int_value:w \int_eval:w
      \exp_after:wN \str_length_loop:NNNNNNNNN \tl_to_str:n {#1}
      {X8}{X7}{X6}{X5}{X4}{X3}{X2}{X1}{X0} \q_stop
    \int_eval_end:
  }
\cs_new:Npn \str_length_loop:NNNNNNNNN #1#2#3#4#5#6#7#8#9
  {
    \if_catcode:w X #9
      \exp_after:wN \use_none_delimit_by_q_stop:w
    \else:
      9 +
      \exp_after:wN \str_length_loop:NNNNNNNNN
    \fi:
  }
\cs_new_nopar:Npn \iow_newline: { ^^J }
\cs_new_eq:NN \iow_char:N \cs_to_str:N
\prg_new_conditional:Nnn \ior_if_eof:N { p , T , F , TF }
  {
    \cs_if_exist:NTF #1
      {
        \if_eof:w #1
          \prg_return_true:
        \else:
          \prg_return_false:
        \fi:
      }
      { \prg_return_true: }
  }
\cs_new_protected_nopar:Npn \ior_to:NN #1#2
  { \tex_read:D #1 to #2 }
\cs_new_protected_nopar:Npn \ior_gto:NN #1#2
  { \pref_global:D \tex_read:D #1 to #2 }
\cs_new_protected_nopar:Npn \ior_str_to:NN #1#2
  { \etex_readline:D #1 to #2 }
\cs_new_protected_nopar:Npn \ior_str_gto:NN #1#2
  { \pref_global:D \etex_readline:D #1 to #2 }
\cs_new_protected:Npn \iow_now_buffer_safe:Nn #1#2
  { \iow_wrap:xnnnN { \exp_not:n {#2} } { } \c_zero { } \iow_now:Nn #1 }
\cs_new_protected:Npn \iow_now_buffer_safe:Nx #1#2
  { \iow_wrap:xnnnN {#2} { } \c_zero { } \iow_now:Nn #1 }
\cs_new_eq:NN \ior_new:N \use_none:n
\cs_new_eq:NN \ior_new:c \use_none:n
\cs_new_eq:NN \iow_new:N \use_none:n
\cs_new_eq:NN \iow_new:c \use_none:n
\cs_new_eq:NN \ior_open_streams: \ior_list_streams:
\cs_new_eq:NN \iow_open_streams: \iow_list_streams:
%% 
%%
%% End of file `l3io.sty'.
