writeini [Concept #2]

By alch3my on Mar 23, 2009

This alias replicates the /writeini command using $fread, /fopen, /fclose, /fseek and /fwrite

Basically, we know that remini and writeini have constraints of doing what they're suppose to do in files larger than 64kb.

I created this in response to a comment from writeini Concept #1, this is my first attempt at using the File Handling commands, so any constructive criticism is welcome!

This snippet works exactly the same as writeini, except it utilizes other mIRC commands to perform the function.

Usage: /_writeini

alias _writeini {
  ;No file? Let's create it!
  if (!$exists($1)) {
    .fopen -n writeini $1
    .fclose writeini
  }
  ;Is the <inifile> larger than 64kb?
  if ($file($1).size > 65536) {
    ;Let's open the file
    .fopen writeini $1
    ;Does the <section> exist?
    .fseek -w writeini $+(*,$chr(91),$2,$chr(93),*)
    ;No <section> found, all we have to do is add it
    if (!$fread(writeini)) {
      .fwrite writeini $+($chr(91),$2,$chr(93))
      .fwrite writeini $+($3,$chr(61),$4-)
      .fclose writeini
      return
    }
    var %x = $lines($1), %y = 1, %t = 0, %k = 0
    while (%y <= %x) {
      ;Now we're going to read line by line looking for <section>
      .fseek -l writeini %y
      ;Here's the data from the line we're reading
      var %l = $fread(writeini)
      ;We found <section>
      if ($remove(%l,$chr(91),$chr(93)) == $2) {
        var %r = $calc(%y + 1)
        while (%r <= %x) {
          .fseek -l writeini %r
          ;Finding the number of [items] in the <section> now
          if ($chr(91) != $left($fread(writeini),1)) { inc %t }
          ;Scanning of [items] complete, let's move on
          else { break }
          inc %r
        }
        ;This is where we can write to an [item] within <section>
        var %a = %t
        while (%a) {
          ;Our [items] begin on this line
          var %s = $calc(%y + %a)
          .fseek -l writeini %s
          ;We found our [item]
          if ($gettok($fread(writeini),1,61) == $3) {
            inc %k
            ;Ok, back to the line so we can write our new <value>
            .fseek -l writeini %s
            .fwrite writeini $+($v1,$chr(61),$4-,$crlf)
            .fclose writeini
            ;We're done here, no point continuing
            halt
          }
          dec %a
        }
        ;[item] wasn't found, we better add it, and <value>
        if (!%k) {
          .fseek -l writeini $calc(%t + %y + 1)
          .fwrite writeini $+($3,$chr(61),$4-,$crlf)
          .fclose writeini
        }
        ;We're done here, no point continuing the loop
        break
      }
      inc %y
    }
    ;We're done here, let's close the file if it's open
    if ($fopen(writeini)) { .fclose writeini }
  }
  ;File size isn't larger than 64kb, so we invoke standard command
  else { writeini -n $1- }
}

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.