$gzencode - Compress as gzip.

By rockcavera on Feb 22, 2017

/*

Update: 05/09/2017 (dd/mm/yyyy)

This identifier is used to compress binary variables and files, being compatible with gzip.

-Syntax-

$gzencode(filename|&bvar[,Nbnkf[c,&bvar2]])

-Parameters-

N = compression level of 1 to 6 (default is 6).
b = indicates a binary variable in the first parameter.
n = do not save the original name and modification time.
k = keep (don't delete) input files.
f = force overwrite of output file.
c = output on &bvar2, keep original files unchanged.

-Examples-

//echo -a Return: $gzencode($mircexe,kf)

//echo -a Return: $gzencode($mircexe,c,&bvar2) - After: $bvar(&bvar2,0)

//bread $+(",$mircexe,") 0 $file($mircexe).size &bvar | echo -a Before: $bvar(&bvar,0) - Return: $gzencode(&bvar,1b) - After: $bvar(&bvar,0)

-Compatibility-

mIRC 6.1 or later.

-Comments-

On success, it will return filename.gz, &bvar or &bvar2. Otherwise, it returns null.

Don't use in &bvar and &bvar2 a binary variable with name &_gz or &_data.
Also avoid using these binary variable names in the script that calls the $gzcompress().

*/

alias gzencode {
  if ($version < 6.1) || (!$isid) || (!$1) return
  set -l %l 6
  set -l %flg 8
  set -l %xfl 0
  var %b,%c,%k,%name
  if ($2) {
    if ($regex($2,/([1-6])/)) {
      %l = $regml(1)
      if (%l == 1) %xfl = 4
    }
    if (b isincs $2) {
      if ($bvar($1,0) !isnum 1-4294967295) || (&_gz == $1) return
      %b = 1
      %flg = 0
      goto i
    }
    if (n isincs $2) %flg = 0
    if (c isincs $2) {
      if (!$3) || ($left($3,1) != &) || ($istok(&_data &_gz,$3,32)) return
      %c = $3
      goto i
    }
    if (k isincs $2) %k = 1
    if (f !isincs $2) && ($isfile($+($1,.gz))) return
  }
  :i
  if (!%b) && ((!$isfile($1)) || ($file($1).size !isnum 1-4294967295)) return
  set -l %crc $reverse($longip($base($crc($1,$iif(%b,1,2)),16,10)))
  set -l %size $reverse($longip($iif(%b,$bvar($1,0),$file($1).size)))
  set -l %mtime $iif(%flg,$reverse($longip($file($mircexe).mtime)),0 0 0 0)
  if (%flg) %name = $ascname($nopath($1)) 0
  set -l %bvar &_data
  if (%b) %bvar = $1
  else bread $+(",$1,") 0 $file($1).size %bvar
  if (!$compress(%bvar,$+(bl,%l))) return
  bunset &_gz
  bset &_gz 1 31 139 8 %flg %mtime %xfl 11 %name
  bcopy &_gz $calc($bvar(&_gz,0) + 1) %bvar 3 $calc($bvar(%bvar,0) - 6)
  bset &_gz $calc($bvar(&_gz,0) + 1) %crc %size
  if (%b) {
    bcopy -c $1 1 &_gz 1 -1
    bunset &_gz
    return $1
  }
  bunset %bvar
  if (%c) {
    bcopy -c %c 1 &_gz 1 -1
    bunset &_gz
    return %c
  }
  if ($isfile($+($1,.gz))) .remove $+(",$1,.gz")
  bwrite $+(",$1,.gz") -1 -1 &_gz
  bunset &_gz
  if (!%k) .remove $+(",$1,")
  return $+($1,.gz)
}

alias -l reverse {
  tokenize 46 $1
  return $4 $3 $2 $1
}

alias -l ascname {
  set -l %name
  set -l %x $len($1)
  :a
  %name = $asc($mid($1,%x,1)) %name
  dec %x
  if (%x > 0) goto a
  return %name
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.