<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi,</p>
<p>following the advice of howardpc, I repost here my question from
the forum
(<a class="moz-txt-link-freetext" href="https://forum.lazarus.freepascal.org/index.php/topic,60287.0.html">https://forum.lazarus.freepascal.org/index.php/topic,60287.0.html</a>).</p>
<p>Recently I wanted to rewind a file to read again a previous line.
After some search I came to the conclusion to modify the
TStreamReader class. Do you think that would be a useful addition?
Should it be an a modification to TStreamReader or a new class?<br>
</p>
<p>Ciao,</p>
<p>Gilles Marcou<br>
</p>
<p><br>
Below is a sketch of the modifications.<br>
<br>
1. Add a list of line start positions in the stream:<br>
</p>
<div class="codeheader">Code: Pascal <a class="codeoperation">[Select]</a><a
style="float: right;">[+]</a></div>
<div class="pascal geshi" style="font-family: monospace; max-height:
none; height: 213px; resize: vertical;">
<ol>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;">TListInt64
<span style="color: #000066;">=</span> specialize
TFPGList<Int64><span style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">(</span><span style="color:
#000066;">...</span><span style="color: #009900;">)</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;">
TStreamReader <span style="color: #000066;">=</span> class<span
style="color: #009900;">(</span>TTextReader<span
style="color: #009900;">)</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">private</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> flPos<span
style="color: #000066;">:</span> TListInt64<span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">function</span>
ReadPos<span style="color: #009900;">(</span>Idx<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">integer</span><span
style="color: #009900;">)</span><span style="color:
#000066;">:</span> Int64<span style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">procedure</span>
WritePos<span style="color: #009900;">(</span>Idx<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">integer</span><span
style="color: #000066;">;</span> AValue<span style="color:
#000066;">:</span> Int64<span style="color: #009900;">)</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">(</span><span style="color:
#000066;">...</span><span style="color: #009900;">)</span>
</div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">public</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">property</span>
lPos<span style="color: #009900;">[</span>Idx<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">integer</span><span
style="color: #009900;">]</span><span style="color:
#000066;">:</span> Int64 <span style="color: #000066;">read</span>
ReadPos <span style="color: #000066;">write</span> WritePos<span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
</ol>
</div>
<br>
2. Manage this list<br>
<div class="codeheader">Code: Pascal <a class="codeoperation">[Select]</a><a
style="float: right;">[+]</a></div>
<div class="pascal geshi" style="font-family: monospace; max-height:
none; height: 306px; resize: vertical;">
<ol>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">function</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">ReadPos</span><span style="color:
#009900;">(</span>Idx<span style="color: #000066;">:</span>
<span style="color: #000066; font-weight: bold;">integer</span><span
style="color: #009900;">)</span><span style="color:
#000066;">:</span> Int64<span style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> Result<span
style="color: #000066;">:</span><span style="color:
#000066;">=</span>flPos<span style="color: #009900;">[</span>Idx<span
style="color: #009900;">]</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">end</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">procedure</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">WritePos</span><span style="color:
#009900;">(</span>Idx<span style="color: #000066;">:</span>
<span style="color: #000066; font-weight: bold;">integer</span><span
style="color: #000066;">;</span> AValue<span style="color:
#000066;">:</span> Int64<span style="color: #009900;">)</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> flPos<span
style="color: #009900;">[</span>idx<span style="color:
#009900;">]</span><span style="color: #000066;">:</span><span
style="color: #000066;">=</span>AValue<span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">end</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">constructor</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">Create</span><span style="color:
#009900;">(</span>AStream<span style="color: #000066;">:</span>
TStream<span style="color: #000066;">;</span> ABufferSize<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">Integer</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;">
AOwnsStream<span style="color: #000066;">:</span> <span
style="color: #000066; font-weight: bold;">Boolean</span><span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">(</span><span style="color:
#000066;">...</span><span style="color: #009900;">)</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> flPos<span
style="color: #000066;">:</span><span style="color:
#000066;">=</span>TListInt64<span style="color: #000066;">.</span><span
style="color: #006600;">Create</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">end</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">procedure</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">Close</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">(</span><span style="color:
#000066;">...</span><span style="color: #009900;">)</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;">
FreeAndNil<span style="color: #009900;">(</span>flPos<span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">end</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
</ol>
</div>
<br>
3. Store line start positions<br>
<div class="codeheader">Code: Pascal <a class="codeoperation">[Select]</a><a
style="float: right;">[+]</a></div>
<div class="pascal geshi" style="font-family: monospace; max-height:
none; height: 150px; resize: vertical;">
<ol>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">procedure</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">ReadLine</span><span style="color:
#009900;">(</span>out AString<span style="color: #000066;">:</span>
<span style="color: #000066; font-weight: bold;">string</span><span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> VPByte<span
style="color: #000066;">:</span> PByte<span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> VPosition<span
style="color: #000066;">,</span> VStrLength<span
style="color: #000066;">,</span> VLength<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">Integer</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> flPos<span
style="color: #000066;">.</span><span style="color:
#006600;">Add</span><span style="color: #009900;">(</span>FBufferPosition<span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
</ol>
</div>
<br>
4. Add a procedure to jump to the desired line<br>
<div class="codeheader">Code: Pascal <a class="codeoperation">[Select]</a><a
style="float: right;">[+]</a></div>
<div class="pascal geshi" style="font-family: monospace; max-height:
none; height: 213px; resize: vertical;">
<ol>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> <span
style="color: #000000; font-weight: bold;">procedure</span>
GoToLn<span style="color: #009900;">(</span>n<span
style="color: #000066;">:</span> <span style="color:
#000066; font-weight: bold;">integer</span><span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">(</span><span style="color:
#000066;">...</span><span style="color: #009900;">)</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">procedure</span>
TStreamReader<span style="color: #000066;">.</span><span
style="color: #006600;">GoToLn</span><span style="color:
#009900;">(</span>n<span style="color: #000066;">:</span>
<span style="color: #000066; font-weight: bold;">integer</span><span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">var</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> apos<span
style="color: #000066;">:</span> Int64<span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">begin</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> apos<span
style="color: #000066;">:</span><span style="color:
#000066;">=</span>flPos<span style="color: #009900;">[</span>n<span
style="color: #000066;">-</span><span style="color:
#cc66cc;">1</span><span style="color: #009900;">]</span><span
style="color: #000066;">;</span><span style="color:
#808080; font-style: italic;">//flPos is 0-based and lines
are 1-based</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> Reset<span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> FStream<span
style="color: #000066;">.</span><span style="color:
#006600;">Seek</span><span style="color: #009900;">(</span>apos<span
style="color: #000066;">,</span>soBeginning<span
style="color: #009900;">)</span><span style="color:
#000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">end</span><span
style="color: #000066;">;</span></div>
</li>
<li style="white-space: nowrap; padding: 0 5px;
background-color: #fff; line-height: 16px; border-left: 1px
solid #999;">
<div style="font: normal normal 1em/1.2em monospace; margin:0;
padding:0; background:none; vertical-align:top;"> </div>
</li>
</ol>
</div>
<div class="smalltext modified" id="modified_450457"> </div>
</body>
</html>