Browse Source

Revamp reading mechanism; use strategy

Now by default iniread will stop at first file; old behavior can be
restored by -j|--join switch.
Alois Mahdal 9 years ago
parent
commit
9b60fa8566
1 changed files with 57 additions and 12 deletions
  1. 57
    12
      include/ini.sh

+ 57
- 12
include/ini.sh View File

@@ -70,30 +70,58 @@ __iniread__fltsct() {
70 70
 
71 71
 
72 72
 __iniread__merge() {
73
+    local path
74
+    debug -v strategy
75
+    while read path;
76
+    do
77
+        debug -v path
78
+        case $strategy in
79
+            first)
80
+                local winner
81
+                if test -f "$path";
82
+                then
83
+                    debug "winner: $path"
84
+                    cat "$path"
85
+                    cat >/dev/null  # throw away rest of paths
86
+                fi
87
+                ;;
88
+            join)
89
+                debug -v path
90
+                echo "# file: ${path/$HOME/~}"
91
+                cat "$path" 2>/dev/null
92
+                ;;
93
+        esac
94
+    done
95
+}
96
+
97
+
98
+__iniread__load() {
73 99
     if test -z "$1";
74 100
     then                # guess filename from section head
75 101
         local guess="$(cut -d. -f1 <<<"$wntsct")$FFOO_INI_SUFFIX"
76 102
         debug -v guess
77
-        __iniread__merge "$guess"
103
+        __iniread__load "$guess"
78 104
     fi
79 105
     local arg trydir trypath
80 106
     for arg in "$@";
81 107
     do
82 108
         case $arg in
83 109
             -|*/*)      # stdin, or path (with slash)
84
-                cat $arg
110
+                debug -v arg
111
+                cat "$arg"
85 112
             ;;
86 113
         *)              # name given, find all its incarnations
87 114
             debug -v FFOO_INI_PATH
88
-            echo "$FFOO_INI_PATH" \
115
+            echos "$FFOO_INI_PATH" \
89 116
               | tr ':' '\n' \
90 117
               | while read trydir;
91 118
                 do
92
-                    test -z "$trydir" && continue
119
+                    test -n "$trydir" || continue
93 120
                     trypath="$trydir/$arg"
94 121
                     debug -v trypath
95
-                    cat $trypath 2>/dev/null
96
-                done
122
+                    echos "$trypath"
123
+                done \
124
+              |  __iniread__merge
97 125
             ;;
98 126
         esac
99 127
     done
@@ -106,8 +134,8 @@ iniread() {
106 134
     # A flexible (not just) .ini file reader
107 135
     #
108 136
     # Usage:
109
-    #   iniread [-S] [-1] [-s section] [-k key] [file...]
110
-    #   iniread [-S] [-1] [-p path] [file...]
137
+    #   iniread [-S] [-1] [-j] [-s section] [-k key] [file...]
138
+    #   iniread [-S] [-1] [-j] [-p path] [file...]
111 139
     #
112 140
     #
113 141
     # Selection
@@ -155,9 +183,19 @@ iniread() {
155 183
     #     yield more than one path, which is equivalent as if
156 184
     #     all paths were provided.)
157 185
     #
158
-    # Any files are simply concatenated. (This means that if a
159
-    # section is queried that is present in both files, it is
160
-    # effectively concatenated as well.
186
+    #     Not all files expanded based on `$FFOO_INI_PATH`
187
+    #     are read by default; reading is governed by "read
188
+    #     strategy": the default strategy "first" reads only
189
+    #     the first existing file.  "join" strategy means
190
+    #     that any files are simply concatenated and prefixed
191
+    #     with comment containing path to the file, which can
192
+    #     be observed in strict mode.  (This means that if a
193
+    #     section is queried that is present in both files,
194
+    #     it is effectively concatenated as well.
195
+    #
196
+    #     The read strategy can be specified using parameter
197
+    #     `--read-strategy`, while `-j` or `--join` is a
198
+    #     shorthand for `--read-strategy join`
161 199
     #
162 200
     # Without *file* given at all, same procedure is used as in
163 201
     # case of filename without slash, except that *file* argument
@@ -173,16 +211,23 @@ iniread() {
173 211
     local sct_ok=true
174 212
     local one_line=false
175 213
     local grepex="."  # i.e. throw away empty lines
214
+    local strategy="first"
176 215
     while true; do case $1 in
177 216
         --)                                                       break   ;;
178 217
         -1|--one-line) one_line=true;                             shift 1 ;;
218
+        -j|--join)     strategy="join";                           shift 1 ;;
179 219
         -k|--key)      wntkey="$2";                               shift 2 ;;
180 220
         -p|--path)     wntkey="${2##*.}"; wntsct="${2%.$wntkey}"; shift 2 ;;
181 221
         -s|--section)  wntsct="$2"; sct_ok=false;                 shift 2 ;;
182 222
         -S|--strict)   strict=true;                               shift 1 ;;
183 223
         -l|-L)         warn "--list-* not implemented yet";       shift 1 ;;
224
+        --read-strategy) strategy="$2";                           shift 2 ;;
184 225
         *)                                                        break   ;;
185 226
     esac done
227
+    case "$strategy" in
228
+        first|join)                                   : ;;
229
+        *) warn "invalid read strategy: $strategy"; return 2 ;;
230
+    esac
186 231
     fltsct=__iniread__cat
187 232
     fltkey=__iniread__cat
188 233
     fltcmt=__initead_fltcmt
@@ -194,7 +239,7 @@ iniread() {
194 239
     $strict && fltcmt=__iniread__cat
195 240
     debug -v wntkey wntsct
196 241
     debug "\$@='$@'"
197
-    __iniread__merge "$@" \
242
+    __iniread__load "$@" \
198 243
         | $fltcmt \
199 244
         | $fltsct \
200 245
         | $fltkey \