Browse code

Merge "Fix ini functions to handle spaces in section names"

Jenkins authored on 2012/11/29 01:54:17
Showing 2 changed files
... ...
@@ -415,7 +415,7 @@ function inicomment() {
415 415
     local file=$1
416 416
     local section=$2
417 417
     local option=$3
418
-    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
418
+    sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
419 419
 }
420 420
 
421 421
 # Uncomment an option in an INI file
... ...
@@ -424,7 +424,7 @@ function iniuncomment() {
424 424
     local file=$1
425 425
     local section=$2
426 426
     local option=$3
427
-    sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
427
+    sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
428 428
 }
429 429
 
430 430
 
... ...
@@ -435,7 +435,7 @@ function iniget() {
435 435
     local section=$2
436 436
     local option=$3
437 437
     local line
438
-    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
438
+    line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
439 439
     echo ${line#*=}
440 440
 }
441 441
 
... ...
@@ -447,18 +447,18 @@ function iniset() {
447 447
     local section=$2
448 448
     local option=$3
449 449
     local value=$4
450
-    if ! grep -q "^\[$section\]" $file; then
450
+    if ! grep -q "^\[ *$section *\]" $file; then
451 451
         # Add section at the end
452 452
         echo -e "\n[$section]" >>$file
453 453
     fi
454 454
     if [[ -z "$(iniget $file $section $option)" ]]; then
455 455
         # Add it
456
-        sed -i -e "/^\[$section\]/ a\\
456
+        sed -ie "/^\[ *$section *\]/ a\\
457 457
 $option = $value
458 458
 " $file
459 459
     else
460 460
         # Replace it
461
-        sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
461
+        sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
462 462
     fi
463 463
 }
464 464
 
... ...
@@ -54,6 +54,9 @@ handlers = aa, bb
54 54
 
55 55
 [bbb]
56 56
 handlers=ee,ff
57
+
58
+[ ccc ]
59
+spaces  =  yes
57 60
 EOF
58 61
 
59 62
 # Test with spaces
... ...
@@ -74,6 +77,14 @@ else
74 74
     echo "iniget failed: $VAL"
75 75
 fi
76 76
 
77
+# Test with spaces in section header
78
+
79
+VAL=$(iniget test.ini ccc spaces)
80
+if [[ "$VAL" == "yes" ]]; then
81
+    echo "OK: $VAL"
82
+else
83
+    echo "iniget failed: $VAL"
84
+fi
77 85
 
78 86
 # Test without spaces, end of file
79 87
 
... ...
@@ -112,7 +123,6 @@ else
112 112
     echo "iniget failed: $VAL"
113 113
 fi
114 114
 
115
-
116 115
 # Test option not exist
117 116
 
118 117
 VAL=$(iniget test.ini aaa debug)