From eb7ad4cd8eb1992a0c3c3ad9a087b2506d9e99cf Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Wed, 8 Nov 2023 00:44:29 +0100 Subject: [PATCH] cleanup set_netlist_dir tcl proc --- src/actions.c | 18 +++++++-- src/xschem.h | 2 +- src/xschem.tcl | 103 +++++++++++++++++++++++++------------------------ 3 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/actions.c b/src/actions.c index a106aa52..74769fd3 100644 --- a/src/actions.c +++ b/src/actions.c @@ -311,11 +311,23 @@ void set_grid(double newgrid) tclsetdoublevar("cadgrid", cg); } -int set_netlist_dir(int force, const char *dir) + +/* + * + * change==0: force creation of $netlist_dir (if netlist_dir variable not empty) + * + * change==1: if no dir given prompt user + * else set netlist_dir to dir + * + * Return 1 if netlist_dir is a valid directory and existing + * Return 0 otherwise + * + */ +int set_netlist_dir(int change, const char *dir) { char cmd[PATH_MAX+200]; - if(dir) my_snprintf(cmd, S(cmd), "set_netlist_dir %d {%s}", force, dir); - else my_snprintf(cmd, S(cmd), "set_netlist_dir %d", force); + if(dir) my_snprintf(cmd, S(cmd), "set_netlist_dir %d {%s}", change, dir); + else my_snprintf(cmd, S(cmd), "set_netlist_dir %d", change); tcleval(cmd); if(!strcmp("", tclresult()) ) { return 0; diff --git a/src/xschem.h b/src/xschem.h index 302c55ef..53e65d58 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -1233,7 +1233,7 @@ extern void dbg(int level, char *fmt, ...); extern unsigned int hash_file(const char *f, int skip_path_lines); extern void here(double i); extern void print_version(void); -extern int set_netlist_dir(int force, const char *dir); +extern int set_netlist_dir(int change, const char *dir); extern void netlist_options(int i); extern int check_lib(int what, const char *s); extern int floaters_from_selected_inst(); diff --git a/src/xschem.tcl b/src/xschem.tcl index 958a5235..9d03f07c 100644 --- a/src/xschem.tcl +++ b/src/xschem.tcl @@ -3769,18 +3769,11 @@ proc make_symbol_lcc {name} { return {} } -# create simulation dir 'simulation/' under current schematic directory +# point netlist_dir to simulation dir 'simulation/' under current schematic directory proc simuldir {} { global netlist_dir local_netlist_dir has_x if { $local_netlist_dir == 1 } { set simdir [xschem get current_dirname]/simulation - - # if {[catch {file mkdir "$simdir"} err]} { - # puts $err - # if {[info exists has_x]} { - # tk_messageBox -message "$err" -icon error -parent [xschem get topwindow] -type ok - # } - # } set netlist_dir $simdir return $netlist_dir } @@ -3788,64 +3781,72 @@ proc simuldir {} { } # -# force==0: force creation of $netlist_dir (if netlist_dir variable not empty) +# change==0: force creation of $netlist_dir (if netlist_dir variable not empty) # and return current setting. -# if netlist_dir variable empty: -# if no dir given prompt user -# else set netlist_dir to dir # -# force==1: if no dir given prompt user +# change==1: if no dir given prompt user # else set netlist_dir to dir # # Return current netlist directory # -proc set_netlist_dir { force {dir {} }} { +proc set_netlist_dir { change {dir {} }} { global netlist_dir env OS has_x + #### set local-to-schematic-dir if local_netlist_dir tcl var is set simuldir - if { ( $force == 0 ) && ( $netlist_dir ne {} ) } { - if {![file exist $netlist_dir]} { - if {[catch {file mkdir "$netlist_dir"} err]} { - puts $err - if {[info exists has_x]} { - tk_messageBox -message "$err" -icon error -parent [xschem get topwindow] -type ok - } + + #### change == 0 + if {$change == 0} { + if {$netlist_dir ne {}} { + if {![file exist $netlist_dir]} { + if {[catch {file mkdir "$netlist_dir"} err]} { + puts $err + if {[info exists has_x]} { + tk_messageBox -message "$err" -icon error -parent [xschem get topwindow] -type ok + } + } } - } - regsub {^~/} $netlist_dir ${env(HOME)}/ netlist_dir - return $netlist_dir - } - if { $dir eq {} } { - if { $netlist_dir ne {} } { - set initdir $netlist_dir - } else { - if {$OS == "Windows"} { - set initdir $env(windir) - } else { - set initdir [pwd] - } - } - # 20140409 do not change netlist_dir if user Cancels action - set new_dir [tk_chooseDirectory -initialdir $initdir \ - -parent [xschem get topwindow] -title {Select netlist DIR} -mustexist false] + } + #### change == 1 } else { - set new_dir $dir - } - - if {$new_dir ne {} } { - if {![file exist $new_dir]} { - if {[catch {file mkdir "$new_dir"} err]} { - puts $err - if {[info exists has_x]} { - tk_messageBox -message "$err" -icon error -parent [xschem get topwindow] -type ok - } + if { $dir eq {} } { + if { $netlist_dir ne {} } { + set initdir $netlist_dir + } else { + if {$OS == "Windows"} { + set initdir $env(windir) + } else { + set initdir [pwd] + } } - + # prompt user for a dir + set new_dir [tk_chooseDirectory -initialdir $initdir \ + -parent [xschem get topwindow] -title {Select netlist DIR} -mustexist false] + } else { + # use provided dir argument + set new_dir $dir + } + + # create new dir if not already existing + if {$new_dir ne {} } { + if {![file exist $new_dir]} { + if {[catch {file mkdir "$new_dir"} err]} { + puts $err + if {[info exists has_x]} { + tk_messageBox -message "$err" -icon error -parent [xschem get topwindow] -type ok + } + } + + } + set netlist_dir $new_dir } - set netlist_dir $new_dir } regsub {^~/} $netlist_dir ${env(HOME)}/ netlist_dir - return $netlist_dir + # return $netlist_dir if valid and existing, else return empty string + if {$netlist_dir ne {} && [file exists $netlist_dir]} { + return $netlist_dir + } + return {} }