From 615e4289ec90871791cea6edc70ce6a9d4be4f3c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 29 Nov 2011 04:51:36 +0000 Subject: add expand_vars proc to expand vars like /Users/hans, , etc within the file names svn path=/trunk/externals/tclfile/; revision=15802 --- tclfile.tcl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tclfile.tcl') diff --git a/tclfile.tcl b/tclfile.tcl index 7700a5d..61db3f2 100644 --- a/tclfile.tcl +++ b/tclfile.tcl @@ -1,3 +1,4 @@ + package require Tclpd 0.2.3 package provide tclfile 0.1 @@ -12,3 +13,22 @@ proc tclfile::make_symbol {argslist} { } return $output } + +# expand things like ~ $HOME $ProgramFiles +proc tclfile::expand_vars {filename} { + set sub $filename + foreach var [regexp -all -inline -- {\$\w+} $filename] { + regexp -- {\$(\w+)} $var -> varname + if {[catch {set got $::env($varname)} fid]} { + #puts stderr "caught $fid" + } else { + set sub [string map [list "\$$varname" $got \{ "" \} ""] $filename] + # TODO this should really be a regex that properly + # recognizes {} around the symbol as separate from just {} + # used in a filename. But first, Pd will need a full + # escaping mechanism so it can allow {} + #set sub [regsub "\(.+\)HOME\(.+\)" $sub "==\1==$got==\2=="] + } + } + return $sub +} -- cgit v1.2.1