# $0 - the name of the setting
# More functions and values can be added in future versions.
#
+# The settings can be conditional thanks to using statements like:
+# if
+# else
+# endif
+#
+# Anything after the "if" keyword is the codition to check.
+# Condition is considered true if its value is not an empty string
+# Condition is parsed the same way as a dynamic setting definition
+#
+# Conditional statements must start at from first column in the line.
+# Conditional statements can be nested
+#
+# Example:
+# s1: 1
+# if $s1
+# s3 = $s1
+# if $s2
+# s3 = $s2
+# endif
+# else
+# s3: 0
+# endif
+# Here the value of s3 is '1'.
+#
+#
# Inserting settings to source files:
#
# After all settings are read, the actual source is processed line by line.
my $parse_mode = 0;
my $name = '';
my $value = '';
+ my $if_depth = 0;
+ my $if_block = 0;
while (defined(my $line = <$file>)) {
$line =~ s/[\r\n]//g;
# new definition name: value
if ($line =~ /^([A-Za-z0-9_\-\.]+):[ \t](.*)$/) {
$parse_mode = 0;
- $name = $1;
- $value = $2;
- $cfg{$name} = '';
- print_debug($depth, "STATIC DEFINE $name=$value")
+ unless ($if_block) {
+ $name = $1;
+ $value = $2;
+ $cfg{$name} = '';
+ print_debug($depth, "STATIC DEFINE $name=$value")
+ }
}
# new definition name = value
elsif ($line =~ /^([A-Za-z0-9_\-\.]+)[ \t]*=(.*)$/) {
$parse_mode = 1;
- $name = $1;
- $value = $2;
- $cfg{$name} = '';
- print_debug($depth, "DYNAMIC DEFINE $name=$value")
+ unless ($if_block) {
+ $name = $1;
+ $value = $2;
+ $cfg{$name} = '';
+ print_debug($depth, "DYNAMIC DEFINE $name=$value")
+ }
}
# continued definition
elsif ($line =~ /^[ \t](.*)$/) {
- $value = "\n".$1;
- print_debug($depth, "CONTINUE $name $value")
+ unless ($if_block) {
+ $value = "\n".$1;
+ print_debug($depth, "CONTINUE $name $value")
+ }
}
# include file
- elsif ($line =~ /^include[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/)
- {
- my $path = parse_value($1, $depth+1, %cfg);
- print_debug($depth, "INCLUDE $path");
- %cfg = parse_file($path, $encoding, $depth+1, %cfg);
+ elsif ($line =~ /^include[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/) {
+ unless ($if_block) {
+ my $path = parse_value($1, $depth+1, %cfg);
+ print_debug($depth, "INCLUDE $path");
+ %cfg = parse_file($path, $encoding, $depth+1, %cfg);
+ $name = '';
+ $value = '';
+ }
+ }
+ # if
+ elsif ($line =~ /^if[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/) {
+ $if_depth += 1;
+ unless ($if_block) {
+ my $cond = parse_value($1, $depth+1, %cfg);
+ print_debug($depth, "IF.$if_depth $cond");
+ unless ($cond ne '') {
+ $if_block = $if_depth;
+ }
+ }
+ $name = '';
+ $value = '';
+ }
+ # endif
+ elsif ($line =~ /^endif([ \t].*)?$/) {
+ my $_if_depth = $if_depth;
+ if ($if_depth > 0) {
+ $if_depth -= 1;
+ }
+ if ($if_depth < $if_block) {
+ $if_block = 0;
+ }
+ unless ($if_block) {
+ print_debug($depth, "ENDIF.$_if_depth");
+ }
$name = '';
$value = '';
}
+ # else
+ elsif ($line =~ /^else([ \t].*)?$/) {
+ unless ($if_block) {
+ $if_block = $if_depth;
+ print_debug($depth, "ELSE.$if_depth");
+ }
+ elsif ($if_depth == $if_block) {
+ $if_block = 0;
+ print_debug($depth, "ELSE.$if_depth");
+ }
+ $name = '';
+ $value = '';
+ }
+ # no el(s)if at this point
+
# other line, not understood
else {
$name = '';
$value = '';
}
- if ($name ne '') {
+ if (($name ne '') and (not $if_block)) {
if ($parse_mode != 0) {
$value = parse_value($value, $depth+1, %cfg);
}