Skip to content

Commit

Permalink
Support for #define rusefi/web_backend#214
Browse files Browse the repository at this point in the history
  • Loading branch information
andreika-git committed Jul 17, 2023
1 parent a0d26c7 commit ee63c36
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static function parse($file, $msq, $optionValues)
$conditions = array();

$directives = array();
$defines = array();
$skipByDirective = false;
$settings = INI::getDefaultSettings($optionValues);
$curSettingGroup = NULL;
Expand All @@ -159,7 +160,7 @@ public static function parse($file, $msq, $optionValues)
if ($line == '' || $line[0] == ';') continue;
if ($line[0] == '#')
{
$skipByDirective = INI::processDirective($line, $directives, $settings);
$skipByDirective = INI::processDirective($line, $directives, $defines, $settings);
continue;
}

Expand Down Expand Up @@ -202,19 +203,19 @@ public static function parse($file, $msq, $optionValues)
if ($hasComment !== FALSE) $value = substr($value, 0, $hasComment);

$value = trim($value);

switch ($currentSection)
{
case "Constants": //The start of our journey. Fill in details about variables.
$values[$currentSection][$key] = INI::defaultSectionHandler($value, false, $msq, $outputs);
$values[$currentSection][$key] = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
break;

case "SettingContextHelp": //Any help text for our variable
$values[$currentSection][$key] = trim($value);
break;

case "Menu":
$menu = INI::defaultSectionHandler($value, true, $msq, $outputs);
$menu = INI::defaultSectionHandler($value, true, $msq, $defines, $outputs);
if (is_array($menu)) {
if ($condition !== NULL) {
$menu[count($menu) - 1] = $condition;
Expand All @@ -232,7 +233,7 @@ public static function parse($file, $msq, $optionValues)
case "UserDefined":
if ($key == "dialog")
{
$curDialog = INI::defaultSectionHandler($value, false, $msq, $outputs);
$curDialog = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
if (!is_array($curDialog))
$curDialog = array($curDialog);
} else if ($key == "indicatorPanel") {
Expand All @@ -241,7 +242,7 @@ public static function parse($file, $msq, $optionValues)

if (is_array($curDialog))
{
$dlg = INI::defaultSectionHandler($value, false, $msq, $outputs);
$dlg = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
if (is_array($dlg)) {
if ($condition !== NULL) {
foreach ($dlg as &$d) {
Expand Down Expand Up @@ -394,14 +395,14 @@ public static function parse($file, $msq, $optionValues)
break;

case "OutputChannels": //These are for gauges and datalogging
$v = INI::defaultSectionHandler($value, false, $msq, $outputs);
$v = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
// here we store only computable outputs with expressions
if (isset($v[0]) && strpos($v[0], '{') !== FALSE && $condition !== NULL) {
$outputs["outputs"][$key] = $condition;
}
break;
case "SettingGroups": //misc settings
$values = INI::defaultSectionHandler($value, false, $msq, $outputs);
$values = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
if ($key == "settingGroup") {
$curSettingGroup = isset($settings[$key]) ? count($settings[$key]) : 0;
// this will be the options list
Expand All @@ -416,7 +417,7 @@ public static function parse($file, $msq, $optionValues)

break;
case "PcVariables":
$values[$currentSection][$key] = INI::defaultSectionHandler($value, false, $msq, $outputs);
$values[$currentSection][$key] = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
break;
//Don't care about these
case "Datalog": //Not relevant
Expand All @@ -436,7 +437,7 @@ public static function parse($file, $msq, $optionValues)
case NULL:
//Should be global values (don't think any ini's have them)
assert($currentSection === NULL);
$globals[$key] = INI::defaultSectionHandler($value, false, $msq, $outputs);
$globals[$key] = INI::defaultSectionHandler($value, false, $msq, $defines, $outputs);
break;
}
}
Expand All @@ -450,14 +451,23 @@ public static function parse($file, $msq, $optionValues)
* @param $value
* @returns An array if there's a comma, or just the value.
*/
private static function defaultSectionHandler($value, $isLessStrict = false, $msq, $outputs)
private static function defaultSectionHandler($value, $isLessStrict = false, $msq, $defines, $outputs)
{
if (strpos($value, '$') !== 0) {
$value = preg_replace_callback('/\$(\w+)/', function ($m) use($defines) {
if (array_key_exists($m[1], $defines)) {
return $defines[$m[1]];
}
return $m[0];
}, $value);
}

//For things like "nCylinders = bits, U08, 0,"
//split CSV into an array
if (strpos($value, ',') !== FALSE || $isLessStrict) {
// a simple explode() by comma is not enough since we have expressions like "text,text"
$v = $value;
if (preg_match_all("/\"[^\"]*\"|[A-Za-z0-9_\.\[\]\:]+|{[^\}]+}/", $value, $ret))
if (preg_match_all("/\"[^\"]*\"|[A-Za-z0-9_\.\[\]\:\$]+|{[^\}]+}/", $value, $ret))
$v = $ret[0];
if (count($v) == 1)
$v = $v[0];
Expand Down Expand Up @@ -532,7 +542,7 @@ public static function parseExpression($line, $msq, $outputs, &$outFlags = NULL)
return "return " . implode(" ", $lexemes) . ";";
}

public static function processDirective($line, &$directives, $settings)
public static function processDirective($line, &$directives, &$defines, $settings)
{
$c = count($directives) - 1;
if (preg_match("/#if\s+([A-Za-z0-9]+)/", $line, $ret)) {
Expand All @@ -547,6 +557,9 @@ public static function processDirective($line, &$directives, $settings)
else if (preg_match("/#endif/", $line, $ret)) {
array_pop($directives);
}
else if (preg_match("/#define\s+(\w+)=(.*)$/", $line, $ret)) {
$defines[$ret[1]] = $ret[2];
}
return count($directives) != 0 && end($directives) != 1;
}

Expand Down

0 comments on commit ee63c36

Please sign in to comment.