Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-19-2007, 08:59 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
TIniFile

Oi, heres a little thing I whipped together before the loadini() thing was added, but I don't think that even has all the features as this.

This the script for my TIniFile which I use for things like my NPC Dialog system. Also for saving and loading mud objects.

Added comments to the script.
PHP Code:
// Loads an INI File into the object.
public function load(filename) {
  
// Declaring the temp variables
  
temp.file 0;
  
temp.0;
  
temp.key 0;
  
temp.0;
  
temp.var = 0;
  
temp.val 0;
  
  
// Load all the lines from the file.
  
file.loadlines(filename);
  
// Stop the loading if the file has no lines.
  
if (file.size() < 1) {
    return;
  }
  for (
0file.size(); ++) {
    
// If the line starts with a semi-colon, skip it. (For comments)
    
if (file[i].starts(";"))
      continue;
    
// Checking if the line is an INI section header (key)
    
if (!file[i].starts("[") && !file[i].ends("]")) {
      
// Check to see if a section has been found, can't load variables without one.
      
if (key == null)
        continue;
      
// Get the position of the equal sign on the line.
      
file[i].pos("=");
      
// Skip the line if it doesn't exist
      
if (0)
        continue;
      
// If the line starts with an equal sign, append to last variable loaded.
      // This is good for long multi-line amounts of text.
      
if (== && var != null) {
        
val file[i].substring(1, -1);
        
this.(@ key).(@ var) @= val;
      }
      
// If the equal sign isn't at the beginning, set a variable in the right section
      // <varname>=<value>
      
else {
        var = 
file[i].substring(0e);
        
val file[i].substring(e+1, -1);
        
this.(@ key).(@ var) = val;
      }
    }
    else {
      
// Set the INI section.
      
key file[i].substring(1file[i].length() - 2);
    }
  }
}
// Saves this object as an INI file
public function save(filename) {
  
// Declaring temp variables, and getting dynamic variables in the object
  
temp.output 0;
  
temp.0;
  
temp.0;
  
temp.keys this.getdynamicvarnames();
  
temp.vars 0;
  
  for (
0keys.size(); ++) {
    
// If the dynamic variable is actually a value, skip it
    
if (this.(@ keys[i]).type() != -1)
      continue;
    
// Write the section header
    
output.add("[" keys[i] @ "]");
    
// Get the dynamic variables for the section
    
vars this.(@ keys[i]).getdynamicvarnames();
    for (
0vars.size(); ++) {
      
// Add a line for the variable if its not zero.
      
if (this.(@ keys[i]).(@ vars[e]) != null) {
        
output.add(vars[e] @ "=" this.(@ keys[i]).(@ vars[e]));
      }
    }
  }
  
// write the lines
  
output.savelines(filename0);
}
// Clears all values for a certain INI section.
public function clearkey(keyname) {
  
temp.0;
  
temp.vars 0;
  
  
// Stop the function if its an actual object.
  
if (this.(@ keyname).type() != -1)
    return;
  
vars this.(@ keyname).getdynamicvarnames();
  for (
0vars.size(); ++) {
    
this.(@ keyname).(@ vars[i]) = null;
  }
}
// Copies variables from an INI section to an object (like object.loadvars())
public function copyvars(keynameobject) {
  
// Stop the function if 'object' isn't an actual object.
  
if (object.type() != 2)
    return;
  
// Stop the function if this.keyname is an object.
  
if (this.(@ keyname).type() != -1)
    return;
  for (
temp.ithis.(@ keyname).getdynamicvarnames()) {
    
object.(@ i) = this.(@ keyname).(@ i);
  }

I like INI files because the text editor in RC color-codes them for you

Heres files that it works with:
PHP Code:
// A TMudObject
[items]
i0=gold,600,"0,",0,0
i1
=sword,1,"0,",0,0
i2
=longsword,1,"0,","name_pre,","Rusted,"
[object]
health=50
id
=Inverness
mana
=100
maxhealth
=50
maxmana
=100
type
=player
version
=3
weight
=14400
[spells]
s0=flare
s1
=fireball 
PHP Code:
// Feature example
[section1]
var1=value
var2
=Multi
Line
Text.
= Do 
not forget your spaces =P
[section2]
var3=value
;This is a comment
var4
=3.14 
If you haven't caught on yet, stuff can be read with inifileobject.sectionname.variable
__________________

Last edited by Inverness; 08-19-2007 at 09:17 PM..
Reply With Quote
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:19 AM.


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