@@ -1043,99 +1043,139 @@ magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
10431043cyan_underlined () { print_in_color " \e[4;36m" " $* " ; }
10441044
10451045# src/lib/config.sh
1046- config_init () {
1047- CONFIG_FILE=${CONFIG_FILE:= config.ini}
1048- [[ -f " $CONFIG_FILE " ]] || touch " $CONFIG_FILE "
1046+
1047+ config_show () {
1048+ config_load
1049+ ini_show
10491050}
10501051
10511052config_get () {
1052- local key=$1
1053- local regex=" ^$key *= *(.+)$"
1054- local value=" "
1055-
1056- config_init
1053+ local key=" $1 "
1054+ local default_value=" $2 "
10571055
1058- while IFS= read -r line || [ -n " $line " ]; do
1059- if [[ $line =~ $regex ]]; then
1060- value=" ${BASH_REMATCH[1]} "
1061- break
1062- fi
1063- done < " $CONFIG_FILE "
1064-
1065- echo " $value "
1056+ config_load
1057+ echo " ${ini["$key"]:- $default_value } "
10661058}
10671059
10681060config_set () {
1069- local key=$1
1061+ local key=" $1 "
10701062 shift
10711063 local value=" $* "
10721064
1073- config_init
1074-
1075- local regex=" ^($key ) *= *.+$"
1076- local output=" "
1077- local found_key=" "
1078- local newline
1079-
1080- while IFS= read -r line || [ -n " $line " ]; do
1081- newline=$line
1082- if [[ $line =~ $regex ]]; then
1083- found_key=" ${BASH_REMATCH[1]} "
1084- newline=" $key = $value "
1085- output=" $output$newline \n"
1086- elif [[ $line ]]; then
1087- output=" $output$line \n"
1088- fi
1089- done < " $CONFIG_FILE "
1065+ config_load
1066+ ini[" $key " ]=" $value "
1067+ config_save
1068+ }
10901069
1091- if [[ -z $found_key ]]; then
1092- output=" $output$key = $value \n"
1093- fi
1070+ config_del () {
1071+ local key=" $1 "
10941072
1095- printf " %b\n" " $output " > " $CONFIG_FILE "
1073+ config_load
1074+ unset " ini[$key ]"
1075+ config_save
10961076}
10971077
1098- config_del () {
1099- local key=$1
1078+ config_keys () {
1079+ config_load
1080+ ini_keys
1081+ }
11001082
1101- local regex=" ^($key ) *="
1102- local output=" "
1083+ config_has_key () {
1084+ [[ $( config_get " $1 " ) ]]
1085+ }
11031086
1104- config_init
1087+ config_reload () {
1088+ declare -g config_loaded=false
1089+ config_load
1090+ }
11051091
1106- while IFS= read -r line || [ -n " $line " ]; do
1107- if [[ $line ]] && [[ ! $line =~ $regex ]]; then
1108- output=" $output$line \n"
1109- fi
1110- done < " $CONFIG_FILE "
1092+ config_load () {
1093+ [[ " $config_loaded " == " true" ]] && return
11111094
1112- printf " %b\n" " $output " > " $CONFIG_FILE "
1095+ declare -g CONFIG_FILE=${CONFIG_FILE:= config.ini}
1096+ declare -g config_loaded=true
1097+ [[ -f " $CONFIG_FILE " ]] || touch " $CONFIG_FILE "
1098+ ini_load " $CONFIG_FILE "
11131099}
11141100
1115- config_show () {
1116- config_init
1117- cat " $CONFIG_FILE "
1101+ config_save () {
1102+ ini_save " $CONFIG_FILE "
11181103}
11191104
1120- config_keys () {
1121- local regex=" ^([a-zA-Z0-9_\-\/\.]+) *="
1105+ # src/lib/ini.sh
11221106
1123- config_init
1107+ ini_load () {
1108+ declare -gA ini
11241109
1125- local keys=()
1126- local key
1110+ local ini_file=" $1 "
11271111
1128- while IFS= read -r line || [ -n " $line " ]; do
1129- if [[ $line =~ $regex ]]; then
1112+ local section=" "
1113+ local key=" "
1114+ local value=" "
1115+ local section_regex=" ^\[(.+)\]"
1116+ local key_regex=" ^([^ =]+) *= *(.*) *$"
1117+ local comment_regex=" ^;"
1118+
1119+ while IFS= read -r line; do
1120+ if [[ $line =~ $comment_regex ]]; then
1121+ continue
1122+ elif [[ $line =~ $section_regex ]]; then
1123+ section=" ${BASH_REMATCH[1]} ."
1124+ elif [[ $line =~ $key_regex ]]; then
11301125 key=" ${BASH_REMATCH[1]} "
1131- keys+=(" $key " )
1126+ value=" ${BASH_REMATCH[2]} "
1127+ ini[" ${section}${key} " ]=" $value "
11321128 fi
1133- done < " $CONFIG_FILE "
1134- echo " ${keys[@]} "
1129+ done < " $ini_file "
11351130}
11361131
1137- config_has_key () {
1138- [[ $( config_get " $1 " ) ]]
1132+ ini_save () {
1133+ declare -gA ini
1134+
1135+ local ini_file=" $1 "
1136+
1137+ local current_section=" "
1138+ local has_free_keys=false
1139+
1140+ rm -f " $ini_file "
1141+
1142+ for key in $( ini_keys) ; do
1143+ [[ $key == * .* ]] && continue
1144+ has_free_keys=true
1145+ value=" ${ini[$key]} "
1146+ echo " $key = $value " >> " $ini_file "
1147+ done
1148+
1149+ [[ " ${has_free_keys} " == " true" ]] && echo >> " $ini_file "
1150+
1151+ for key in $( ini_keys) ; do
1152+ [[ $key == * .* ]] || continue
1153+ value=" ${ini[$key]} "
1154+ IFS=" ." read -r section_name key_name <<< " $key"
1155+
1156+ if [[ " $current_section " != " $section_name " ]]; then
1157+ [[ $current_section ]] && echo >> " $ini_file "
1158+ echo " [$section_name ]" >> " $ini_file "
1159+ current_section=" $section_name "
1160+ fi
1161+
1162+ echo " $key_name = $value " >> " $ini_file "
1163+ done
1164+ }
1165+
1166+ ini_show () {
1167+ declare -gA ini
1168+
1169+ for key in $( ini_keys) ; do
1170+ echo " $key = ${ini[$key]} "
1171+ done
1172+ }
1173+
1174+ ini_keys () {
1175+ declare -gA ini
1176+
1177+ local keys=(" ${! ini[@]} " )
1178+ for a in " ${keys[@]} " ; do echo " $a " ; done | sort
11391179}
11401180
11411181# src/lib/is_busybox_grep.sh
0 commit comments