Differences

This shows you the differences between two versions of the page.

Link to this comparison view

declare [2014/09/04 22:06]
127.0.0.1 external edit
declare [2017/08/04 13:52] (current)
rzr [KO]
Line 138: Line 138:
  
   * http://ss64.com/bash/declare.html   * http://ss64.com/bash/declare.html
 +
 +==== ARRAYS ====
 +
 +
 +<code bash>
 +initialize_struct_framework()
 +{
 +  g_struct_container=()
 +
 +  generic_struct_builder()
 +  {
 +    local p_struct_name="$1"
 +    local p_builder_name="$2"
 +    declare -A "${p_struct_name}" || error_handler || return
 +    eval "${p_struct_name}"'[builder]="${p_builder_name}"' || error_handler || return
 +    shift 2 || error_handler || return
 +    while [ "0" -lt "$#" ]
 +    do
 +      local p_key_name="$1"
 +      local p_key_value="$2"
 +      shift 2 || error_handler || return
 +      eval "${p_struct_name}"'["${p_key_name}"]="${p_key_value}"' || error_handler || return
 +    done
 +    g_struct_container+=("$(declare -p "${p_struct_name}")") || error_handler || return
 +  }
 +
 +  filesystem_item_struct_builder()
 +  {
 +    local p_struct_name="$1"
 +    local name_field_key="name"
 +    local p_name_field_value="$2"
 +    local child_set_field_key="child_set"
 +    local p_child_set_field_value="$3"
 +    local type_field_key="type"
 +    local p_type_field_value="$4"
 +    generic_struct_builder                                  \
 +      "${p_struct_name}"                                    \
 +      "${FUNCNAME}"                                         \
 +      "${name_field_key}"      "${p_name_field_value}"      \
 +      "${child_set_field_key}" "${p_child_set_field_value}" \
 +      "${type_field_key}"      "${p_type_field_value}" || error_handler || return
 +  }
 +}
 +
 +add_item()
 +{
 +  local p_item="$1"
 +  filesystem_item_struct_builder "struct" "${p_item}" "" ""
 +  local struct_index="$((${#g_struct_container[@]}-1))"
 +  struct[child_set]+="${struct_index} "
 +  eval "${g_struct_container[struct_index]}"
 +  if [ -d "${p_item}" ]
 +  then
 +    struct[type]="directory"
 +    cd "${p_item}"
 +    local item
 +    for item in *
 +    do
 +      add_item "${item}"
 +    done
 +    cd ..
 +  else
 +    struct[type]="file"
 +  fi
 +  g_struct_container[struct_index]="$(declare -p struct)"
 +}
 +
 +display_rec()
 +{
 +  local p_path="$1"
 +  [ "${p_path}" != "" ] && p_path+="/"
 +  echo "${p_path}${struct[name]}"
 +  p_path+="${struct[name]}"
 +  local struct_index
 +  for struct_index in ${struct[child_set]}
 +  do
 +    eval "${g_struct_container[struct_index]}"
 +    display_rec "${p_path}"
 +  done
 +}
 +
 +display()
 +{
 +  eval "${g_struct_container}"
 +  display_rec ""
 +}
 +
 +initialize_struct_framework
 +add_item "."
 +display
 +declare -p g_struct_container
 +</code>
  
 ===== MORE ===== ===== MORE =====
  
 @TaG: BasH BuG @TaG: BasH BuG
declare.txt ยท Last modified: 2017/08/04 13:52 by rzr
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki