Mercurial > hg > mgallery
diff mgtool.php @ 322:2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 Apr 2020 11:55:08 +0300 |
parents | a4c6937dfaaf |
children | 9a4297e59a40 |
line wrap: on
line diff
--- a/mgtool.php Thu Apr 09 10:13:45 2020 +0300 +++ b/mgtool.php Thu Apr 09 11:55:08 2020 +0300 @@ -1337,6 +1337,93 @@ } +function mgGetDValStr($key, $mdata, $val, $format, $multi) +{ + $vfmt = "%s"; + + switch ($mdata[0]) + { + case MG_STR_ARRAY: + if ($val === FALSE) + $val = []; + else + if (is_array($val) || is_string($val)) + { + $vfmt = "\"%s\""; + if (is_string($val)) + $val = [ $val ]; + } + else + $val = "ERROR1"; + break; + + case MG_STR: + case MG_STR_LC: + if (is_string($val)) + { + $vfmt = "\"%s\""; + if ($mdata[0] == MG_STR_LC) + $val = strtolower($val); + } + else + $val = "ERROR2"; + break; + + case MG_BOOL: + $val = $val ? "yes" : "no"; + break; + + case MG_FLAGS: + { + $mstr = []; + foreach ($mdata[2] as $vkey => $vval) + { + if ($val & $vval) + $mstr[] = $vkey; + } + + $val = "\"".implode(" | ", $mstr)."\""; + } + break; + + case MG_INT: + $val = (string) $val; + break; + + default: + $val = "ERROR"; + } + + if (!is_array($val)) + { + if ($val === NULL || $val === FALSE) + $val = []; + else + $val = [ $val ]; + } + + $res = (count($val) == 0) ? "# " : ""; + + if ($multi) + { + if (count($val) > 0) + { + foreach ($val as $vv) + $res .= sprintf($format, $key, sprintf($vfmt, $vv)); + } + else + $res .= sprintf($format, $key, ""); + } + else + { + $res .= sprintf($format, $key, + implode(", ", array_map(function($vv) use ($vfmt) { return sprintf($vfmt, $vv); }, $val))); + } + + return $res; +} + + function mgShowCopyright() { global $mgProgVersion, $mgProgCopyright, @@ -1545,22 +1632,19 @@ case "config": case "dump": + foreach ($mgDefaults as $ckey => $cdata) + { + $sval = mgGetSetting($ckey); - foreach ($mgDefaults as $key => $dval) - { - $sval = mgGetSetting($key); if ($cmd == "dump") { - printf("%-20s = %s\n", - $key, - mgGetDValStr($dval, $sval)); + echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s\n", FALSE); } else { - printf("%-20s = %s%s\n", - $key, - mgGetDValStr($dval, $sval), - ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : ""); + echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s", TRUE). + (($cdata[1] !== NULL && $sval !== $cdata[1]) ? + " (default: ".mgGetDValStr($ckey, $cdata, $cdata[1], "%2\$s", FALSE).")" : "")."\n"; } } break;