Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 1523

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>3.2 Example: Expanding TAB Characters</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node6.html#section.files.basic">&lt;&lt; Prev</A></TD><TD><A href="node5.html">- Up -</A></TD></TR></TABLE><DIV id="section.files.expand-a"><H2><A name="section.files.expand-a">3.2 Example: Expanding TAB Characters</A></H2><P> Suppose we want to read a file, expand all <SPAN class="char">TAB</SPAN> characters to space characters, and write the expanded lines to another file. The program which implements this task is shown in <A href="node7.html#program.expand">Program&nbsp;3.1</A>. </P><DIV class="program" id="program.expand"><HR><P><A name="program.expand"></A></P></DIV><DL class="anonymous"><DD class="code"><CODE><SPAN class="keyword">local</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">fun</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">Insert</SPAN>&nbsp;N&nbsp;Is}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">if</SPAN>&nbsp;N<SPAN class="keyword">&gt;</SPAN>0&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;{Insert&nbsp;N<SPAN class="keyword">-</SPAN>1&nbsp;<SPAN class="string">&amp;&nbsp;</SPAN><SPAN class="keyword">|</SPAN>Is}&nbsp;<SPAN class="keyword">else</SPAN>&nbsp;Is&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">fun</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">Scan</SPAN>&nbsp;Is&nbsp;Tab&nbsp;N}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">case</SPAN>&nbsp;Is&nbsp;<SPAN class="keyword">of</SPAN>&nbsp;nil&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;nil<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">[]</SPAN>&nbsp;I<SPAN class="keyword">|</SPAN>Ir&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">case</SPAN>&nbsp;I&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">of</SPAN>&nbsp;<SPAN class="string">&amp;\t</SPAN>&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;M=Tab<SPAN class="keyword">-</SPAN>(N&nbsp;<SPAN class="keyword">mod</SPAN>&nbsp;Tab)&nbsp;<SPAN class="keyword">in</SPAN>&nbsp;{Insert&nbsp;M&nbsp;{Scan&nbsp;Ir&nbsp;Tab&nbsp;M<SPAN class="keyword">+</SPAN>N}}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">[]</SPAN>&nbsp;<SPAN class="string">&amp;\n</SPAN>&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;I<SPAN class="keyword">|</SPAN>{Scan&nbsp;Ir&nbsp;Tab&nbsp;0}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">[]</SPAN>&nbsp;<SPAN class="string">&amp;\b</SPAN>&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;I<SPAN class="keyword">|</SPAN>{Scan&nbsp;Ir&nbsp;Tab&nbsp;{Max&nbsp;0&nbsp;N<SPAN class="keyword">-</SPAN>1}}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">else</SPAN>&nbsp;I<SPAN class="keyword">|</SPAN>{Scan&nbsp;Ir&nbsp;Tab&nbsp;N<SPAN class="keyword">+</SPAN>1}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR><SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">proc</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">Expand</SPAN>&nbsp;Tab&nbsp;IN&nbsp;ON}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF={New&nbsp;Open<SPAN class="keyword">.</SPAN>file&nbsp;init(name:IN)}&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OF={New&nbsp;Open<SPAN class="keyword">.</SPAN>file&nbsp;init(name:ON&nbsp;flags:[write&nbsp;create&nbsp;truncate])}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Is<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{IF&nbsp;read(list:Is&nbsp;size:all)}&nbsp;&nbsp;&nbsp;&nbsp;{IF&nbsp;close}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{OF&nbsp;write(vs:{Scan&nbsp;Is&nbsp;Tab&nbsp;0})}&nbsp;{OF&nbsp;close}<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR><SPAN class="keyword">end</SPAN></CODE></DD></DL><DIV class="program"><P class="caption"><STRONG>Program&nbsp;3.1:</STRONG> The <CODE>Expand</CODE> procedure.</P><HR></DIV><P> The file with name <CODE>IN</CODE> is opened for reading. After reading the entire file into the list <CODE>Is</CODE>, the file and the associated object are closed. Remember that reading the entire file is obtained by giving <CODE>all</CODE> as the value for feature <CODE>size</CODE>. </P><P> The expansion of <SPAN class="char">TAB</SPAN> characters is done in the function <CODE>Scan</CODE>. It takes as input parameters the list of characters <CODE>Is</CODE>, the <CODE>Tab</CODE>, and the current position <CODE>N</CODE> in the current line. </P><P> The outer <CODE><SPAN class="keyword">case</SPAN></CODE> of <CODE>Scan</CODE> figures out whether there are characters to process. If the next character to process is a <SPAN class="char">TAB</SPAN> character, enough space characters to reach the next multiple of <CODE>TabStop</CODE> are inserted. This is performed by the self explanatory function <CODE>Insert</CODE>. </P><P> A newline character resets the position&nbsp;<CODE>N</CODE> to zero. The position is decremented whenever a backspace character is encountered. Any other character increments the position. </P><P> A second file is opened for writing (indicated by <CODE>write</CODE>). If a file with name <CODE>ON</CODE> does not exist, it is created (indicated by <CODE>create</CODE>). Otherwise the already existing file is truncated to length zero (indicated by <CODE>truncate</CODE>) and rewritten. The expanded string is written to this file. </P><P> The file and its associated file object are closed after writing the expanded list of characters to it. </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node6.html#section.files.basic">&lt;&lt; Prev</A></TD><TD><A href="node5.html">- Up -</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>