| 1 |
# Hash: |
| 2 |
# Hash function library |
| 3 |
# Author: Mariusz Gniazdowski <mariusz.gn-at-gmail.com> |
| 4 |
# Date: 2005-04-07 |
| 5 |
|
| 6 |
# Functions making emulating hashes in Bash a little less painful. |
| 7 |
|
| 8 |
|
| 9 |
# Limitations: |
| 10 |
# * Only global variables are supported. |
| 11 |
# * Each hash instance generates one global variable per value. |
| 12 |
# * Variable names collisions are possible |
| 13 |
#+ if you define variable like __hash__hashname_key |
| 14 |
# * Keys must use chars that can be part of a Bash variable name |
| 15 |
#+ (no dashes, periods, etc.). |
| 16 |
# * The hash is created as a variable: |
| 17 |
# ... hashname_keyname |
| 18 |
# So if somone will create hashes like: |
| 19 |
# myhash_ + mykey = myhash__mykey |
| 20 |
# myhash + _mykey = myhash__mykey |
| 21 |
# Then there will be a collision. |
| 22 |
# (This should not pose a major problem.) |
| 23 |
|
| 24 |
|
| 25 |
Hash_config_varname_prefix=__hash__ |
| 26 |
|
| 27 |
|
| 28 |
# Emulates: hash[key]=value |
| 29 |
# |
| 30 |
# Params: |
| 31 |
# 1 - hash |
| 32 |
# 2 - key |
| 33 |
# 3 - value |
| 34 |
function hash_set { |
| 35 |
eval "${Hash_config_varname_prefix}${1}_${2}=\"${3}\"" |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
# Emulates: value=hash[key] |
| 40 |
# |
| 41 |
# Params: |
| 42 |
# 1 - hash |
| 43 |
# 2 - key |
| 44 |
# 3 - value (name of global variable to set) |
| 45 |
function hash_get_into { |
| 46 |
eval "$3=\"\$${Hash_config_varname_prefix}${1}_${2}\"" |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
# Emulates: echo hash[key] |
| 51 |
# |
| 52 |
# Params: |
| 53 |
# 1 - hash |
| 54 |
# 2 - key |
| 55 |
# 3 - echo params (like -n, for example) |
| 56 |
function hash_echo { |
| 57 |
eval "echo $3 \"\$${Hash_config_varname_prefix}${1}_${2}\"" |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
# Emulates: hash1[key1]=hash2[key2] |
| 62 |
# |
| 63 |
# Params: |
| 64 |
# 1 - hash1 |
| 65 |
# 2 - key1 |
| 66 |
# 3 - hash2 |
| 67 |
# 4 - key2 |
| 68 |
function hash_copy { |
| 69 |
eval "${Hash_config_varname_prefix}${1}_${2}\ |
| 70 |
=\"\$${Hash_config_varname_prefix}${3}_${4}\"" |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
# Emulates: hash[keyN-1]=hash[key2]=...hash[key1] |
| 75 |
# |
| 76 |
# Copies first key to rest of keys. |
| 77 |
# |
| 78 |
# Params: |
| 79 |
# 1 - hash1 |
| 80 |
# 2 - key1 |
| 81 |
# 3 - key2 |
| 82 |
# . . . |
| 83 |
# N - keyN |
| 84 |
function hash_dup { |
| 85 |
local hashName="$1" keyName="$2" |
| 86 |
shift 2 |
| 87 |
until [ ${#} -le 0 ]; do |
| 88 |
eval "${Hash_config_varname_prefix}${hashName}_${1}\ |
| 89 |
=\"\$${Hash_config_varname_prefix}${hashName}_${keyName}\"" |
| 90 |
shift; |
| 91 |
done; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
# Emulates: unset hash[key] |
| 96 |
# |
| 97 |
# Params: |
| 98 |
# 1 - hash |
| 99 |
# 2 - key |
| 100 |
function hash_unset { |
| 101 |
eval "unset ${Hash_config_varname_prefix}${1}_${2}" |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
# Emulates something similar to: ref=&hash[key] |
| 106 |
# |
| 107 |
# The reference is name of the variable in which value is held. |
| 108 |
# |
| 109 |
# Params: |
| 110 |
# 1 - hash |
| 111 |
# 2 - key |
| 112 |
# 3 - ref - Name of global variable to set. |
| 113 |
function hash_get_ref_into { |
| 114 |
eval "$3=\"${Hash_config_varname_prefix}${1}_${2}\"" |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
# Emulates something similar to: echo &hash[key] |
| 119 |
# |
| 120 |
# That reference is name of variable in which value is held. |
| 121 |
# |
| 122 |
# Params: |
| 123 |
# 1 - hash |
| 124 |
# 2 - key |
| 125 |
# 3 - echo params (like -n for example) |
| 126 |
function hash_echo_ref { |
| 127 |
eval "echo $3 \"${Hash_config_varname_prefix}${1}_${2}\"" |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
# Emulates something similar to: $$hash[key](param1, param2, ...) |
| 133 |
# |
| 134 |
# Params: |
| 135 |
# 1 - hash |
| 136 |
# 2 - key |
| 137 |
# 3,4, ... - Function parameters |
| 138 |
function hash_call { |
| 139 |
local hash key |
| 140 |
hash=$1 |
| 141 |
key=$2 |
| 142 |
shift 2 |
| 143 |
eval "eval \"\$${Hash_config_varname_prefix}${hash}_${key} \\\"\\\$@\\\"\"" |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
# Emulates something similar to: isset(hash[key]) or hash[key]==NULL |
| 148 |
# |
| 149 |
# Params: |
| 150 |
# 1 - hash |
| 151 |
# 2 - key |
| 152 |
# Returns: |
| 153 |
# 0 - there is such key |
| 154 |
# 1 - there is no such key |
| 155 |
function hash_is_set { |
| 156 |
eval "if [[ \"\${${Hash_config_varname_prefix}${1}_${2}-a}\" = \"a\" && |
| 157 |
\"\${${Hash_config_varname_prefix}${1}_${2}-b}\" = \"b\" ]] |
| 158 |
then return 1; else return 0; fi" |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
# Emulates something similar to: |
| 163 |
# foreach($hash as $key => $value) { fun($key,$value); } |
| 164 |
# |
| 165 |
# It is possible to write different variations of this function. |
| 166 |
# Here we use a function call to make it as "generic" as possible. |
| 167 |
# |
| 168 |
# Params: |
| 169 |
# 1 - hash |
| 170 |
# 2 - function name |
| 171 |
function hash_foreach { |
| 172 |
local keyname oldIFS="$IFS" |
| 173 |
IFS=' ' |
| 174 |
for i in $(eval "echo \${!${Hash_config_varname_prefix}${1}_*}"); do |
| 175 |
keyname=$(eval "echo \${i##${Hash_config_varname_prefix}${1}_}") |
| 176 |
eval "$2 $keyname \"\$$i\"" |
| 177 |
done |
| 178 |
IFS="$oldIFS" |
| 179 |
} |
| 180 |
|
| 181 |
# NOTE: In lines 103 and 116, ampersand changed. |
| 182 |
# But, it doesn't matter, because these are comment lines anyhow. |