Javascript ConfigFile
(Created page with "Configuration File handling object. ==Constructor== new ConfigFile(); new ConfigFile(path); new ConfigFile(path,warn); Where: * path - Relative or absolute path to the Confi...") |
(Added doc for clearSection() and addValue()) |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
==Constructor== | ==Constructor== | ||
− | new ConfigFile() | + | * '''new ConfigFile()''' |
− | new ConfigFile(path) | + | * '''new ConfigFile(path)''' |
− | new ConfigFile(path,warn) | + | * '''new ConfigFile(path,warn)''' |
Where: | Where: | ||
* path - Relative or absolute path to the Configuration file like "/path/to/file.conf" | * path - Relative or absolute path to the Configuration file like "/path/to/file.conf" | ||
* warn - True to warn if the file does not exist | * warn - True to warn if the file does not exist | ||
+ | |||
+ | # To allow Yate to detect where to read from: | ||
+ | var conf = new ConfigFile(Engine.configFile("regfile")); | ||
+ | |||
+ | # When using absolute path: | ||
+ | var conf = new ConfigFile("/usr/local/etc/yate/regfile.conf"); | ||
==Methods== | ==Methods== | ||
Line 47: | Line 53: | ||
* '''getValue(section,key,defValue)''' | * '''getValue(section,key,defValue)''' | ||
− | Retrieves the value of ''key'' in ''section''. Returns '' | + | Retrieves the value of ''key'' in ''section''. Returns ''defValue'' if the key or section does not exist. |
+ | |||
+ | * '''getIntValue(section,key)''' | ||
+ | Retrieves the integer value of ''key'' in ''section''. Returns 0 if the key or section does not exist. | ||
+ | |||
+ | * '''getIntValue(section,key,defValue)''' | ||
+ | Retrieves the integer value of ''key'' in ''section''. Returns ''defValue'' if the key or section does not exist. | ||
+ | |||
+ | * '''getBoolValue(section,key)''' | ||
+ | Retrieves the boolean value of ''key'' in ''section''. Returns ''false'' if the key or section does not exist. | ||
+ | |||
+ | * '''getBoolValue(section,key,defValue)''' | ||
+ | Retrieves the boolean value of ''key'' in ''section''. Returns ''defValue'' if the key or section does not exist. | ||
* '''setValue(section,key,value)''' | * '''setValue(section,key,value)''' | ||
Sets the ''value'' for ''key'' in specified file ''section''. | Sets the ''value'' for ''key'' in specified file ''section''. | ||
+ | |||
+ | * '''addValue(section,key,value)''' | ||
+ | Add a new ''value'' for ''key'' in specified file ''section''. | ||
+ | |||
+ | * '''clearSection(section)''' | ||
+ | Deletes the given ''section''. | ||
+ | Deletes all sections if ''section'' is undefined, null or empty | ||
* '''clearKey(section,key)''' | * '''clearKey(section,key)''' | ||
Line 57: | Line 82: | ||
* '''keys(section)''' | * '''keys(section)''' | ||
Returns a numbered (non-associative) Array containing all the key names in ''section''. | Returns a numbered (non-associative) Array containing all the key names in ''section''. | ||
+ | |||
+ | ==Notes== | ||
+ | |||
+ | * Boolean | ||
+ | You may see that some parameters use true/false, on/off, enable/disable, t/f as values. This are all internally used as boolean values, so it is possible to use any of this values for boolean properties. | ||
+ | |||
+ | <b>Example:</b><br /> | ||
+ | |||
+ | var prop = true == on == enable == t <br /> | ||
+ | var prop = false == off == disable == f | ||
+ | |||
+ | Note that this will not work: enable === true | ||
[[Category:Javascript]] | [[Category:Javascript]] |
Latest revision as of 16:10, 25 August 2015
Configuration File handling object.
[edit] Constructor
- new ConfigFile()
- new ConfigFile(path)
- new ConfigFile(path,warn)
Where:
- path - Relative or absolute path to the Configuration file like "/path/to/file.conf"
- warn - True to warn if the file does not exist
# To allow Yate to detect where to read from: var conf = new ConfigFile(Engine.configFile("regfile")); # When using absolute path: var conf = new ConfigFile("/usr/local/etc/yate/regfile.conf");
[edit] Methods
- name()
Returns the path of the Configuration file.
- name(path)
Sets a new file path
- load(warn)
Loads the file content, optionally warns if it doesn't exist.
Returns true on success or false if the file does not exist.
- save()
Saves the file overwriting any existing content.
Returns true on success or false if the file could not be written.
- count()
Returns the number of sections in the file.
- sections()
Returns an associative Array containing ConfigSection objects representing each file section.
- getSection(name)
Returns a ConfigSection object for the section name.
Returns null if the section does not exist.
- getSection(name,create)
Returns a ConfigSection object for the section name, creates it if create is true.
Returns null if the section could not be created.
- getValue(section,key)
Retrieves the value of key in section. Returns undefined if the key or section does not exist.
- getValue(section,key,defValue)
Retrieves the value of key in section. Returns defValue if the key or section does not exist.
- getIntValue(section,key)
Retrieves the integer value of key in section. Returns 0 if the key or section does not exist.
- getIntValue(section,key,defValue)
Retrieves the integer value of key in section. Returns defValue if the key or section does not exist.
- getBoolValue(section,key)
Retrieves the boolean value of key in section. Returns false if the key or section does not exist.
- getBoolValue(section,key,defValue)
Retrieves the boolean value of key in section. Returns defValue if the key or section does not exist.
- setValue(section,key,value)
Sets the value for key in specified file section.
- addValue(section,key,value)
Add a new value for key in specified file section.
- clearSection(section)
Deletes the given section. Deletes all sections if section is undefined, null or empty
- clearKey(section,key)
Deletes the key from specified file section.
- keys(section)
Returns a numbered (non-associative) Array containing all the key names in section.
[edit] Notes
- Boolean
You may see that some parameters use true/false, on/off, enable/disable, t/f as values. This are all internally used as boolean values, so it is possible to use any of this values for boolean properties.
Example:
var prop = true == on == enable == t
var prop = false == off == disable == f
Note that this will not work: enable === true