Difference between revisions of "Libs-dev/libini"

From KolibriOS wiki
Jump to navigation Jump to search
m
(Usage example added)
Line 1: Line 1:
INI files are structured human-readable text files. Each file consists of one or more sections (text within square braces), each having several keys (text before "=" sign) with corresponding values (text after "=" sign). Here's the example of such file:
+
INI files are structured human-readable text files. Each file consists of one or more sections, each having several keys with values. Here's the example of such file:
  
 
<source lang="ini">
 
<source lang="ini">
Line 11: Line 11:
 
</source>
 
</source>
  
Section names should be unique within file scope. Key names should be unique within section scope. This means, there could be keys with same names located in different sections, but no two sections with same name within a file.
+
Section names are embraced with square braces and are one-line strings which may contain any characters. Sections divide file into parts, where keys and their corresponding values are specified. In the begining of file (before the first section) there may also be keys and values found which aren't in any section. Keys and values are also one-line strings which are separated from each other with equality sign "=". Key name of course can't contain "=" character. Key value is the string after first "=" character found in line till the end on line.
 +
 
 +
Section names, key names and key values are trimmed i.e. space characters are deleted from the beginning and end of the strings during comparison.
 +
 
 +
If each section identifies the set of keys, each key in turn identifies one value. Section and key names don't have to be unique, but if there're two keys (within a section) with the same name, value will be read and written from the one which appears earlier in the file.  
  
 
=Functions (enumerating entities)=
 
=Functions (enumerating entities)=
Line 215: Line 219:
 
:<tt><nowiki>[[_modifiers]]</nowiki></tt>
 
:<tt><nowiki>[[_modifiers]]</nowiki></tt>
 
::unchanged (error) / modifiers state for this shortcut <int>
 
::unchanged (error) / modifiers state for this shortcut <int>
 +
 +
=Usage examples=
 +
 +
This one enumerates sections, and then enumerates keys for each section in its callback:
 +
 +
<source>
 +
; ...
 +
 +
        invoke  ini_enum_sections, filename, ini_section_callback
 +
 +
; ...
 +
 +
proc ini_section_callback _filename, _section_name
 +
        ; this function is being called by library for each section found in the file
 +
 +
        invoke  ini_enum_keys, [_filename], [_section_name], ini_key_callback
 +
 +
        ; ...
 +
 +
        ret
 +
endp
 +
 +
proc ini_key_callback _filename, _section_name, _key_name, _key_value
 +
        ; this function is being called by library for each key of each section found in the file
 +
 +
        ; ...
 +
 +
        ret
 +
endp
 +
 +
; ...
 +
 +
filename db '/hd0/1/a.ini', 0
 +
</source>
  
 
[[Category:Coding]][[Category:Manuals]]
 
[[Category:Coding]][[Category:Manuals]]

Revision as of 12:36, 23 July 2010

INI files are structured human-readable text files. Each file consists of one or more sections, each having several keys with values. Here's the example of such file:

[fruit colors]
banana = yellow
orange = orange
strawberry = red

[vegetable colors]
cucumber = green

Section names are embraced with square braces and are one-line strings which may contain any characters. Sections divide file into parts, where keys and their corresponding values are specified. In the begining of file (before the first section) there may also be keys and values found which aren't in any section. Keys and values are also one-line strings which are separated from each other with equality sign "=". Key name of course can't contain "=" character. Key value is the string after first "=" character found in line till the end on line.

Section names, key names and key values are trimmed i.e. space characters are deleted from the beginning and end of the strings during comparison.

If each section identifies the set of keys, each key in turn identifies one value. Section and key names don't have to be unique, but if there're two keys (within a section) with the same name, value will be read and written from the one which appears earlier in the file.

Functions (enumerating entities)

ini_enum_sections

Enumerate sections, calling callback function for each of them.

Prototype:

proc ini.enum_sections _f_name, _callback

Arguments:

_f_name
ini filename <asciiz>
_callback
callback function address: func(f_name, sec_name), where
f_name
ini filename (as passed to the function) <asciiz>
sec_name
section name found <asciiz>

Result:

eax
-1 (error) / 0

ini_enum_keys

Enumerate keys within a section, calling callback function for each of them.

Prototype:

proc ini.enum_keys _f_name, _sec_name, _callback

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_callback
callback function address: func(f_name, sec_name, key_name, key_value), where
f_name
ini filename (as passed to the function) <asciiz>
sec_name
section name (as passed to the function) <asciiz>
key_name
key name found <asciiz>
key_value
value of key found <asciiz>

Result:

eax
-1 (error) / 0

Functions (getting and setting values)

All the getter functions are fail-safe, returning supplied default value on any error.

ini_get_str

Read string.

Prototype:

proc ini.get_str _f_name, _sec_name, _key_name, _buffer, _buf_len, _def_val

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_buffer
destination buffer address <byte*>
_buf_len
buffer size (maximum bytes to read) <dword>
_def_val
default value to return if no key, section or file found <asciiz>

Result:

eax
-1 (error) / 0
[_buffer]
[_def_val] (error) / found key value <asciiz>

ini_set_str

Write string.

Prototype:

proc ini.set_str _f_name, _sec_name, _key_name, _buffer, _buf_len

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_buffer
source buffer address <byte*>
_buf_len
buffer size (bytes to write) <dword>

Result:

eax
-1 (error) / 0

ini_get_int

Read integer.

Prototype:

proc ini.get_int _f_name, _sec_name, _key_name, _def_val

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_def_val
default value to return if no key, section or file found <dword>

Result:

eax
[_def_val] (error) / found key value <dword>

ini_set_int

Write integer.

Prototype:

proc ini.set_int _f_name, _sec_name, _key_name, _val

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_val
value <dword>

Result:

eax
-1 (error) / 0

ini_get_color

Read color.

Prototype:

proc ini.get_color _f_name, _sec_name, _key_name, _def_val

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_def_val
default value to return if no key, section or file found <dword>

Result:

eax
[_def_val] (error) / found key value <dword>

ini_set_color

Write color.

Prototype:

proc ini.set_color _f_name, _sec_name, _key_name, _val

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_val
value <dword>

Result:

eax
-1 (error) / 0

ini_get_shortcut

Read shortcut key.

Prototype:

proc ini.get_shortcut _f_name, _sec_name, _key_name, _def_val, _modifiers

Arguments:

_f_name
ini filename <asciiz>
_sec_name
section name <asciiz>
_key_name
key name <asciiz>
_def_val
default value to return if no key, section or file found <dword>
_modifiers
pointer to dword variable which receives modifiers state as in 66.4 <dword*>

Result:

eax
[_def_val] (error) / shortcut key value as scancode <int>
[[_modifiers]]
unchanged (error) / modifiers state for this shortcut <int>

Usage examples

This one enumerates sections, and then enumerates keys for each section in its callback:

; ...

        invoke  ini_enum_sections, filename, ini_section_callback

; ...

proc ini_section_callback _filename, _section_name
        ; this function is being called by library for each section found in the file

        invoke  ini_enum_keys, [_filename], [_section_name], ini_key_callback

        ; ...

        ret
endp 

proc ini_key_callback _filename, _section_name, _key_name, _key_value
        ; this function is being called by library for each key of each section found in the file

        ; ...

        ret
endp

; ...

filename db '/hd0/1/a.ini', 0