$mIni ini searching alias

From Scriptwiki
Jump to: navigation, search

$mIni([folder/][,file][,file2..,fileN],[topic][,topic2..,topicN],searchstring,[N])
Properties: .data .file .topic
Attach properties with + ie. $mIni().data+file+topic will use all of the properties
.data = search for data from item=data
.file = include <filename> into the result
.topic = include [topic] into the result

.file and .topic are mostly for debugging, but can also be used to find out what file has the searchstring

All parameters except the searchsting are optional, if you don't specify folder/file $mircdir is used if you specify folder, all of ini files in the folder will be searched you can also specify many files ie. $mIni(one.ini two.ini three.ini,topics,searchstring) or $mIni(folder/one.ini two.ini three.ini,topics,searchstring)

If you don't specify topic(s), all the sections of the files are searched, seperate different topics with space

Searchstring is a plaintext search, no wildcards (might change that in the future)

N is also optional, if not given, returns all of the matches in form of "item=data item2=data2" 0 returns number of mathces, a number above 0 returns Nth match

Examples:

lets say i have a folder /ini/ in my mircdir, where there are 3 ini files one.ini, two.ini and three.ini with various sections and items
$mIni(ini/one.ini two.ini,,test) returns test=abc test=def test=ghi
$mIni(ini/one.ini two.ini,,test).file+topic returns: <one.ini>[topic_1]test=abc <one.ini>[topic_2]test=def <two.ini>[some_topic]test=ghi
$mIni(ini/one.ini two.ini,,test,0) returns: 3
$mIni(ini,,test).file+topic returns: <one.ini>[topic_1]test=abc <one.ini>[topic_2]test=def <two.ini>[some_topic]test=ghi <three.ini>[another_topic]test=jkl
$mIni(,,main).file+topic searches all files from $mircdir and every topic, eventually returns: <mirc.ini>[windows]main=0,1196,166,745,3,1,0
note that $mIni(,,main,1) will be a lot faster than $mIni(,,main) since the script stops searching after the 1st match

$mIni

alias mIni {
 ;replace \ with / so we can match / later.
 var %s = $replace($1,\,/)

 ;take folder out of the string and take the full path ie. "ini/file.ini" -> "C:/program files/mIRC/ini/"
 ;if no folder found, use $mircdir
 var %dir = $+($iif($isdir(%s),$longfn(%s),$iif(/ isin %s,$longfn($gettok(%s,1- $calc($numtok(%s,47) -1),47)),$left($mircdir,-1))),\)

 ;get the files (will take the directory if no files are specified, but we'll strip it out)
 var %files = $gettok(%s,-1,47)

 ;set topics which we need to search for into a variable
 var %topics = $2

 ;set the searchstring into a variable and make variables %result to store the return value, and %tinc which will be increased if N is >0
 var %find = $3
 var %result,%tinc = 0

 ;get the properties and set the value of variables $true if certain properties are given, ie $mIni().data+file+topic
 if ($istok($prop,data,43)) { var %prop_data = $true }
 if ($istok($prop,file,43)) { var %prop_file = $true }
 if ($istok($prop,topic,43)) { var %prop_topic = $true }

 ;this loop goes through all of the files in %files variable and checks if they exsis and they end in .ini, removes if they doesn't
 var %x = 1 
 while (%x <= $numtok(%files,32)) { 
   if ($right($gettok(%files,%x,32),4) != .ini) || (!$exists($+(%dir,$gettok(%files,%x,32)))) { 
     var %files = $deltok(%files,%x,32) 
   }
   inc %x
 }

 ;if %files is empty (no files given, or files given didn't exist and were stripped), find all ini files from %dir
 if (!%files) { noop $findfile(%dir,*.ini,0,1, var %files = %files $nopath($1-)) }

 ;a loop to go through the files in %files variable
 var %x = 1
 while (%x <= $numtok(%files,32)) {

   ;%dirfile is the filename, including the directory, and %file is only the filename, for output
   var %dirfile = $+(%dir,$gettok(%files,%x,32))
   var %file = $gettok(%files,%x,32)

   ;we need to unset %topics2, so it won't be filled already next time this loop runs
   unset %topics2

   ;if the user doesn't give a topic to search, find all topics from the file
   if (%topics == $null) { 
     var %xy = 1
     while (%xy <= $ini(%dirfile,0)) {
       var %topics2 = %topics2 $ini(%dirfile,%xy)
       inc %xy
     }
   }

   ;else we use the topics user has given
   else { var %topics2 = %topics }

   ;a loop to go through the given topics from a file
   var %y = 1
   while (%y <= $numtok(%topics2,32)) { 

     ;set %topic (for readability purposes), and loop through all the items in %topic that is in %file
     var %topic = $gettok(%topics2,%y,32)
     var %z = 1
     while (%z <= $ini(%dirfile,%topic,0)) {

       ;check if the user is searching for an item, not a data value, and matching item found
       if ((!%prop_data) && ($ini(%dirfile,%topic,%z) == %find)) { 

         ;if $4 is null or not given, get all of the item=data into the same string
         ;also if %prop_file is $true, add <%file> in front of the item=data
         ;same for %prop_topic to include [%topic] 
         if ($4 == $null) { 
           var %result = %result $+($iif(%prop_file,$+(<,%file,>),),$iif(%prop_topic,$+($chr(91),%topic,$chr(93)),),$ini(%dirfile,%topic,%z),=,$readini(%dirfile,n,%topic,$ini(%dirfile,%topic,%z)))  
         }

         ;if $4 is 0, increment the %result, so that number of the matches can be returned
         elseif ($4 == 0) { inc %result } 

         ;if $4 is a number >0, increment %tinc and check if its value is the same as $4, return the match
         ;also checks for .file and .topic properties
         elseif ($4 isnum 1-) { 
           inc %tinc 
           if (%tinc == $4) { 
             %result =  $+($iif(%prop_file,$+(<,%file,>),),$iif(%prop_topic,$+($chr(91),%topic,$chr(93)),),$readini(%dirfile,n,%topic,$ini(%dirfile,%topic,%z)))
             return %result
           }
         }       
       }

       ;else if the user is looking for data and matching data found
       elseif ((%prop_data) && ($readini(%dirfile,n,%topic,$ini(%dirfile,%topic,%z)) == %find))  {

         ;if $4 is null or not given, get all of the item=data into same string
         ;also if %prop_file is $true, add <%file> in front of the item=data
         ;same for %prop_topic to include [%topic] 
         if ($4 == $null) { 
           var %result = %result $+($iif(%prop_file,$+(<,%file,>),),$iif(%prop_topic,$+($chr(91),%topic,$chr(93)),),$ini(%dirfile,%topic,%z),=,$readini(%dirfile,n,%topic,$ini(%dirfile,%topic,%z)))  
         }

         ;if $4 is 0, increment the %result, so that number of the matches can be returned
         elseif ($4 == 0) { inc %result }  
 
         ;if $4 is a number >0, increment %tinc and check if its value is the same as $4, return the match
         ;also checks for .file and .topic properties
         elseif ($4 isnum 1-) { 
           inc %tinc 
           if (%tinc == $4) { 
             %result = $+($iif(%prop_file,$+(<,%file,>),),$iif(%prop_topic,$+($chr(91),%topic,$chr(93)),),$ini(%dirfile,%topic,%z))
             return %result
           }
         }       
       }
       inc %z
     }
     inc %y 
   } 
   inc %x
 }
 return %result
}