Variable cleanup
Posted by Rustynails
Monday, August 14, 2006
Description: Cleans unused variables from your Variables list
This script cleans up your variables list by looping through all of your scripts (including Aliases.ini and Popups.ini) and collects a list of all of your active variables (Except variables set by %variable. [ $+ [ value ] ]) and displays a log of what variables were found, where they were found (which scripts) and a list of removed variables including their values.
A brand new function that was added is the /restore function to restore all the variables from a supplied log file that you have removed.
Usage: /clean
Usage: /restore [log file]
Example: /restore [17.08.06][05.17.16].log
Special comments: Thanks to ENE|Kyrillos for this idea!
This script cleans up your variables list by looping through all of your scripts (including Aliases.ini and Popups.ini) and collects a list of all of your active variables (Except variables set by %variable. [ $+ [ value ] ]) and displays a log of what variables were found, where they were found (which scripts) and a list of removed variables including their values.
A brand new function that was added is the /restore function to restore all the variables from a supplied log file that you have removed.
Usage: /clean
Usage: /restore [log file]
Example: /restore [17.08.06][05.17.16].log
Special comments: Thanks to ENE|Kyrillos for this idea!
;; /clean alias
alias clean {
if ($var(*,0) > 0) {
var %clean::ticks = $ticks
var %clean::date $+($date([dd.mm.yy]),$time([hh.nn.ss]),.log)
if (!$isdir(cleanup)) mkdir cleanup
var %clean::initialize 1 | while (%clean::initialize <= $clean::type) {
if ($hget($clean::type(%clean::initialize))) hfree $clean::type(%clean::initialize)
inc %clean::initialize
}
var %clean::initialize 1 | while (%clean::initialize <= $clean::type) { hmake $clean::type(%clean::initialize) 100 | inc %clean::initialize }
var %clean::fopen 1 | while (%clean::fopen <= $calc($script(0) + $alias(0) + 1)) {
var %clean::file $iif(%clean::fopen > $script(0),$iif(%clean::fopen != $calc($script(0) + $alias(0) + 1),$alias($calc(%clean::fopen - $script(0))),$mircdirPopups.ini),$script(%clean::fopen))
.fopen $+(clean::file::,%clean::fopen) $+(",%clean::file,")
while (!$feof($+(clean::file::,%clean::fopen))) {
var %clean::line = $fread($+(clean::file::,%clean::fopen))
if ((%clean::line) && ($calc($gettok(%clean::line,0,37) - 1) > 0)) {
var %clean::get 1
while (%clean::get <= $calc($gettok(%clean::line,0,37) - 1)) {
var %clean::variable $+($chr(37),$gettok($gettok($gettok($gettok(%clean::line,$calc(%clean::get + 1),37),1,44),1,41),1,32))
if ((* !isin %clean::variable) && ($len(%clean::variable) > 1)) {
if ($+(",$nopath(%clean::file),") !isin $hget(clean::variables,%clean::variable)) {
hadd clean::variables %clean::variable $iif($hget(clean::variables,%clean::variable),$hget(clean::variables,%clean::variable) $+(",$nopath(%clean::file),"),$+(",$nopath(%clean::file),"))
}
inc %clean::get
}
else inc %clean::get
}
}
}
.fclose $+(clean::file::,%clean::fopen) | inc %clean::fopen
}
var %clean::variables 1, %clean::removed 0
while (%clean::variables <= $var(*,0)) {
if (!$hget($clean::type(1),$var(*,%clean::variables))) {
hadd $clean::type(2) $var(*,%clean::variables) $var(*,%clean::variables).value
hdel $clean::type(1) $var(*,%clean::variables)
clean::unset $var(*,%clean::variables)
inc %clean::removed
if (%clean::variables <= $var(*,0)) dec %clean::variables
}
inc %clean::variables
}
var %clean::remove 1 | while (%clean::remove <= $hget($clean::type(1),0).item) {
if ($hget($clean::type(1),%clean::remove).data == $+(",$nopath($script),")) hdel $clean::type(1) $hget($clean::type(1),%clean::remove).item
if ($+(",$nopath($script),") isin $hget($clean::type(1),%clean::remove).data) hadd $clean::type(1) $hget($clean::type(1),%clean::remove).item $remove($hget($clean::type(1),%clean::remove).data,$+(",$nopath($script),"))
inc %clean::remove
}
var %clean::list 1 | while (%clean::list <= $clean::type) {
if (%clean::list == 1) write $+(cleanup/,%clean::date) *** NON-REMOVED VARIABLES ***
if (%clean::list == 2) write $+(cleanup/,%clean::date) *** REMOVED VARIABLES ***
if ($hget($clean::type(%clean::list),0).item < 1) write $+(cleanup/,%clean::date) $iif(%clean::list == 1,No variables throughout your scripts were kept.,No variables were removed.)
var %clean::listadd 1
while (%clean::listadd <= $hget($clean::type(%clean::list),0).item) {
write $+(cleanup/,%clean::date) Variable: $+($hget($clean::type(%clean::list),%clean::listadd).item,$chr(44)) $iif(%clean::list == 1,Found in:,Value:) $iif(%clean::list == 1,$hget($clean::type(%clean::list),$hget($clean::type(%clean::list),%clean::listadd).item),$hget($clean::type(%clean::type),%clean::listadd).data)
inc %clean::listadd
}
inc %clean::list
}
if ($hget(clean::variables)) hfree clean::variables
echo -a $+([^K],$color(info),*) Removed %clean::removed $iif(%clean::removed == 1,variable,variables) successfully in $calc(($ticks - %clean::ticks) / 1000) seconds..
if ($exists($+(cleanup/,%clean::date))) echo -a $+([^K],$color(info),*) To view a log file of this process, simple type "/run $+(cleanup/,%clean::date,"..)
}
else echo -a $+([^K],$color(info),*) There are no variables in your Variables list..
}
alias -l clean::unset unset $1
alias clean::type {
if (!$1) return 2
if ($1 == 1) return clean::variables
if ($1 == 2) return clean::notfound
}
;; /restore alias
alias restore {
if ($exists($+(cleanup/,$1))) {
var %restore::line 1, %restore::add 0, %restore::ticks $ticks
.fopen restore::file $+(",$scriptdircleanup/,$1,")
while (!$feof(restore::file)) {
var %restore::read = $fread(restore::file)
if (!%restore::start) {
if (%restore::read == *** REMOVED VARIABLES ***) var %restore::start = 1
}
else {
if ((%restore::read != No variables were removed.) && (%restore::read)) {
set $gettok($gettok(%restore::read,2,32),-1,44) $gettok(%restore::read,4-,32)
inc %restore::add
}
}
}
.fclose restore::file
echo -a $+([^K],$color(info),*) Restored %restore::add $iif(%restore::add == 1,variable,variables) from log file $+(",cleanup/,$1,") in $calc(($ticks - %restore::ticks) / 1000) seconds..
}
else echo -a $+([^K],$color(info),*) Please provide a valid log file..
}