Browse code

Fix iniset and his friends

* In python the white spaces are part of the section name
* Handle options with empty value
* Support paths with white spaces

Change-Id: I69a584608853cfdb8b7dce1e24d929216ef2fc41

Attila Fazekas authored on 2012/12/20 18:57:16
Showing 2 changed files
... ...
@@ -460,7 +460,7 @@ function inicomment() {
460 460
     local file=$1
461 461
     local section=$2
462 462
     local option=$3
463
-    sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
463
+    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
464 464
 }
465 465
 
466 466
 # Uncomment an option in an INI file
... ...
@@ -469,7 +469,7 @@ function iniuncomment() {
469 469
     local file=$1
470 470
     local section=$2
471 471
     local option=$3
472
-    sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
472
+    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
473 473
 }
474 474
 
475 475
 
... ...
@@ -480,10 +480,20 @@ function iniget() {
480 480
     local section=$2
481 481
     local option=$3
482 482
     local line
483
-    line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
483
+    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
484 484
     echo ${line#*=}
485 485
 }
486 486
 
487
+# Determinate is the given option present in the INI file
488
+# ini_has_option config-file section option
489
+function ini_has_option() {
490
+    local file=$1
491
+    local section=$2
492
+    local option=$3
493
+    local line
494
+    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
495
+    [ -n "$line" ]
496
+}
487 497
 
488 498
 # Set an option in an INI file
489 499
 # iniset config-file section option value
... ...
@@ -492,18 +502,18 @@ function iniset() {
492 492
     local section=$2
493 493
     local option=$3
494 494
     local value=$4
495
-    if ! grep -q "^\[ *$section *\]" $file; then
495
+    if ! grep -q "^\[$section\]" "$file"; then
496 496
         # Add section at the end
497
-        echo -e "\n[$section]" >>$file
497
+        echo -e "\n[$section]" >>"$file"
498 498
     fi
499
-    if [[ -z "$(iniget $file $section $option)" ]]; then
499
+    if ! ini_has_option "$file" "$section" "$option"; then
500 500
         # Add it
501
-        sed -i -e "/^\[ *$section *\]/ a\\
501
+        sed -i -e "/^\[$section\]/ a\\
502 502
 $option = $value
503
-" $file
503
+" "$file"
504 504
     else
505 505
         # Replace it
506
-        sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
506
+        sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
507 507
     fi
508 508
 }
509 509
 
... ...
@@ -57,6 +57,9 @@ handlers=ee,ff
57 57
 
58 58
 [ ccc ]
59 59
 spaces  =  yes
60
+
61
+[ddd]
62
+empty =
60 63
 EOF
61 64
 
62 65
 # Test with spaces
... ...
@@ -79,13 +82,22 @@ fi
79 79
 
80 80
 # Test with spaces in section header
81 81
 
82
-VAL=$(iniget test.ini ccc spaces)
82
+VAL=$(iniget test.ini " ccc " spaces)
83 83
 if [[ "$VAL" == "yes" ]]; then
84 84
     echo "OK: $VAL"
85 85
 else
86 86
     echo "iniget failed: $VAL"
87 87
 fi
88 88
 
89
+iniset test.ini "b b" opt_ion 42
90
+
91
+VAL=$(iniget test.ini "b b" opt_ion)
92
+if [[ "$VAL" == "42" ]]; then
93
+    echo "OK: $VAL"
94
+else
95
+    echo "iniget failed: $VAL"
96
+fi
97
+
89 98
 # Test without spaces, end of file
90 99
 
91 100
 VAL=$(iniget test.ini bbb handlers)
... ...
@@ -104,6 +116,29 @@ else
104 104
     echo "iniget failed: $VAL"
105 105
 fi
106 106
 
107
+# test empty option
108
+if ini_has_option test.ini ddd empty; then
109
+   echo "OK: ddd.empty present"
110
+else
111
+   echo "ini_has_option failed: ddd.empty not found"
112
+fi
113
+
114
+# test non-empty option
115
+if ini_has_option test.ini bbb handlers; then
116
+   echo "OK: bbb.handlers present"
117
+else
118
+   echo "ini_has_option failed: bbb.handlers not found"
119
+fi
120
+
121
+# test changing empty option
122
+iniset test.ini ddd empty "42"
123
+
124
+VAL=$(iniget test.ini ddd empty)
125
+if [[ "$VAL" == "42" ]]; then
126
+    echo "OK: $VAL"
127
+else
128
+    echo "iniget failed: $VAL"
129
+fi
107 130
 
108 131
 # Test section not exist
109 132
 
... ...
@@ -132,6 +167,12 @@ else
132 132
     echo "iniget failed: $VAL"
133 133
 fi
134 134
 
135
+if ! ini_has_option test.ini aaa debug; then
136
+    echo "OK aaa.debug not present"
137
+else
138
+    echo "ini_has_option failed: aaa.debug"
139
+fi
140
+
135 141
 iniset test.ini aaa debug "999"
136 142
 
137 143
 VAL=$(iniget test.ini aaa debug)