Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting - Added multiline setting parsing support #1581

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions addons/settings/fnc_parse.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,58 @@ Returns:
Settings with values and priority states. <ARRAY>

Author:
commy2
commy2, johnb43
---------------------------------------------------------------------------- */

// parses bool, number, string
private _fnc_parseAny = {
params ["_string"];

// Remove whitespace so parseSimpleArray can handle arrays.
// Means that strings inside arrays don't support white space chars, but w/e.
// No such setting exists atm anyway.
if (_string find """" != 0) then {
_string = _string splitString WHITESPACE joinString "";
};

parseSimpleArray (["[", _string, "]"] joinString "") select 0
};

params [["_info", "", [""]], ["_validate", false, [false]], ["_source", "", [""]]];

// remove whitespace at start and end of each line
private _result = [];
// Remove whitespace at start and end of each line, a line being define by the ";" at its end
johnb432 marked this conversation as resolved.
Show resolved Hide resolved
private _parsed = [];

{
_result pushBack (_x call CBA_fnc_trim);
} forEach (_info splitString NEWLINE);
_parsed pushBack (trim _x);
} forEach (_info splitString ";");

{
if (_x select [count _x - 1] != ";") then {
_result set [_forEachIndex, _x + ";"];
};
} forEach _result;
// Remove empty strings
_parsed = _parsed - [""];

_info = (_result joinString NEWLINE) + NEWLINE;

// separate statements (setting = value)
_result = [];
// Separate statements (setting = value)
private _result = [];
private _indexEqualSign = -1;
private _setting = "";
private _priority = 0;
private _value = "";
private _countForce = count "force";
private _whitespaces = [toString [ASCII_NEWLINE], toString [ASCII_CARRIAGE_RETURN], toString [ASCII_TAB], toString [ASCII_SPACE]];
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

{
private _indexEqualSign = _x find "=";
_indexEqualSign = _x find "=";

private _setting = (_x select [0, _indexEqualSign]) call CBA_fnc_rightTrim;
private _priority = 0;
// Setting name is front of "="
_setting = (_x select [0, _indexEqualSign]) trim [WHITESPACE, 2];
_priority = 0;

if (_setting select [0, count "force"] == "force") then {
_setting = _setting select [count "force"] call CBA_fnc_leftTrim;
// Check if the first entry is force and not followed by an empty space
if (_setting select [0, _countForce] == "force" && {(_setting select [_countForce, 1]) in _whitespaces}) then {
_setting = (_setting select [_countForce]) trim [WHITESPACE, 1];
_priority = _priority + 1;
};

if (_setting select [0, count "force"] == "force") then {
_setting = _setting select [count "force"] call CBA_fnc_leftTrim;
// Check if the second entry is force and not followed by an empty space
if (_setting select [0, _countForce] == "force" && {(_setting select [_countForce, 1]) in _whitespaces}) then {
_setting = (_setting select [_countForce]) trim [WHITESPACE, 1];
_priority = _priority + 1;
};

// If setting is valid, get it's value
if (_setting != "") then {
private _value = ((_x select [_indexEqualSign + 1]) call CBA_fnc_trim) call _fnc_parseAny;
// Remove whitespaces; Parse bool, number, string
_value = parseSimpleArray (["[", (_x select [_indexEqualSign + 1]) splitString WHITESPACE joinString "", "]"] joinString "") select 0;
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

if !(_validate) then {
_result pushBack [_setting, _value, _priority];
} else {
// Check if setting is valid
if (isNil {[_setting, "default"] call FUNC(get)}) exitWith {
ERROR_1("Setting %1 does not exist.",_setting);
};
Expand All @@ -84,6 +77,6 @@ _result = [];
_result pushBack [_setting, _value, _priority];
};
};
} forEach ([_info, ";" + NEWLINE] call CBA_fnc_split);
} forEach _parsed;

_result