mIRC Home    About    Download    Register    News    Help

Print Thread
replace LF with CRLF #98737 26/09/04 12:14 PM
Joined: Dec 2002
Posts: 29
C
Charlie Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Dec 2002
Posts: 29
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?


Charlie
Re: replace LF with CRLF #98738 26/09/04 08:03 PM
Joined: Aug 2003
Posts: 314
S
Sigh Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
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
}

Re: replace LF with CRLF #98739 27/09/04 06:56 AM
Joined: Dec 2002
Posts: 29
C
Charlie Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Dec 2002
Posts: 29
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?


Charlie
Re: replace LF with CRLF #98740 27/09/04 10:41 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.

Last edited by qwerty; 27/09/04 10:59 AM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: replace LF with CRLF #98741 27/09/04 10:55 AM
Joined: Dec 2002
Posts: 29
C
Charlie Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Dec 2002
Posts: 29
wow, its work. thanks you so much qwerty, you're the best


Charlie
Re: replace LF with CRLF #98742 27/09/04 01:01 PM
Joined: Aug 2003
Posts: 314
S
Sigh Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
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

Re: replace LF with CRLF #98743 27/09/04 02:23 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com