protothreads.c

Git mirror of http://dunkels.com/adam/pt/index.html
git clone git://git.finwo.net/lib/protothreads.c
Log | Files | Refs

ini.sh (2164B)


      1 #!/usr/bin/env bash
      2 
      3 # Copied from https://github.com/finwo/dep/blob/main/src/util/ini.sh
      4 
      5 # Required for the whitespace trimming
      6 shopt -s extglob
      7 
      8 # Arguments:
      9 #   $0 <fn_keyHandler> <str_filename> [section[.key]]
     10 function ini_foreach {
     11 
     12   # No file = no data
     13   inifile="${2}"
     14   if [[ ! -f "$inifile" ]]; then
     15     exit 1
     16   fi
     17 
     18   # Process the file line-by-line
     19   SECTION=
     20   while read line; do
     21 
     22     # Remove surrounding whitespace
     23     line=${line##*( )} # From the beginning
     24     line=${line%%*( )} # From the end
     25 
     26     # Remove comments and empty lines
     27     if [[ "${line:0:1}" == '#' ]] || [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then
     28       continue
     29     fi
     30 
     31     # Handle section markers
     32     if [[ "${line:0:1}" == "[" ]]; then
     33       SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/')
     34       SECTION=${SECTION##*( )}
     35       SECTION=${SECTION%%*( )}
     36       SECTION="${SECTION}."
     37       continue
     38     fi
     39 
     40     # Output found variable
     41     NAME=${line%%=*}
     42     NAME=${NAME%%*( )}
     43     VALUE=${line#*=}
     44     VALUE=${VALUE##*( )}
     45 
     46     # Output searched or all
     47     if [[ -z "${3}" ]]; then
     48       $1 "$SECTION" "$NAME" "${VALUE}"
     49     elif [[ "${SECTION}" == "${3}" ]] || [[ "${SECTION}${NAME}" == "${3}" ]]; then
     50       $1 "$SECTION" "$NAME" "${VALUE}"
     51     fi
     52 
     53   done < "${inifile}"
     54 }
     55 
     56 function ini_write {
     57   PREVIOUSSECTION=
     58   echo -en "" > "$1"
     59   while read line; do
     60     KEYFULL=${line%%=*}
     61     VALUE=${line#*=}
     62     SECTION=${KEYFULL%%.*}
     63     KEY=${KEYFULL#*.}
     64     if [[ "${SECTION}" != "${PREVIOUSSECTION}" ]]; then
     65       echo "[${SECTION}]" >> "$1"
     66       PREVIOUSSECTION="${SECTION}"
     67     fi
     68     echo "${KEY}=${VALUE}" >> "$1"
     69   done < <(sort --unique)
     70 }
     71 
     72 function ini_output_full {
     73   echo "$1$2=$3"
     74 }
     75 function ini_output_section {
     76   echo "$2=$3"
     77 }
     78 function ini_output_value {
     79   echo "$3"
     80 }
     81 
     82 # Allow this file to be called stand-alone
     83 # ini.sh <filename> [section[.key]] [sectionmode]
     84 if [ $(basename $0) == "ini.sh" ]; then
     85   fullMode=full
     86   sectionMode=value
     87   if [[ ! -z "${3}" ]]; then
     88     fullMode=${3}
     89     sectionMode=${3}
     90   fi
     91   if [[ -z "${2}" ]]; then
     92     ini_foreach ini_output_${fullMode} "$@"
     93   else
     94     ini_foreach ini_output_${sectionMode} "$@"
     95   fi
     96 fi