cIniFile


Index:
Articles
Classes
Hooks

Quick navigation:
cArrowEntity
cBeaconEntity
cBlockArea
cBlockEntity
cBlockEntityWithItems
cBlockInfo
cBoundingBox
cBrewingstandEntity
cChatColor
cChestEntity
cChunkDesc
cClientHandle
cCommandBlockEntity
cCompositeChat
cCraftingGrid
cCraftingRecipe
cCryptoHash
cCuboid
cDispenserEntity
cDropperEntity
cDropSpenserEntity
cEnchantments
cEntity
cEntityEffect
cExpBottleEntity
cFile
cFireChargeEntity
cFireworkEntity
cFloater
cFlowerPotEntity
cFurnaceEntity
cGhastFireballEntity
cHangingEntity
cHopperEntity
cIniFile
cInventory
cItem
cItemFrame
cItemGrid
cItems
cJson
cJukeboxEntity
cLineBlockTracer
cLuaWindow
cMap
cMapManager
cMobHeadEntity
cMobSpawnerEntity
cMojangAPI
cMonster
cNetwork
cNoteEntity
cObjective
cPainting
cPawn
cPickup
cPlayer
cPlugin
cPluginLua
cPluginManager
cProjectileEntity
cRankManager
cRoot
cScoreboard
cServer
cServerHandle
cSignEntity
cSplashPotionEntity
cStatManager
cStringCompression
cTCPLink
cTeam
cThrownEggEntity
cThrownEnderPearlEntity
cThrownSnowballEntity
cTNTEntity
cTracer
cUDPEndpoint
cUrlClient
cUrlParser
cWebAdmin
cWindow
cWitherSkullEntity
cWorld
HTTPFormData
HTTPRequest
HTTPTemplateRequest
ItemCategory
lxp
sqlite3
TakeDamageInfo
tolua
Vector3d
Vector3f
Vector3i
Globals

Contents


cIniFile class

This class implements a simple name-value storage represented on disk by an INI file. These files are suitable for low-volume high-latency human-readable information storage, such as for configuration. Cuberite itself uses INI files for settings and options.

The INI files follow this basic structure:

; Header comment line
[KeyName0]
; Key comment line 0
ValueName0=Value0
ValueName1=Value1

[KeyName1]
; Key comment line 0
; Key comment line 1
ValueName0=SomeOtherValue
The cIniFile object stores all the objects in numbered arrays and provides access to the information either based on names (KeyName, ValueName) or zero-based indices.

The objects of this class are created empty. You need to either load a file using ReadFile(), or insert values by hand. Then you can store the object's contents to a disk file using WriteFile(), or just forget everything by destroying the object. Note that the file operations are quite slow.

For storing high-volume low-latency data, use the sqlite3 class. For storing hierarchically-structured data, use the XML format, using the LuaExpat parser in the lxp class.


Constants

NameValueNotes
noID -1

Functions

NameParametersReturn valueNotes
() (constructor) cIniFile Creates a new empty cIniFile object.
AddHeaderComment Comment Adds a comment to be stored in the file header.
AddKeyComment KeyID, Comment Adds a comment to be stored in the file under the specified key
AddKeyComment KeyName, Comment Adds a comment to be stored in the file under the specified key
AddKeyName KeyName Adds a new key of the specified name. Returns the KeyID of the new key.
AddValue KeyName, ValueName, Value Adds a new value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)
AddValueB KeyName, ValueName, Value Adds a new bool value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)
AddValueF KeyName, ValueName, Value Adds a new float value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)
AddValueI KeyName, ValueName, Value Adds a new integer value of the specified name to the specified key. If another value of the same name exists in the key, both are kept (nonstandard INI file)
CaseInsensitive Sets key names' and value names' comparisons to case insensitive (default).
CaseSensitive Sets key names and value names comparisons to case sensitive.
Clear Removes all the in-memory data. Note that , like all the other operations, this doesn't affect any file data.
DeleteHeaderComment CommentID bool Deletes the specified header comment. Returns true if successful.
DeleteHeaderComments Deletes all headers comments.
DeleteKey KeyName bool Deletes the specified key, and all values in that key. Returns true if successful.
DeleteKeyComment KeyID, CommentID bool Deletes the specified key comment. Returns true if successful.
DeleteKeyComment KeyName, CommentID bool Deletes the specified key comment. Returns true if successful.
DeleteKeyComments KeyID bool Deletes all comments for the specified key. Returns true if successful.
DeleteKeyComments KeyName bool Deletes all comments for the specified key. Returns true if successful.
DeleteValue KeyName, ValueName bool Deletes the specified value. Returns true if successful.
DeleteValueByID KeyID, ValueID bool Deletes the specified value. Returns true if successful.
FindKey KeyName number Returns the KeyID for the specified key name, or the noID constant if the key doesn't exist.
FindValue KeyID, ValueName numebr Returns the ValueID for the specified value name, or the noID constant if the specified key doesn't contain a value of that name.
Flush boolean Writes the data stored in the object to the file that was last associated with the object (ReadFile() or WriteFile()). Returns true on success, false on failure.
GetHeaderComment CommentID string Returns the specified header comment, or an empty string if such comment doesn't exist
GetKeyComment KeyID, CommentID string Returns the specified key comment, or an empty string if such a comment doesn't exist
GetKeyComment KeyName, CommentID string Returns the specified key comment, or an empty string if such a comment doesn't exist
GetKeyName KeyID string Returns the key name for the specified key ID. Inverse for FindKey().
GetNumHeaderComments number Retuns the number of header comments.
GetNumKeyComments KeyID number Returns the number of comments under the specified key
GetNumKeyComments KeyName number Returns the number of comments under the specified key
GetNumKeys number Returns the total number of keys. This is the range for the KeyID (0 .. GetNumKeys() - 1)
GetNumValues KeyID number Returns the number of values stored under the specified key.
GetNumValues KeyName number Returns the number of values stored under the specified key.
GetValue KeyID, ValueID, [DefaultValue] string Returns the value of the specified name under the specified key. Returns DefaultValue (empty string if not given) if the value doesn't exist.
GetValue KeyName, ValueName, [DefaultValue] string Returns the value of the specified name under the specified key. Returns DefaultValue (empty string if not given) if the value doesn't exist.
GetValueB KeyName, ValueName, [DefaultValue] bool Returns the value of the specified name under the specified key, as a bool. Returns DefaultValue (false if not given) if the value doesn't exist.
GetValueF KeyName, ValueName, [DefaultValue] number Returns the value of the specified name under the specified key, as a floating-point number. Returns DefaultValue (zero if not given) if the value doesn't exist.
GetValueI KeyName, ValueName, [DefaultValue] number Returns the value of the specified name under the specified key, as an integer. Returns DefaultValue (zero if not given) if the value doesn't exist.
GetValueName KeyID, ValueID string Returns the name of the value specified by its ID. Inverse for FindValue().
GetValueName KeyName, ValueID string Returns the name of the value specified by its ID. Inverse for FindValue().
GetValueSet KeyName, ValueName, [DefaultValue] string Returns the value of the specified name under the specified key. If the value doesn't exist, creates it with the specified default (empty string if not given).
GetValueSetB KeyName, ValueName, [DefaultValue] bool Returns the value of the specified name under the specified key, as a bool. If the value doesn't exist, creates it with the specified default (false if not given).
GetValueSetF KeyName, ValueName, [DefaultValue] number Returns the value of the specified name under the specified key, as a floating-point number. If the value doesn't exist, creates it with the specified default (zero if not given).
GetValueSetI KeyName, ValueName, [DefaultValue] number Returns the value of the specified name under the specified key, as an integer. If the value doesn't exist, creates it with the specified default (zero if not given).
HasValue KeyName, ValueName bool Returns true if the specified value is present.
ReadFile FileName, [AllowExampleFallback] bool Reads the values from the specified file. Previous in-memory contents are lost. If the file cannot be opened, and AllowExample is true, another file, "filename.example.ini", is loaded and then saved as "filename.ini". Returns true if successful, false if not.
SetValue KeyID, ValueID, NewValue bool Overwrites the specified value with a new value. If the specified value doesn't exist, returns false (doesn't add).
SetValue KeyName, ValueName, NewValue, [CreateIfNotExists] bool Overwrites the specified value with a new value. If CreateIfNotExists is true (default) and the value doesn't exist, it is first created. Returns true if the value was successfully set, false if not (didn't exists, CreateIfNotExists false).
SetValueB KeyName, ValueName, NewValueBool, [CreateIfNotExists] bool Overwrites the specified value with a new bool value. If CreateIfNotExists is true (default) and the value doesn't exist, it is first created. Returns true if the value was successfully set, false if not (didn't exists, CreateIfNotExists false).
SetValueF KeyName, ValueName, NewValueFloat, [CreateIfNotExists] bool Overwrites the specified value with a new floating-point number value. If CreateIfNotExists is true (default) and the value doesn't exist, it is first created. Returns true if the value was successfully set, false if not (didn't exists, CreateIfNotExists false).
SetValueI KeyName, ValueName, NewValueInt, [CreateIfNotExists] bool Overwrites the specified value with a new integer value. If CreateIfNotExists is true (default) and the value doesn't exist, it is first created. Returns true if the value was successfully set, false if not (didn't exists, CreateIfNotExists false).
WriteFile FileName bool Writes the current in-memory data into the specified file. Returns true if successful, false if not.

Code example: Reading a simple value

cIniFile is very easy to use. For example, you can find out what port the server is supposed to use according to settings.ini by using this little snippet:
local IniFile = cIniFile();
if (IniFile:ReadFile("settings.ini")) then
	ServerPort = IniFile:GetValueI("Server", "Port");
end

Code example: Enumerating all objects in a file

To enumerate all keys in a file, you need to query the total number of keys, using GetNumKeys(), and then query each key's name using GetKeyName(). Similarly, to enumerate all values under a key, you need to query the total number of values using GetNumValues() and then query each value's name using GetValueName().

The following code logs all keynames and their valuenames into the server log:

local IniFile = cIniFile();
IniFile:ReadFile("somefile.ini")
local NumKeys = IniFile:GetNumKeys();
for k = 0, (NumKeys - 1) do
	local NumValues = IniFile:GetNumValues(k);
	LOG("key \"" .. IniFile:GetKeyName(k) .. "\" has " .. NumValues .. " values:");
	for v = 0, (NumValues - 1) do
		LOG("  value \"" .. IniFile:GetValueName(k, v) .. "\".");
	end
end
Generated on 2016-08-22 23:53:06, Build ID Unknown, Commit approx: 2ed4af74edd14ae17e1c6c64d44caa7b7fc30d5a