Skip to content
Browse files

improve internal/variables/file

  • Loading branch information...
1 parent 47e0c48 commit 56ae645dab2374dfdfc3d4138b560a5d2c26ba04 @mpapis mpapis committed Jul 31, 2012
View
27 core/internal/shell/variables/file/functions
@@ -2,25 +2,28 @@
__sm_variables_file_read()
{
- eval "\$$1=\"\$(cat \"\${2:-\$1}\")\""
+ eval "$1=\"\$(cat \"\${2:-\$1}\")\""
}
__sm_variables_file_write()
{
eval "echo \"\$$1\"" > "${2:-$1}"
}
+__sm_variables_file_read_one()
+{
+ eval "${2//-/_}=\"\$(sed '/^$2=/! d; s/^$2=//' < \"\$1\")\""
+}
+
__sm_variables_file_read_many()
{
typeset __sm_variables_file
__sm_variables_file="$1"
shift
- while
- (( $# ))
+ for name
do
- eval "${1//-/_}=\"\$(awk -F= '\$1==\"$1\" {sub(\"^$1=\",\"\"); print}' < \"\${__sm_variables_file}\")\""
- shift
+ __sm_variables_file_read_one "${__sm_variables_file}" "$name"
done
}
@@ -32,9 +35,15 @@ __sm_variables_file_read_all()
while read name
do
- eval "${name//-/_}=\"\$(awk -F= '\$1==\"$name\" {sub(\"^$name=\",\"\"); print}' < \"\${__sm_variables_file}\")\""
- done < <( awk -F= '{print $1}' < "${__sm_variables_file}" )
- # n+1 awk is slow but it's also safe
+ __sm_variables_file_read_one "${__sm_variables_file}" "$name"
+ done \
+ < <( awk -F= '{print $1}' < "${__sm_variables_file}" )
+ # n+1 read is slow but it's the only safe one
+}
+
+__sm_variables_file_write_one()
+{
+ eval "sed -i '/^$1=/ s/^.*$/$1='\"\${${1//-/_}}\"'/' \"\${__sm_variables_file}\""
}
__sm_variables_file_write_many()
@@ -46,7 +55,7 @@ __sm_variables_file_write_many()
while
(( $# ))
do
- eval "sed -i '/^$1=/ s/^.*$/$1='\"\${${1//-/_}}\"'/' \"\${__sm_variables_file}\""
+ __sm_variables_file_write_one "${__sm_variables_file}" "$1"
shift
done
}
View
12 core/internal/shell/variables/file/simple_file_comment_test.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env zsh
+
+source test-sm
+includes internal/variables/file
+
+export _sm_version_read
+__sm_variables_file_read _sm_version_read VERSION
+# status=0
+# env[_sm_version_read]=/[[:digit:]\.]+/
+
+echo $_sm_version_read
+# match=/[[:digit:]\.]+/
View
5 test-sm
@@ -0,0 +1,5 @@
+export sm_script=/
+source ./local
+set -o NO_ERR_EXIT ERR_RETURN
+trap - USR2
+trap - ZERR

0 comments on commit 56ae645

Please sign in to comment.
Something went wrong with that request. Please try again.