#!/bin/bash
###############################################################################
# #
# F01 common.lib : A library for common functions. #
# _common4bash.lib : Is an extension for Bash function that cause problem #
# under KSH. #
# #
# +---------------------------------------------------------------------+ #
# | Copyright (c) 2010-2012 Flyounet | #
# +---------------------------------------------------------------------+ #
# | This program is free software: you can redistribute it and/or | #
# | modify it under the terms of the GNU General Public License as | #
# | published by the Free Software Foundation, either version 3 of the | #
# | License, or (at your option) any later version. | #
# | | #
# | This program is distributed in the hope that it will be useful, | #
# | but WITHOUT ANY WARRANTY; without even the implied warranty of | #
# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | #
# | GNU General Public License for more details. | #
# | | #
# | You should have received a copy of the GNU General Public License | #
# | along with this program. | #
# | If not, see . | #
# +---------------------------------------------------------------------+ #
# | Cette œuvre est distribuée SANS AUCUNE GARANTIE hormis celle d'être | #
# | distribuée sous les termes de la License Demerdez-vous ("Demerden | #
# | Sie Sich License") plusommunément appelée DSSL telle que publiée | #
# | par Flyounet : soit la version 1 de cette licence, soit (à votre | #
# | gré) toute version ultérieure. | #
# | | #
# | Vous devriez avoir reçu une copie de la Licence Démerdez-vous avec | #
# | cette œuvre ; si ce n’est pas le cas, consultez : | #
# | . | #
# +---------------------------------------------------------------------+ #
# | Author: Flyounet < dev @@ flyounet . net > | #
# +---------------------------------------------------------------------+ #
# #
###############################################################################
# #
# v0.01 [14/04/2010] Flyounet [@Home] : #
# > Initiale Release (from common.lib) #
# > Version is linked to the common.lib #
# v0.02 [??/??/2011] Flyounet [@Home] : #
# #
# #
###############################################################################
# #
# Todo : #
# #
# #
###############################################################################
# #
# Legende : #
# + --> Indique une nouveaute, un ajout de fonctionnalite. #
# * --> Indique une correction de bogue. #
# - --> Indique la suppression d'une fonctionnalite/variable. #
# > --> Indique une information n'ayant pas forcement de rapport avec le code#
# < --> Indique une amelioration a apporter au code. #
# <- --> Indique une amelioration en cours de developpement/realisation. #
# OK --> Indique qu'une amelioration a ete effectuee. #
# #
###############################################################################
# ############################################################################ #
# Variables Initialisation
# ############################################################################ #
# Indicate that the file is loaded
export __BASH_EXTENSION='loaded'
# ############################################################################ #
# Functions
# ############################################################################ #
#F01.23-1.03
#Function : _textBox4Bash, Create a box around the text
#Syntaxe : _textBox4Bash <(left|center|right)>
#Return : 1=OK
#Notes : export the text variable in __textBox
# : All catacters \t, \n, \r and space are removed.
_textBox4Bash() {
myDebug ${__MYDEBUG} "_textBox4Bash(${@})"
_max=0
if [ ${#} -eq 3 ]; then _align="${3}"; else _align="left"; fi
eval $(
p2s "${1}" | while read _string; do
if [ ${#_string} -gt ${_max} ]; then export _max=${#_string}; fi
p2s "_max=${_max};"
done
)
let _maxRow=${_max}+4
_padding="${2}"
__textBox=''
while read _string; do
_subString=""
case "${_align,,}" in
center)
let "_padLeft=(${_max}-${#_string})/2"
let _padRight=${_max}-${#_string}-${_padLeft}
textFill ' ' ${_padLeft}
_subString="${_padding} ${__textFill}${_string}"
textFill ' ' ${_padRight}
_subString="${_subString}${__textFill} ${_padding}"
;;
left|right|*)
let _pad=${_max}-${#_string}
textFill ' ' ${_pad}
if [ "${_align,,}" = "right" ]; then
_subString="${_padding} ${__textFill}${_string} ${_padding}"
else
_subString="${_padding} ${_string}${__textFill} ${_padding}"
fi
;;
esac
__textBox="${__textBox}${_subString}\n"
done < <( p2s "${1}" )
textFill "${2}" ${_maxRow}
__textBox="${__textFill}\n${__textBox}${__textFill}"
export __textBox
return 1
}
#F01.18-1.01
#Function : _ltrimBash, Remove all blank from begining
#Syntaxe : ltrim
#Return : N/1
#Notes : Get data from Variable Name given as Arguement and re-set this variable
# : All catacters \t, \n, \r and space are removed.
# : VariableName ($1) MUST NOT be empty. No check.
_ltrimBash () {
myDebug ${__MYDEBUG} "_ltrimBash(${@})"
# if [ -z "${1:-}" ]; then
# myError "_ltrim: Variable name empty"
# myDebug 6 "_ltrim: Variable name empty"
# elif [ ! -z "${!1}" ]; then
if [ ! -z "${!1}" ]; then
_local="${!1}"
while [ "x${_local:0:1}" = "x " -o "x${_local:0:1}" = "x " -o "x${_local:0:1}" = "x\n" -o "x${_local:0:1}" = "x\r" -o "x${_local:0:1}" = 'x
' ]; do
_local="${_local:1}"
done
eval export "${1}"='${_local}'
unset _local
fi
}
#F01.19-1.01
#Function : _rtrimBash, Remove all blank from end
#Syntaxe : _rtrimBash
#Return : N/A
#Notes : Get data from Variable Name given as Arguement and re-set this variable
# : All catacters \t, \n, \r and space are removed.
_rtrimBash () {
myDebug ${__MYDEBUG} "_rtrimBash(${@})"
if [ ! -z "${!1}" ]; then
_local="${!1}"
while [ "x${_local: -1}" = "x " -o "x${_local: -1}" = "x " -o "x${_local: -1}" = "x\n" -o "x${_local: -1}" = "x\r" -o "x${_local: -1}" = 'x
' ]; do
let __=${#_local}-1
_local="${_local:0:$__}"
done
eval export "${1}"='${_local}'
unset _local
fi
}
# +---------------------------------------------------------------------------+ #
# | Last subversion informations :
# | $Revision: 20 $ : $Date: 2010-04-12 17:43:50 +0200 (lun. 12 avril 2010) $
# | $Author: flyounet $
# +---------------------------------------------------------------------------+ #