Skip to content

Instantly share code, notes, and snippets.

@qinglee
Last active December 10, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qinglee/4512279 to your computer and use it in GitHub Desktop.
Save qinglee/4512279 to your computer and use it in GitHub Desktop.
Use a stack to manage the allocation of input and output streams.
\RequirePackage{expl3}
\ExplSyntaxOn
\seq_new:N \c__iow_streams_seq
\seq_new:N \g__iow_streams_seq
\int_step_inline:nnnn \c_zero \c_one \c_fifteen
{ \seq_gput_right:Nn \g__iow_streams_seq {#1} }
\seq_gset_eq:NN \c__iow_streams_seq \g__iow_streams_seq
\tl_new:N \l__iow_stream_tl
\cs_set_protected:Npn \__iow_open:Nn #1#2
{
\iow_close:N #1
\seq_gpop:NNTF \g__iow_streams_seq \l__iow_stream_tl
{
\tex_global:D \tex_chardef:D #1 = \l__iow_stream_tl \scan_stop:
\iow_log:x
{ \token_to_str:N #1 ~=~ output~stream~ \tl_use:N \l__iow_stream_tl }
\prop_gput:NVn \g__iow_streams_prop #1 {#2}
\tex_immediate:D \tex_openout:D #1#2 \scan_stop:
}
{ \__msg_kernel_fatal:nn { kernel } { output-streams-exhausted } }
}
\cs_set_protected:Npn \iow_close:N #1
{
\token_if_chardef:NTF #1
{
\seq_if_in:NVT \c__iow_streams_seq #1
{
\seq_if_in:NVF \g__iow_streams_seq #1
{
\tex_immediate:D \tex_closeout:D #1
\seq_gpush:NV \g__iow_streams_seq #1
\prop_gremove:NV \g__iow_streams_prop #1
\cs_gset_eq:NN #1 \c_term_iow
}
}
}
{ \ERROR }
}
\int_step_inline:nnnn \c_zero \c_one \c_fifteen
{ \iow_new:c { g_tmp_#1_iow } }
\int_step_inline:nnnn \c_zero \c_one \c_fifteen
{ \iow_open:cn { g_tmp_#1_iow } { test- #1 .txt } }
\seq_show:N \g__iow_streams_seq
\iow_list_streams:
\int_step_inline:nnnn \c_zero \c_one \c_ten
{ \iow_close:c { g_tmp_#1_iow } }
\seq_show:N \g__iow_streams_seq
\iow_list_streams:
\int_step_inline:nnnn \c_zero \c_one \c_ten
{ \iow_open:cn { g_tmp_#1_iow } { test- #1 .txt } }
\seq_show:N \g__iow_streams_seq
\iow_list_streams:
\tex_end:D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment