And here is the full header of my ini lib. I like to have "talkative" headers too.
Code:
/*
Title: Basic ini string functions
Operate on variables instead of files. An easy to use ini parser.
About: License
New BSD License
Copyright (c) 2010, Tuncay
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Tuncay nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Tuncay BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
About: Introduction
Ini files are used mostly as configuration files. In general, they have the
".ini"-extension. It is a simple standardized organization of text data.
Many other simple programs use them for storing text.
AutoHotkey provides three commands IniDelete, IniRead and IniWrite. These
commands are stable, but they have some disadvantages. First disadvantage
is, that they access the file directly. The file on the disk is opened,
load into memory and then read or manipulated and then saved with every
single command.
With the custom functions I wrote here, the user accessess on variables
instead of files. This is super fast, in comparison to disk access. Ini
files can be created by Ahk just like any other variable. But Ahk itself
does not have any function to operate on ini strings (variables). If you
read often from ini file, then this might for you.
No other framework or library is required, no special object files are
created; just work on ordinary ini file contents or variables. The load
and save functions are added for comfort reason and are not really needed.
* *First do this:*
> FileRead, ini, config.ini
* *or load default file with:*
> ini_load(ini)
* *or create the content yourself:*
(Start Code)
ini =
(
[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)
(End Code)
In this example "Tip" and "Recent File List" are name of the sections. The
file consist in this example of 2 sections. Every section contains variables,
so called "keys". Every key is a part of a section. In this example, the
section "Tip" have one key "TimeStamp". And every key has a content,
called value. The "TimeStamp" key have the value "20090716194758".
After that, you can access and modify the content of the ini variable with
the following functions. But the modifications are only temporary and must
me saved to disk. This should be done by overwriting the source (not
appending).
*Notes*: A keys content (the value) goes until end of line. Any space
surroounding the value is at default lost. For best compatibility, the
names of section and key should consist of alpha (a-z), num (0-9) and the
underscore only. In general, the names are case insensitiv.
Links:
* Lib Home: [http://autohotkey.net/~Tuncay/lib/index.html]
* Download: [http://autohotkey.net/~Tuncay/lib/ini.zip]
* Discussion: [http://www.autohotkey.com/forum/viewtopic.php?t=46226]
* License: [http://autohotkey.net/~Tuncay/licenses/newBSD_tuncay.txt]
Date:
2010-09-26
Revision:
1.0
Developers:
* Tuncay (Author)
* Mystiq (Tester and Co-Author of an important regex)
* Fry (Tester)
Category:
String Manipulation, FileSystem
Type:
Library
Standalone (such as no need for extern file or library):
Yes
StdLibConform (such as use of prefix and no globals use):
Yes
Related:
*Format Specifications (not strictly implemented)*
* Wikipedia - INI file [http://en.wikipedia.org/wiki/INI_file]
* Cloanto Implementation of INI File Format [http://www.cloanto.com/specs/ini.html]
*AutoHotkey Commands*
* IniRead: [http://www.autohotkey.com/docs/commands/IniRead.htm]
* IniWrite: [http://www.autohotkey.com/docs/commands/IniWrite.htm]
* IniDelete: [http://www.autohotkey.com/docs/commands/IniDelete.htm]
*Other Community Solutions*
* INI Library by Titan: [http://www.autohotkey.com/forum/viewtopic.php?t=26141]
* [Class] IniFile by bmcclure: [http://www.autohotkey.com/forum/viewtopic.php?t=41506]
* [module] Ini by majkinetor: [http://www.autohotkey.com/forum/viewtopic.php?t=22495]
* Auto read,load and save by Superfraggle: [http://www.autohotkey.com/forum/viewtopic.php?t=21346]
* globalsFromIni by Tuncay: [http://www.autohotkey.com/forum/viewtopic.php?t=27928]
* Read .INI file in one go by Smurth: [http://www.autohotkey.com/forum/viewtopic.php?t=36601]
About: Examples
Usage:
(Code)
value := ini_getValue(ini, "Section", "Key") ; <- Get value of a key.
value := ini_getValue(ini, "", "Key") ; <- Get value of first found key.
key := ini_getKey(ini, "Section", "Key") ; <- Get key/value pair.
section := ini_getSection(ini, "Section") ; <- Get full section with all keys.
ini_replaceValue(ini, "Section", "Key", A_Now) ; -> Update value of a key.
ini_replaceKey(ini, "Section", "Key") ; -> Delete a key.
ini_replaceSection(ini, "Section", "[Section1]Key1=0`nKey2=1") ; -> Replace a section with all its keys.
ini_insertValue(ini, "Section", "Key" ",ListItem") ; -> Add a value to existing value.
ini_insertKey(ini, "Section", "Key=" . A_Now) ; -> Add a key/value pair.
ini_insertSection(ini, "Section", "Key1=ini`nKey2=Tuncay") ; -> Add a section.
keys := ini_getAllKeyNames(ini, "Section") ; <- Get a list of all key names.
sections := ini_getAllSectionNames(ini) ; <- Get a list of all section names.
(End Code)
About: Functions
Parameters:
Content - Content of an ini file (also this can be one section
only).
Section - Unique name of the section. Some functions support the
default empty string "". This leads to look up at
first found section.
Key - Name of the variable under the section.
Replacement - New content to use.
PreserveSpace - Should be set to 1 if spaces around the value of a key
should be saved, otherwise they are lost. The
surrounding single or double quotes are also lost.
The 'get' functions returns the desired contents without touching the
variable.
The 'replace' and 'insert' functions changes the desired content directly
and returns 1 for success and 0 otherwise.
There are some more type of functions and parameters. But these are not listed
here.
Remarks:
On success, ErrorLevel is set to '0'. Otherwise ErrorLevel is set to '1' if
key under desired section is not found.
The functions are not designed to be used in all situations. On rare
conditions, the result could be corrupt or not usable. In example, there
is no handling of commas inside the key or section names.
Any "\E" would end the literal sequence and switch back to regex. The
"\E" sequence is not escaped, because its very uncommon to use backslashes
inside key and section names. To workaround this, replace at every key or
section name the "\E" part with "\E\\E\Q":
> Name := "Folder\Edit\Test1"
> IfInString, Name, \
> {
> StringReplace, Name, Name, \E, \E\\E\Q, All
> }
> MsgBox % ini_getValue(ini, "paths", Name)
This allows us to work with regex, but then at the end it should be closed
with "\Q" again.
> ; Used regex at keyname: "Time.*"
> value := ini_getValue(ini, "Tip", "\ETime.*\Q")
*/
Only full libraries or applications have such comprehensive headers. But nearly all published scripts have at least the most of these basics, which are the following: License, Link(s), Date, Revision, Developers / Author, Category, Type, Standalone, StdLibConform.