Skip to content

How the parser assigns relative key and section ids?

Yashas Samaga edited this page May 8, 2015 · 1 revision

#How the parser assigns relative key/section ids? The parser checks each and every line and stores the information regarding the line in an user-defined enumerator array.It uses linked lists to the store the lines.Once the parser identifies the type of the line, it creates a key or a section element in an user-defined enumerator array of keys or sections respectively.The keys are also stored in linked lists.Each section and key is given an id based on the segment-offset method.

Segment-Offset Method:

A segment is an index or an address which indicates the start of a region of memory and offset is also an index or an address which tells how far the variable from the segment is situated in the memory.

If you have had a glimpse through the include, you would have probably noticed these

static stock INI_Line[INI_MAX_MULTI_FILES*INI_MAX_LINES][E_INI_LINE];
static stock INI_Section[INI_MAX_MULTI_FILES*INI_MAX_SECTIONS][E_INI_SECTION];
static stock INI_Key[INI_MAX_MULTI_FILES*INI_MAX_KEYS][E_INI_KEY];

Let's investigate INI_Key.The number of elements with INI_Key is INI_MAX_MULTI_FILES*INI_MAX_KEYS.This variable 'INI_Key' stores the Keys for all the keys of different files.Each file gets its segment and each key gets an offset.The indexes that belong to the first file would be from 0 to INI_MAX_KEYS and of the second file would be INI_MAX_KEYS to INI_MAX_KEYS*2 and so on...

For nth file, INI_MAX_KEYS*(n-1) to INI_MAX_KEYS*n belongs to the nth INI Handle.

Now to access a key of the file, we would need an offset.The offset indicates how far away from the segment the key is.

Lets take an example:

[SECTION1]
key1=123
key2=4534
[SECTION2]
key3=23423
.
.
.

Lets assume that this was the second file that was opened and has been assigned the INI Handle 2.So this handle would own the keys with index starting from INI_MAX_KEYS to INI_MAX_KEYS*2.So its starting index of the segment would be INI_MAX_LINES.The key one(i.e:"key1") gets its absolute index as INI_MAX_KEYS and offset as 0, the second key(i.e:"key2") gets an absolute index as INI_MAX_KEYSS+1, an offset of 1, third key(i.e:"key3") gets an absolute index as INI_MAX_KEYS+2 and offset as 2 and so on.All of these keys have the segment index as INI_MAX_KEYS.

This is how eINI assigns key ids.

Now to get the name of the key of key 'n' of this file, we use the following formula

INI_Key[segment + offset][E_INI_KEY_name];
INI_Key[INI_MAX_KEYS + n][E_INI_KEY_name]

INI_Key[INI_MAX_KEYS + 2][E_INI_KEY_name] //Gets the name of key3

I hope that now you have a fair idea about how indexing(giving ids) in eINI works.

So we would come across two terms while using eINI namely relative keyid/sectionid and absolute keyid/sectionid. This is exactly same as segment and offset.

The parser assigns IDs linearly.The first key of the file gets offset 0,second gets offset 1 and so on.Similarly the first section gets an offset of 1, second gets an offset of 2 and so on.Where did the offset 0 go?Its reserved for global section irrespective if there is any global key or not(global keys could be added later).

#####Why do we need to know this?** You can exploit the way the parser assigns IDs linearly to improve the performance of the script.Instead of asking eINI to search and find the section or the key, you could directly tell the Read or Write functions the keyids and sectionids knowing the file already which would improve the performance to a large extent(we would avoid so many strcmps for finding sections and keys). We'll see more examples later in the Reading and Writing Sections.

Example:

key1=asd
[SEC1]
key1=asdasd
key2=34234
[SEC2]
a=123
b=234 ;some gibberish..asdasd.sd.ad.as.da.sd.sad
c=345
[SEC3]
[SEC4]
note=the previous section was empty ;random comment

Relative Key ID Assignments:

  • key1 = 0 (Global Key, therefore has a sectionid 0)
  • key1 = 1(key1 of SEC1)
  • key2 = 2
  • a = 3
  • b= 4
  • c = 5

Relative Section ID Assignments:

  • Global Section = 0 (irrespective of if there is any element or not)
  • SEC1 = 1
  • SEC2 = 2
  • SEC3 = 3
  • SEC4 = 4