cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
vince_faller
Super User (Alumni)

Anyone have a good JSL .ini file reader?

I'm imagining

 

[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString
y=28

becoming

[
	"Thing1"=>
		["x"=>14, //not sure if this should be a number or string
		"y"=>"String", 
		"z"=>"..."], 
	"Thing2"=>
		["x"=>"DifferentString", 
		"y"=>28]
]
Vince Faller - Predictum
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Anyone have a good JSL .ini file reader?

100% agree than XML and JSON and dumped Associative Arrays are not intended to be edited in notepad. And it does not always make sense to write your own editor, so .INI files still have their place.

Craige

View solution in original post

6 REPLIES 6
jthi
Super User

Re: Anyone have a good JSL .ini file reader?

I think I did something like this when I wanted to use same config file in python and JMP, but gave up dont remember why. But I had to do some parsing for Minitab MAC files and the same idea might work here. 

 

Below is modified script to fit this type of strings. Might require adding Trim Whitespace:

Names Default To Here(1);
config_file = "[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString
y=28
";

aa_ini = Associative Array();
cur_key = "";
For Each({line}, Words(config_file, "\!N"),
	If(Ends With(line, "]") & Starts With(line, "["),
		new_key = Word(1, line, "[]");
		aa_ini[Word(1, line, "[]")] = Associative Array();
		cur_key = new_key;
	,
		{key, value} = Words(line, "=");
		//string conversion.. expect everything to be numbers and then try to convert
		new_value = If(IsMissing(num(value)),
			value,
			num(value)
		);
		aa_ini[cur_key][key] = new_value;
	);
);
show(aa_ini);

aa_ini = ["Thing1" => ["x " => 14, "y " => " String", "z " => " ..."], "Thing2" => ["x" => "DifferentString", "y" => 28]];
-Jarmo
Craige_Hales
Super User

Re: Anyone have a good JSL .ini file reader?

nice! It does need trim for key and value.

config_file =
"[Thing1]
x = 14
y = String
z = ...

[Thing2]
x=DifferentString with space
y=28
";

aa_ini = Associative Array();
cur_key = "";
For Each( {line}, Words( config_file, "\!N" ),
    If( Ends With( line, "]" ) & Starts With( line, "[" ),
        new_key = Word( 1, line, "[]" );
        aa_ini[Word( 1, line, "[]" )] = Associative Array();
        cur_key = new_key;
    ,
        {key, value} = Words( line, "=" );
		//string conversion.. expect everything to be numbers and then try to convert
        new_value = If( Is Missing( Num( value ) ),
            Trim( value ),
            Num( value )
        );
        aa_ini[cur_key][Trim( key )] = new_value;
    )
);

Write( As JSON Expr( aa_ini ) );

 

 

Move those .ini files into the current century!

 

 

{"Thing1":{"x":14,"y":"String","z":"..."},"Thing2":{"x":"DifferentString with space","y":28}}

 

Sometimes it is hard to tell if JSON is a step forward or backwards.

 

Craige
jthi
Super User

Re: Anyone have a good JSL .ini file reader?

I prefer ini/config files over JSON in simple cases because they are usually easier to read and edit directly from the file (be this either good or bad thing) and when I did work with LabVIEW the format got familiar configuration files VI.

 

Currently thinking how I would handle fairly simple configuration files in few of my scripts. I will have to play around with just using .jsl or json/.ini files.

 

 

Names Default To Here(1);

read_ini_file = function({ini_str}, {Default Local},
	aa_ini = Associative Array();
	cur_key = "";
	For Each({line}, Words(ini_str, "\!N"),
		If(Ends With(line, "]") & Starts With(line, "["),
			new_key = Word(1, line, "[]");
			aa_ini[Word(1, line, "[]")] = Associative Array();
			cur_key = new_key;
		,
			{key, value} = Words(line, "=");
			//string conversion.. expect everything to be numbers and then try to convert
			new_value = If(Is Missing(Num(value)),
				Trim(value),
				Num(value)
			);
			aa_ini[cur_key][Trim(key)] = new_value;
		)
	);
	return(aa_ini);
);

aa_to_ini = function({aa_section}, {Default Local},
	ini_str = "";
	section = "\!N[^key_section^]\!N";
	key_value = "^key^=^value^\!N";
	For Each({{key_section, value_section}}, aa_section,
		ini_str ||= Eval Insert(section);
		For Each({{key, value}}, value_section,
			ini_str ||= Eval Insert(key_value);
		);
	);
	return(Trim Whitespace(ini_str,left));
);

config_file = "[Thing1]
x=14
y=String
z=...

[Thing2]
x=DifferentString with space
y=28
";

aa_test = read_ini_file(config_file);
back_to_ini = aa_to_ini(aa_test);

show(aa_test,back_to_ini,config_file);

//back_to_ini == config_file;
//aa_test == read_ini_file(back_to_ini)
//Save Text File("$TEMP/DeleteMe.txt", back_to_ini);
//Open("$TEMP");

//config_file = Load Text File("$TEMP/DeleteMe.txt");
//aa_test == read_ini_file(back_to_ini);
//aa_test["Thing1"]["x"]

 

 

 

-Jarmo
Craige_Hales
Super User

Re: Anyone have a good JSL .ini file reader?

100% agree than XML and JSON and dumped Associative Arrays are not intended to be edited in notepad. And it does not always make sense to write your own editor, so .INI files still have their place.

Craige
pmroz
Super User

Re: Anyone have a good JSL .ini file reader?

I routinely read and write associative arrays to an Oracle database.  This is a similar approach for an .ini file:

 

/*
Function Name: char_to_aa

Description:    Convert a character string to an associative array
*/
char_to_aa = Function({one_char_aa},

	if (contains(one_char_aa, "Associative Array"),
		one_aa = eval(parse(one_char_aa));
		,
		one_aa = parse(one_char_aa);
	);
	one_aa;
);
//---------------------------------------------------------------------
// Now try this out
aa = associative array();
aa["Thing1"] = {14, "String", "z-value"};
aa["Thing2"] = {"DifferentString", 28, "other-z-value"};

// Convert the associative array to a char
char_aa = char(aa);
// Save it to a file
save text file("$temp\myfile.ini", char_aa);
write(char_aa);

// Convert it back to an associative array
new_char_aa = load text file("$temp\myfile.ini");
new_aa = char_to_aa(new_char_aa);
print(new_aa);
vince_faller
Super User (Alumni)

Re: Anyone have a good JSL .ini file reader?

Coolio.  Yeah. I'm really just trying to do the exact same thing of consolidating my configs for multilple langugaes/frameworks.
One uses XML, one YML, one TOML, and then I have python and JSL.  I personally like the YML for readability and commenting. 

I realized after I asked this I can't really use INI because I need nesting (though I could just have some naming convention and deal with it on my end)

 

This is super useful still.  Thanks all.  

Vince Faller - Predictum