mIRC Home    About    Download    Register    News    Help

Print Thread
Page 2 of 2 1 2
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Here's a version that doesn't create an extra text file, which is very fast (600 ms on my system), and yet works entirely different.
That's one of the things I like the most about programming, that you can achieve the same thing with different tools.

; Usage: $split(source filepath, destionationfile) without .txt
;
; Example: //echo -a $split($mircdir\test\bigfile.txt, c:\files\test)
;
; will output text files like test1.txt, test2.txt, etc.
;

Code:
alias split {
  if !$isfile($1) || !$isdir($nofile($2)) || !$mkfn($nopath($2)) { return Error }
  window -h @@
  loadbuf -r @@ " $+ $1"
  var %a = 0, %b = $+(",$nofile($2),$mkfn($nopath($2)))
  while $line(@@,1) != $null {
    inc %a
    savebuf 1-5000 @@ $+(%b,%a,.txt")
    dline @@ 1-5000
  }
  close -@ @@
  return %a
}


Gone.
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Yes i saw that before you removed it above smile, i knew it would be faster again, but i just thought id stick to /filter alone when i did mine.
Yours took 1/2 the time of mine about 1 second, just as i suspected.
I made a slight fix on the error detet line
if !$isfile($1) || ($nofile($2) && (!$isdir($nofile($2)))) || !$mkfn($nopath($2)) { return Error }

!$isdir($nofile($2)) is true if no dir is present in $2

* Now For any one who cares *
loadbuf/savebuf kicks arse
filter follows up close behind
fopen etc comes a distant 3rd
$read/write well im still waiting for it to finish!

heres my Fopen one, i did try and tweak it up more than this, but just couldnt gain any significatnt speed out of it, and the code starts looking real bizzare, with prereads before the while loop

Code:
alias Splitup2 {
  if (!$isfile($1)) { return -1 }
  if (($nofile($2)) && (!$isdir($v1))) { return -2 }
  if (!$mkfn($nopath($2))) { return -3 }
  ;
  var %i = 1, %l = 5000
  .fclose splitup.source.file*      | .fopen    splitup.source.file      $+(",$1,")
  .fclose splitup.destination.file* | .fopen -o splitup.destination.file $+(",$nofile($2),$mkfn($nopath($2)),%i,.txt")
  .timer.splitup.close.files 0 1 fclose splitup.source.file* $(|,) fclose splitup.destination.file*
  while (!$fopen(splitup.source.file).eof) {
    .fwrite -n splitup.destination.file $fread(splitup.source.file)
    dec %l
    if ((!%l) && (!$fopen(splitup.source.file).eof)) {
      inc %i
      .fclose splitup.destination.file* | .fopen -o splitup.destination.file $+(",$nofile($2),$mkfn($nopath($2)),%i,.txt")
      var %l = 5000
    }
  }
  .timer.splitup.close.files off
  .fclose splitup.source.file*
  .fclose splitup.destination.file*
  return %i
}


It takes some time still 44 seconds on my test, so dont be thinking it hung if u try it.

Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Hmm, why would you put $nofile($2) && (!$isdir($nofile($2)) ?

$isdir($nofile($2)) will return $false if $nofile($2) is not a directory or $null.

Therefore you don't need to check for existance of $nofile($2) since $isdir($null) is $false, making !$isdir($null) $true.


Gone.
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
And if its true then you were erroring out, so the output files had to have a DIR component to them, if it didnt you got !$isdir($nofile($2)) aka !$isdir($null) aka $true and errored.
I only picked it up because i tried $split(bigfile.txt,dump) and it errored out.

Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Ha, I see what you mean, but it is a user error, not the scripts'.

You're saying $nofile(dump) will return $null, therefore $isdir($null) will be $false, hence the negated of that is $true, making the script to return.

If we dont have a folder named dump, this forms no problem, but if we had a folder named dump, it still would have thought theres no such folder. However, the user error lies in the fact that "dump" isn't a folder path, but a file name. If the user wishes to put the output in folder dump, they should specify dump\, which $nofile wont strip, hence $isdir will give $true, and the negated false, so that the script continues.

Since the user is responsible for entering both the folder and filename, just putting 'dump' as output, actually seems like a good reason to give an error, since only the filename (dump) is recognised, even if the user meant for it to be a folder.

My confusion with your remark thus was the fact that from my point of view, putting just 'dump' should raise an error. However, our views on it don't necessarily need to be the same. Anyone is free to modify the given code smile

Greets


Gone.
Page 2 of 2 1 2

Link Copied to Clipboard