# LaTeX 3 read/write to file like toc

I tried to reimplement some kind of a toc facility. I write wouts argument into a file and try to reinclude this. So the Problem is, that \ior_str_gto:NN seems to stop at linebreaks. Please consider the following example:

\documentclass{report}
\usepackage[latin1]{inputenc}
\usepackage{ expl3, l3keys2e, xfrac, xparse}

\ExplSyntaxOn

\iow_new:N\iow_out
\ior_new:N\ior_out

\file_if_exist:nTF {myout.out} {
\ior_open:Nn\ior_out{myout.out}
\ior_str_gto:NN\ior_out\tl_get_out
\ior_close:N\ior_out }
{ \typeout{no file! rerun}
}

\iow_open:Nn\iow_out{myout.out}

\DeclareDocumentCommand\wout { m } {
\iow_now:Nx\iow_out{#1}
}
\DeclareDocumentCommand\rout {  } {
\tl_use:N\tl_get_out
}

\ExplSyntaxOff
%
%
\begin{document}
\wout{bla}
\wout{blub}
\rout
\end{document}


The Question is: How can i make \ior_to_gstr to read in the whole file? and: is there a better way to implement such a function using l3?

-
\ior_to:Nn, etc. read one line at a time. If you look at LaTeX2e, the TOC concept is implemented by \inputing the entire file. We have some experimental stuff for doing whole file loops, but not yet anything 'release ready'. Perhaps suggest what might work for you! –  Joseph Wright Jun 23 '12 at 18:20
I thought that you might answer this. My TeX knowledge is far away from writing s.th. for the kernel. –  bloodworks Jun 23 '12 at 18:41

The LaTeX2e kernel reads files for tables, etc. using \input rather than line by line. However, it is possible to set up a line-by-line read here using epxl3 to work in a 'string' manner. There is an experimental function called \ior_str_map_inline:Nn which does more or less what seems to be wanted here:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn

\iow_new:N \g_my_out_iow
\ior_new:N \g_my_out_ior
\tl_new:N \g_my_out_tl

\file_if_exist:nTF { myout.out }
{
\ior_open:Nn \g_my_out_ior { myout.out }
\ior_str_map_inline:Nn \g_my_out_ior
{ \tl_gput_right:Nn \g_my_out_tl {#1 \par } }
\ior_close:N \g_my_out_ior
}
{ \typeout{no file! rerun} }

\iow_open:Nn \g_my_out_iow { myout.out }

\DeclareDocumentCommand\wout { m }
{ \iow_now:Nn \g_my_out_iow {#1} }
\DeclareDocumentCommand\rout {  }
{ \tl_use:N \g_my_out_tl }

\ExplSyntaxOff
%
%
\begin{document}
\wout{bla}
\wout{blub}
\rout
\end{document}


(I have tided up a few variable names.) I've added \par to each line as it's not clear to me quite what is wanted. I've also written unexpanded to file, again as the wider context is not clear.

-
There are a few changes I will be making to the I/O code before the next CTAN update. That should not impact here, but I do notice I need to address the 'always global' nature of streams! –  Joseph Wright Jun 23 '12 at 18:58
Thats very fine. I´m definitively going to look up this function. One thing still worries me: i do open the output stream for the entire compile time. I wonder if there is a tex function which allows opening out streams w/o recreating the whole file (such as >> in the shell). –  bloodworks Jun 23 '12 at 19:19
@bloodworks There is no >> equivalent in TeX: one can read a file and write back, but there are issues. The LaTeX3 recommendation is not to keep the file open unless you need \iow_shipout:Nn or similar. If you want \iow_now:Nn then you can save everything to a tl and do a one-shot write at the end of the document. (We may well look at the shipout issue again, as one can solve this too, but it's more tricky and at the moment not something we need.) –  Joseph Wright Jun 23 '12 at 19:21
@JosephWright: Would it make sense to add a function (in \ior or \file) to read the contents of a whole file? –  Bruno Le Floch Jun 24 '12 at 9:22
Whats about tl_extract_between –  bloodworks Jun 24 '12 at 19:55