This is how I parse a ini file in JS. Perhaps something that could be done with MS?
Code: Select all
'iniFileToObject': function(string, pipe2array = false) {
let object = {};
let sections = string.match(/^\[[^\]\r\n]+](?:[\r\n]([^[\r\n].*)?)*/gm);
for (let i=0; i < sections.length; i++) {
let sectionName = sections[i].split("\n")[0];
sectionName = sectionName.replace(/[\[\]]/g, '').trim();
object[sectionName] = {};
let key = sections[i].match(/^((?!\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$).)+/gm); //we remove comments "//" syntax, single row
for (let k=1; k < key.length; k++) {
let keyValue = key[k].split("=");
if ((keyValue[1].charAt(0) === "0" && keyValue[1].length > 1) || isNaN(Number(keyValue[1]))) { // leading zeros are interpreted as string values
if (pipe2array && keyValue[1].trim().includes("|")) {
object[sectionName][keyValue[0].trim()] = keyValue[1].trim().split("|");
} else if (keyValue[1].trim() === "null") {
object[sectionName][keyValue[0].trim()] = null;
} else {
object[sectionName][keyValue[0].trim()] = keyValue[1].trim();
}
} else {
object[sectionName][keyValue[0].trim()] = Number(keyValue[1]);
}
}
}
return object;
}
I have added a "pipe2array" thing to be able to have some keys automatically turned into arrays by separating them with a | "pipe".
This parser allow for comments on single rows or on key value rows using "//". Section rows must not contain any comments.
Writing to a ini file would be just to check if a section and key is already present, if so replace the string of that key with the new value, remember the comment if any and add it to the end of the string.
If no section and/or key is found, inject a new line with this information.
For easier parsing you may even allow for the ini file comments to be part of the ini object as simply add them as such:
SECTION1=section1s_name
SECTION1_ROW=row_of_section_in_ini_file
SECTION1_KEY1=keyname
SECTION1_KEY1_ROW=row_of_key_in_ini_file
SECTION1_KEY1_VALUE=value_of_key1
SECTION1_KEY1_COMMENT=comment_found_after_value
...
SECTIONn=section1s_name
SECTIONn_ROW=row_of_section_in_ini_file
SECTIONn_KEYn=keyname
SECTIONn_KEYn_ROW=row_of_key_in_ini_file
SECTIONn_KEYn_VALUE=value_of_keyN
SECTIONn_KEYn_COMMENT=comment_found_after_value
+
INI_FILE_COMMENT1=comment_string
INI_FILE_COMMENT1_ROW=row_of_comment_in_ini_file
...
INI_FILE_COMMENTn=comment_string
INI_FILE_COMMENTn_ROW=row_of_comment_in_ini_file
Just as an idea of how you could implement it, I'm not suggesting you should allow for the user to see this information.
You could then say that a ini file syntax must be used as such:
Sections [section] must reside on a single row with no comments, spaces before and after a section entry will be trimmed away.
Key-value entries must reside on a single row, spaces before and after a key will be trimmed away.
Key must be followed by a = or else the row will be treated as a comment.
Values must be single row and everything after a "//" is treated as a comment.
This way you wouldn't reinvent the wheel, simply make a better propulsion mechanism. And I would love MS even more
