Reading and writing settings in ini-files
Submitted by Saturday, 26 November, 2011 - 13:05
on
Hi, I wanted to ask if anybody might have written a beanshell macro or function to be able to read and write settings in inifiles. The jedit properties do not handle the [Section] parts of the inifiles.
I have been trying to rewrite some functions written for the autohotkey language but the regexes seem to go over my head. I managed to scramble together one function (ini_get, see below), but it was more trial and error than knowing what I was doing, and I was wondering if someone already has written something similar.
Code:
ini_get(_Content, _Section, _Key, Flags, Default)
{
_Section = "\\[\\s*?\\Q"+_Section+"\\E\\s*?]";
//-- Note: The regex of this function was rewritten by Mystiq.
RegEx = "(?im)(?:\\n|^)\\s*"+_Section+"\\s*(?:\\n\\s*|\\n\\s*.+\\s*=\\s*.*?\\s*(?=\\n)|\\n\\s*[;#].*?(?=\\n))*\\n(\\s*\\Q"+_Key+"\\E\\s*=(.*))(?=\\n|$)";
//-- Kompilovat regex expression
Pattern p = Pattern.compile(RegEx);
//-- Vytvořit matcher (boolean)
Matcher m = p.matcher(_Content);
if(m.find())
{
Section = m.group(0);
Key = m.group(1);
Value = m.group(2);
}
if(Flags.indexOf('s') != -1)
return Section;
else if(Flags.indexOf('v') != -1)
return Value;
else if(Flags == null || Flags == "" || Flags.indexOf('k') != -1)
return Key;
}
Regards, tvojeho
I have been trying to rewrite some functions written for the autohotkey language but the regexes seem to go over my head. I managed to scramble together one function (ini_get, see below), but it was more trial and error than knowing what I was doing, and I was wondering if someone already has written something similar.
Code:
ini_get(_Content, _Section, _Key, Flags, Default)
{
_Section = "\\[\\s*?\\Q"+_Section+"\\E\\s*?]";
//-- Note: The regex of this function was rewritten by Mystiq.
RegEx = "(?im)(?:\\n|^)\\s*"+_Section+"\\s*(?:\\n\\s*|\\n\\s*.+\\s*=\\s*.*?\\s*(?=\\n)|\\n\\s*[;#].*?(?=\\n))*\\n(\\s*\\Q"+_Key+"\\E\\s*=(.*))(?=\\n|$)";
//-- Kompilovat regex expression
Pattern p = Pattern.compile(RegEx);
//-- Vytvořit matcher (boolean)
Matcher m = p.matcher(_Content);
if(m.find())
{
Section = m.group(0);
Key = m.group(1);
Value = m.group(2);
}
if(Flags.indexOf('s') != -1)
return Section;
else if(Flags.indexOf('v') != -1)
return Value;
else if(Flags == null || Flags == "" || Flags.indexOf('k') != -1)
return Key;
}
Regards, tvojeho