Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   TIniFile (split from loadIni2() - loadIni() replica) (https://forums.graalonline.com/forums/showthread.php?t=79421)

Inverness 04-15-2008 12:41 AM

TIniFile (split from loadIni2() - loadIni() replica)
 
Indeed, this is meant to be joined to a TStaticVar
Lines starting with ; are comments.
Lines starting with = are appended to last line.
PHP Code:

public function load(filename) {
  
temp.file 0;
  
temp.0;
  
temp.key 0;
  
temp.0;
  
temp.var = 0;
  
temp.val 0;
  
this.keys = {};
  
  if (
filename == null) {
    return;
  }
  
file.loadlines(filename);
  if (
file.size() < 1) {
    return;
  }
  for (
0file.size(); ++) {
    if (
file[i].starts(";"))
      continue;
    if (!(
file[i].starts("[") && file[i].ends("]"))) {
      if (
key == null)
        continue;
      
file[i].pos("=");
      if (
0)
        continue;
      if (
== 0) {
        
val file[i].substring(1, -1);
        
this.(@ key).(@ var) @= val;
      }
      else {
        var = 
file[i].substring(0e);
        
val file[i].substring(e+1, -1);
        
this.(@ key).(@ var) = val;
      }
    }
    else {
      
key file[i].substring(1file[i].length() - 2);
      if (
        
key in {
          
"keys",
          
"initialized",
          
"ispaused",
          
"joinedclasses",
          
"maxlooplimit",
          
"name",
          
"scripterrors",
          
"scriptlogmissingfunctions",
          
"timeout",
          
"objecttype"
        
}
      ) 
key @= "_";
      
this.keys.add(key);
    }
  }
}
public function 
save(filename) {
  
temp.output 0;
  
temp.0;
  
temp.0;
  
temp.keys this.getdynamicvarnames();
  
temp.vars 0;
  
  
keys.remove("objecttype");
  for (
0keys.size(); ++) {
    if (
this.(@ keys[i]).type() != -1) continue;
    if (
keys[i] == null) continue;
    
output.add("[" keys[i] @ "]");
    
vars this.(@ keys[i]).getdynamicvarnames();
    for (
0vars.size(); ++) {
      if (
this.(@ keys[i]).(@ vars[e]) != null) {
        
output.add(vars[e] @ "=" this.(@ keys[i]).(@ vars[e]));
      }
    }
  }
  
output.savelines(filename0);
}
public function 
clearkey(keyname) {
  
temp.0;
  
temp.vars 0;
  
  
vars this.(@ keyname).getdynamicvarnames();
  for (
0vars.size(); ++) {
    
this.(@ keyname).(@ vars[i]) = null;
  }
}
public function 
copyvars(objectkeyname) {
  if (
object.type() != 2) return;
  for (
temp.ithis.(@ keyname).getdynamicvarnames()) {
    
object.(@ i) = this.(@ keyname).(@ i);
  }
}
public function 
objecttype() {
  return 
"TIniFile";



Chompy 04-15-2008 12:56 AM

Updated version? :D

nice

And for people wondering about joining it to a TStaticVar
First you put the code Inverness posted in a class and name it something like, "file_ini" or something.

Then you do something like this serverside, for example in a dbnpc or a wnpc.

PHP Code:

function onCreated() {
  
this.ini = new TStaticVar("TIniFile");
  
this.ini.join("file_ini");


Then you can do from anywhere serverside something like
PHP Code:

function onCreated() {
  
temp.ini = new TIniFile();
  
temp.ini.load("path/to/filename.ext");

  echo(
temp.ini.section.var);



This makes it so you don't have to join "file_ini" everytime you need to load a ini file, as you can use TIniFile, as that object will have "file_ini" pre-joined.

I think Inverness can explain it better though, but I tried :p

Inverness 04-16-2008 01:40 AM

I no longer use that method for making objects, I have a public function in System instead:

var = System.INIFile(<objectname>, <filename>);

It's kinda like Python. If the filename isn't null then it does load(<filename>) automatically before returning.

Chompy 04-16-2008 03:16 PM

Interesting :o


All times are GMT +2. The time now is 05:30 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.