All pastes #586229 Raw Edit

Untitled

public text v1 · immutable
#586229 ·published 2007-06-23 18:39 UTC
rendered paste body
package require tdom
#set root ""
set cell(lib)   "libcan"
set cell(name1) "OTA"
set cell(view1) "schematic"
set cell(tool)  "xcircuit-schematic"

set design "design.xml1"
set count 1
namespace eval ::WalkXML { variable stack}

proc ::WalkXML::NextNode {{XML ""}} {
    variable stack                              ;# List of VISITED ancestors

    if {$XML ne ""} {                           ;# Parse given xml
        set doc [dom parse $XML]
        set root [$doc documentElement]
        set stack [list $root]
        return $root
    }

    while {1} {
        set last [lindex $stack end]            ;# Last already visited node

        set next [$last firstChild]             ;# Try going down the tree
        if {$next ne ""} break

        set stack [lrange $stack 0 end-1]
        set next [$last nextSibling]            ;# Try going across the tree
        if {$next ne ""} break

        while {1} {                             ;# Must go up the tree
            set next [lindex $stack end]        ;# Parent
            set stack [lrange $stack 0 end-1]
            if {$next eq ""} break

            set next [$next nextSibling]        ;# Try parent's sibling
            if {$next ne ""} break
        }
        break
    }
    if {$next ne ""} {
        lappend stack $next
    }
    return $next
}


if [file isfile $design] {
    set fp   [open $design]
    set XML  [read $fp]
    close $fp

    set doc  [dom parse $XML]
    set root [$doc documentElement]

} else {
    set doc  [dom parse "<Profiles/>"]
    set root [$doc documentElement]
    $root setAttribute version 0.1
    $root appendXML "<library name='$cell(lib)'/>"
    [$root selectNodes /Profiles/library] appendXML "<cell name='$cell(name1)'/>"
}

set cell_name [[$root selectNodes /Profiles/library/cell] getAttribute name]

if { $cell_name == $cell(name1) } {
    [$root selectNodes /Profiles/library/cell] \
        appendXML "<view name='$cell(view1)' toOpenWith='$cell(tool)'/>"
} else {
    [$root selectNodes /Profiles/library] appendXML "<cell name='$cell(name1)'/>"


    set node [::WalkXML::NextNode [$root asXML]]

    while {$node ne ""} {
        if {[$node nodeType] eq "TEXT_NODE"} {
            puts "\"[$node nodeValue]\""
        } else {
            if { [$node nodeName] == "cell" } {
                set cell_name [$node getAttribute name]
                if { $cell_name == $cell(name1) } {
                    set rode [ [$root selectNodes /Profiles/library] child $count]
                    set rode1 [ $rode nodeName ]
                    $rode appendXML "<view name='$cell(view1)' toOpenWith='$cell(tool)'/>"
                }
                set $count [ expr [incr count] ]
            }

        }
        set node [::WalkXML::NextNode]
    }
}
puts [$root asXML]