合并2个不同结构的文件

时间:2017-09-10 08:24:27

标签: tcl

想要使用 name layer 将2个文件 proc_2.tcl 合并到 proc_1.tcl bbox

文件 ports_1.tcl

create_terminal \
        -name {DMItoPCUNotL1XnnnH} \
        -port {DMItoPCUNotL1XnnnH} \
        -layer m10 \
        -bbox {{0.000 2.104} {0.320 2.184}}

set obj [get_terminal {"DMItoPCUNotL1XnnnH"}]
set_attribute -quiet $obj layer  m10
set_attribute -quiet $obj owner_port  {DMItoPCUNotL1XnnnH}
set_attribute -quiet $obj bbox {{0.000 2.104} {0.320 2.184}}
set_attribute -quiet $obj status Fixed
set_attribute -quiet $obj access_direction Left
set_attribute -quiet $obj direction input
set_attribute -quiet $obj eeq_class 0

文件 proc_2.tcl

DMItoPCUNotL1XnnnH M8 {{0.000 4} {0.320 5}}

name : DMItoPCUNotL1XnnnH
layer: M8
bbox : {{0.000 4} {0.320 5}}

1 个答案:

答案 0 :(得分:0)

假设第二个文件是:

DMItoPCUNotL1XnnnH M8 {{0.000 4} {0.320 5}}

然后可以使用它来解析和转换/生成第一个类似的东西:

set TEMPLATE {
    create_terminal \
            -name {$name} \
            -port {$name} \
            -layer $layer \
            -bbox {$bbox}
    set obj [get_terminal {"$name"}]
    set_attribute -quiet \$obj layer $layer
    set_attribute -quiet \$obj owner_port {$name}
    set_attribute -quiet \$obj bbox {$bbox}
    set_attribute -quiet \$obj status Fixed
    set_attribute -quiet \$obj access_direction Left
    set_attribute -quiet \$obj direction input
    set_attribute -quiet \$obj eeq_class 0
}

set f_in [open "file2.whatever.you.call.it"]
set f_out [open "file1.tcl" w]

# For each line 
while {[gets $f_in line] >= 0} {
    # Skip blank lines or lines that start with # so you have comments
    if {$line eq "" || [string match "#*" [string trim $line]]} continue

    # We assume that each line is a complete Tcl list.
    # This is usually a dodgy idea, but looks good in your specific case.
    lassign $line name layer bbox

    # Now generate the text using subst on the template
    # I disable command substitutions; this reduces the number of backslashes needed
    set text [subst -nocommands $TEMPLATE]

    # Write the substituted text to the output file
    puts $f_out $text
}

close $f_in
close $f_out

如果需要,您可以内联很多内容(例如,subst和模板是合理的候选者)。如果您最终从更通用的格式(例如,CSV或JSON)中提取输入数据,那么解析部分就会有所不同 - 有很好的软件包可以提供帮助 - 但是一般模式可识别相似。

请注意,模板由subst处理,因此内部括号不重要。 subst的处理与使用双引号有很多共同点。