mIRC Homepage

replace LF with CRLF

Posted By: Charlie

replace LF with CRLF - 26/09/04 12:14 PM

i have a little problem with this code

Code:
      while ($bfind(&buffer,1,10)) {
        set %pos $ifmatch
        if (%pos == 1) {
          bset &binvar $calc($bvar(&binvar,0) + 1) 13 10
          bcopy -cz &binvar $calc($bvar(&binvar,0) + 1) &buffer 2 -1
        }
        else {
          bcopy -cz &binvar $calc($bvar(&binvar,0) + 1) &buffer 1 -1
          bset &binvar $calc($bvar(&binvar,0) + 1) 13 10
        }
        bcopy -cz &buffer 1 &buffer $calc(%pos + 1) -1
      }
      bcopy &buffer 1 &binvar 1 -1


it should replace all LF code with CRLF but it does not work like it should be. have someone an idea how i can resolve it?
Posted By: Sigh

Re: replace LF with CRLF - 26/09/04 08:03 PM

You could just replace all linefeed characters in &binvar with a simple loop:

var %x = 1
while ($bfind(&binvar,%x,10)) {
bcopy &binvar $calc(1+$v1) &binvar $v1 -1
bset &binvar $v1 13
%x = $v1 + 2
}
Posted By: Charlie

Re: replace LF with CRLF - 27/09/04 06:56 AM

it doesn't work, i need CRLF not just CR or LF. btw, your snippet have the same effect with
Code:
breplace &binvar 10 13

but its not what i need. any other idea?
Posted By: qwerty

Re: replace LF with CRLF - 27/09/04 10:41 AM

Try this:
Code:
  var %x = 1
  while %x <= $bvar(&buffer,0) && $bfind(&buffer,%x,10) {
    bcopy &temp $calc($bvar(&temp,0) + 1) &buffer %x $calc($v1 - %x)
    bset &temp $calc($bvar(&temp,0) + 1) 13 10
    %x = $v1 + 1
  }
  if $bvar(&buffer,0) >= %x { bcopy &temp $calc($bvar(&temp,0) + 1) &buffer %x -1 }
  bcopy -c &buffer 1 &temp 1 -1
Normally, the %x <= $bvar(&buffer,0) condition in the while loop shouldn't be necessary, but there's a bug in $bfind(); if you're searching a binvar for a character and that character is the last byte in that binvar and you search past that byte (ie with index $calc($bvar(&binvar,0) + 1) ), it claims to have found it, while this is certainly not true (how can you find something when you search past its position?).


Personally, I would probably follow a different approach though:
Code:
  btrunc temp.tmp 0
  bwrite temp.tmp 1 -1 &amp;buffer
  filter -ffc temp.tmp temp.tmp
  bread temp.tmp 0 $file(temp.tmp) &amp;buffer
  .remove temp.tmp
Apart from it being simpler in understanding/writing, it is also faster, since all the work is done internally. The fact that you're writing to a file may slow it down a bit, but, in the end, it has proven faster than the first method for me. Btw, keep in mind that /filter adds a CRLF at the end of the file, if it isn't there already, so you may want to trim that trailing CRLF, depending on what you need the binvar for.
Posted By: Charlie

Re: replace LF with CRLF - 27/09/04 10:55 AM

wow, its work. thanks you so much qwerty, you're the best
Posted By: Sigh

Re: replace LF with CRLF - 27/09/04 01:01 PM

That breplace will just replace a line feed char with another, this replaces a line feed with a carriage return & line feed but it does indeed fail if char 10 is at the end of the binary variable, qwerty's method seems ideal
Posted By: qwerty

Re: replace LF with CRLF - 27/09/04 02:23 PM

I didn't actually pay attention to your code before, I just noticed Charlie's reply that it doesn't do what he wants. Taking a closer look though, your method does what it's supposed to and can be used instead of mine with a small modification:
Code:
  var %x = 1
  while [color:blue]%x &lt;= $bvar(&amp;buffer,0) &amp;&amp;[/color] $bfind(&amp;buffer,%x,10) {
    bcopy &amp;buffer $calc(1+$v1) &amp;buffer $v1 -1
    bset &amp;buffer $v1 13
    %x = $v1 + 2
  }
This way, trailing LFs do not cause problems and seeing that your method is about 30% faster than mine (the first, 'traditional' version), I like it more. Good job smile
© 2020 mIRC Discussion Forums