Difference between revisions of "$readini"

From Scriptwiki
Jump to: navigation, search
m (made old code a bit more readable and added $readini2 snippet)
Line 12: Line 12:
 
The following example will loop through all item in mirc.ini in the section called ''mirc''.
 
The following example will loop through all item in mirc.ini in the section called ''mirc''.
 
  [[var]] %i = 1
 
  [[var]] %i = 1
 +
 
  ; begin a while-loop through all items in section ''mirc''. $ini(mirc.ini,mirc,0) returns  
 
  ; begin a while-loop through all items in section ''mirc''. $ini(mirc.ini,mirc,0) returns  
 
  ; total number of items in this section.
 
  ; total number of items in this section.
 
  [[while]] (%i <= [[$ini]](mirc.ini,mirc,0)) {
 
  [[while]] (%i <= [[$ini]](mirc.ini,mirc,0)) {
 +
 
   ; echo item's name and value
 
   ; echo item's name and value
 
   [[echo]] -a $ini(mirc.ini,mirc,%i) => $readini(mirc.ini,mirc,$ini(mirc.ini,mirc,%i))
 
   [[echo]] -a $ini(mirc.ini,mirc,%i) => $readini(mirc.ini,mirc,$ini(mirc.ini,mirc,%i))
 +
 
   ; increase looping-variable by one
 
   ; increase looping-variable by one
 
   [[inc]] %i
 
   [[inc]] %i
 
  }
 
  }
  
 +
Here is also a little snippet to find a name of an item by a value<br />
 +
Usage: $readini2(file.ini,topic,value[,N])<br />
 +
N is optional, 0 returns the total number of matched values, 1 returns the first matched values item name.
 +
 +
[[alias]] readini2 {
 +
 +
  ;check if the alias was called as an identifier
 +
  if ([[$isid]]) {
 +
    [[var]] %x = 1,%y
 +
 +
    ;loop through topics in an ini file
 +
    [[while]] (%x <= [[$ini]]($1,$2,0)) {
 +
 +
      ;check if we have a matching value
 +
      [[if]] ($readini($1,$2,[[$ini]]($1,$2,%x)) == $3) {
 +
 +
        ;see if $4 is null (no N given), then return the first match
 +
        [[if]] ($4 == [[$null]]) { [[return]] [[$ini]]($1,$2,%x) | [[halt]] }
 +
 +
        ;see if $4 is 0 (to find out how many matchinf values there are), then increment a value of %y
 +
        [[elseif]] ($4 == 0) { inc %y }
 +
 +
        ;see if $4 is a number (find Nth match)
 +
        [[elseif]] ($4 [[If|isnum]]) {
 +
 +
          ;increment %y, and if the value is same as $4 (Nth value to find) return the Nth match
 +
          [[inc]] %y
 +
          [[if]] (%y == $4) { [[return]] [[$ini]]($1,$2,%x) | [[halt]] }
 +
        }
 +
      }
 +
      [[inc]] %x
 +
    }
 +
 +
    ;return %y (only if N = 0, other if's halt the script)
 +
    [[return]] %y
 +
  }
 +
}
 
== See Also ==
 
== See Also ==
 
[[$ini]] returns the name/Nth position of the specified topic/item in an ini file.
 
[[$ini]] returns the name/Nth position of the specified topic/item in an ini file.

Revision as of 11:28, 31 October 2006

Returns a single line of text from an ini file

$readini(filename, [np], section, item)

If the n switch is specified then the line read in will not be evaluated and will be treated as plain text.

If the p switch is specified, command | separators are treated as such instead of as plain text.

Example

echo -a My command prefix is: $readini(mirc.ini,text,commandchar)

This example echo's your command prefix to your active window (usually /).

The following example will loop through all item in mirc.ini in the section called mirc.

var %i = 1

; begin a while-loop through all items in section mirc. $ini(mirc.ini,mirc,0) returns 
; total number of items in this section.
while (%i <= $ini(mirc.ini,mirc,0)) {

 ; echo item's name and value
 echo -a $ini(mirc.ini,mirc,%i) => $readini(mirc.ini,mirc,$ini(mirc.ini,mirc,%i))

 ; increase looping-variable by one
 inc %i
}

Here is also a little snippet to find a name of an item by a value
Usage: $readini2(file.ini,topic,value[,N])
N is optional, 0 returns the total number of matched values, 1 returns the first matched values item name.

alias readini2 {

  ;check if the alias was called as an identifier
  if ($isid) {
    var %x = 1,%y

    ;loop through topics in an ini file
    while (%x <= $ini($1,$2,0)) {

      ;check if we have a matching value
      if ($readini($1,$2,$ini($1,$2,%x)) == $3) { 

        ;see if $4 is null (no N given), then return the first match
        if ($4 == $null) { return $ini($1,$2,%x) | halt }

        ;see if $4 is 0 (to find out how many matchinf values there are), then increment a value of %y
        elseif ($4 == 0) { inc %y }

        ;see if $4 is a number (find Nth match)
        elseif ($4 isnum) { 

          ;increment %y, and if the value is same as $4 (Nth value to find) return the Nth match
          inc %y 
          if (%y == $4) { return $ini($1,$2,%x) | halt }
        }
      } 
      inc %x
    }

    ;return %y (only if N = 0, other if's halt the script)
    return %y
  }
}

See Also

$ini returns the name/Nth position of the specified topic/item in an ini file.

With /writeini you can write to an ini file.