【问题标题】:Is the Tcl `file dirname` command usable for URL?Tcl `file dirname` 命令可用于 URL 吗?
【发布时间】:2015-06-11 09:35:00
【问题描述】:

我想从 Subversion HTTP URL 中删除一些额外的文件/文件夹级别以在 Tcl 脚本中获取分支根 URL。

例如:

http://svn.example.com/repos/trunk/file.tcl

-->

http://svn.example.com/repos/trunk/

我尝试使用file dirname 命令,但遇到了一些“意外行为”(至少从我的角度来看):

file dirname {http://svn.example.com/repos/trunk/file.tcl}

返回(Windows 7,来自 Altera Quartus 发行版的 Tcl 8.5):

http:svn.example.com/repos/trunk

网址前缀的“//”斜线已被删除!?!


在其他 Tcl 版本(Windows 7、Cygwin / Linux 的 Tcl 8.5、Tcl 8.5)中,我得到了不同但仍然不正确的结果:

http:/svn.example.com/repos/trunk

(两个“//”斜杠中的一个“/”已被删除)

为什么会有这样的结果?

还有其他选择吗?(除了使用string laststring range...的自定义函数)


注意:file separator 命令根据版本返回不同的结果:

  • \”在来自 Altera Quartus 的 Windows “native” Tcl 中;
  • /”在 Tcl Cygwin / Linux 版本中。

【问题讨论】:

    标签: url svn tcl


    【解决方案1】:

    file 命令设计用于处理文件和文件名,而不是 URL。这意味着它所做的事情(例如删除多余的 / 字符)对于文件名是正确,但对于 URL 是错误,这完全是故意的。

    uri package in Tcllib 是您真正需要的,因为它可以让您从 URL 中提取路径( 类似于文件名),以便您对其进行操作:

    package require uri
    set uri http://svn.example.com/repos/trunk/file.tcl
    
    # Split a URL up into its components
    set uricomponents [uri::split $uri]
    # ==> fragment {} port {} path repos/trunk/file.tcl scheme http host svn.example.com query {} pwd {} user {}
    
    # Manipulate the dictionary, in this case to adjust the path part
    dict with uricomponents {
        set path [file dirname $path]
    }
    
    # Compose back into a normal URL
    set newuri [uri::join {*}$uricomponents]
    # ==> http://svn.example.com/repos/trunk
    

    是的,dict with 非常适合这种事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多