Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6a2afa29e9
Fetching contributors…

Cannot retrieve contributors at this time

file 359 lines (332 sloc) 10.016 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#!/usr/bin/tclsh
#
# ps3mfw -- PS3 MFW creator
#
# Copyright (C) Anonymous Developers (Code Monkeys)
#
# This software is distributed under the terms of the GNU General Public
# License ("GPL") version 3, as published by the Free Software Foundation.
#

namespace eval ::xml {

    proc LoadFile {filename} {
set fd [open $filename r]
set data [read $fd]
close $fd
return [Load $data]
    }

    proc Load {xml} {
# Remove any unwanted characters that could appear before/after the xml
regsub {^.*?<} $xml {<} xml
regsub {>[^>]*?$} $xml {>} xml
if { $xml == "" } { return "" }
# Remove xml file header and comments
# Here the ".*?" in the regexp means a non greedy matching,
# which means match as little characters as possible.. the reason, here's an example :
# <!-- comment --> <tag/> <!-- comment2 --> <tag2/>
# the regsub {<!--.*-->} would remove from the first <!-- to the last -->
# which means we end up with <tag2/> and we loose <tag/>..
# if it's greedy, it will match all possible chars, with non-greedy,
# it will match only the smallest number: only the comment...
regsub -all {<\?xml.*?\?>} $xml "" xml
regsub -all {<!--.*?-->} $xml "" xml
# Avoid unmatched braces in list, in case we have a left or right accolade in the xml data
set xml [string map {"\{" "&right_accolade;" "\}" "&left_accolade;" "\\" "&escape_char;"} $xml]

regsub -all {>\s*<} [string trim $xml " \r\n\t<>"] "\} \{" xml
set xml [string map {> "\} \{#text \{" < "\}\} \{" } $xml]

set res "" ;# string to collect the result
set stack {} ;# track open tags
set rest {}
foreach item "{$xml}" {
switch -regexp -- $item {
^# {
append res "{[lrange $item 0 end]} " ; #text item
}
^/ {
regexp {/(.+)} $item -> tagname ;# end tag
set expected [lindex $stack end]
if {$tagname!=$expected} {error "$item != $expected"}
set stack [lrange $stack 0 end-1]
append res "\}\} "
}
/$ { # singleton - start and end in one <> group
regexp {([^\s]+)(\s(.+))?/$} $item -> tagname - rest
set rest [regsub -all {\s*([^=\s]+)=\"([^\"]+)\"\s*} $rest {\1 {\2} }]
set rest [regsub -all {\s*([^=\s]+)=\'([^\']+)\'\s*} $rest {\1 {\2} }]
append res "{$tagname [list $rest] {}} "
}
default {
regexp {([^\s]+)(\s(.+))?$} $item -> tagname - rest
set rest [regsub -all {\s*([^=\s]+)=\"([^\"]+)\"\s*} $rest {\1 {\2} }]
set rest [regsub -all {\s*([^=\s]+)=\'([^\']+)\'\s*} $rest {\1 {\2} }]
lappend stack $tagname
append res "\{$tagname [list $rest] \{"
}
}
if {[llength $rest]%2} {error "att's not paired: $rest"}
}
if [llength $stack] {error "unresolved: $stack"}
# Unescape chars and accolades
string map {"\} \}" "\}\}" "&right_accolade;" "\\\{" "&left_accolade;" "\\\}" "&escape_char;" "\\\\"} [xmldecode [lindex $res 0] 1]
}

proc Print {list {depth -1}} {
set res ""
switch -- [llength $list] {
2 {
append res [xmlencode [lindex $list 1]]
}
3 {
foreach {tag attributes children} $list break
if {$depth > 0} {
append res [string repeat " " $depth]
}
append res <$tag
foreach {name value} $attributes {
append res " $name=\"$value\""
}
if {[llength $children] == 1 && [llength [lindex $children 0]] == 2} {
append res >
append res [Print [lindex $children 0]]
append res </$tag>
if {$depth >= 0} {
append res "\n"
}
} elseif {[llength $children] > 0} {
set child_depth $depth
append res >
if {$depth >= 0} {
append res "\n"
incr child_depth
}
foreach child $children {
append res [Print $child $child_depth]
}
if {$depth > 0} {
append res [string repeat " " $depth]
}
append res </$tag>
if {$depth >= 0} {
append res "\n"
}
} else {
append res />
if {$depth >= 0} {
append res "\n"
}
}
}
default {error "could not parse $list"}
}
return $res
    }
    
    proc xmlencode {string} {
return [string map {
"<" "&lt;"
">" "&gt;"
"\"" "&quot;"
"\t" "&#x9;"
"\r" "&#xd;"
"\n" "&#xa;"} $string]
    }

    proc xmldecode {string {escape 0}} {
set parsed ""
while {[set pos [string first "&#x" $string]] != -1 } {
append parsed [string range $string 0 [expr {$pos - 1}]]
incr pos 3
set byte ""
while {[set char [string range $string $pos $pos]] != ";" } {
append byte $char
incr pos
}
if {[expr {[string length $byte] % 2}] == 1} {
set byte "[string range $byte 0 end-1]0[string range $byte end end]"
}
set value [binary format H* $byte]
if {$escape } {
if {$value == "\{" } {
set value "\\\{"
} elseif {$value == "\}" } {
set value "\\\}"
} elseif {$value == "\\" } {
set value "\\\\"
}
}
append parsed $value
set string [string range $string [expr {$pos + 1}] end]
}
append parsed $string
return [string map { "&lt;" "<" "&gt;" ">" "&amp;" "&" "&quot;" "\"" "&apos;" "'" } $parsed]
    }

    proc PrettyPrint { xml } {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n[Print [Load $xml] 0]"
    }

    proc SaveToFile { list filename {pretty 1} } {
if {$pretty} {
set depth 0
} else {
set depth -1
}
set xml [Print $list $depth]
set fd [open $filename w]
puts $fd $xml
close $fd
    }
    
    proc GetNodeIndices {list find {no 0} {stack ""}} {
variable xmlTag_occurences

if {$stack == "" } {
set xmlTag_occurences 0
}
set current_stack $stack
set index -1
foreach { tag attributes content} $list {
incr index
set current_stack "$stack:$tag"
if {$current_stack == $find || $current_stack == ":$find" } {
#status_log "Found it in $current_stack\n" red
if { $no == $xmlTag_occurences } {
return 0
} else {
incr xmlTag_occurences
}
return ""
} else {
if {[string first $current_stack $find] == -1 &&
[string first $current_stack ":$find"] == -1 } {
#status_log "$find not in $current_stack" red
continue
} else {
set index 2
set subindex 0
#status_log "$find is in a subkey of $current_stack\n" red
foreach subkey $content {
set result [GetNodeIndices $subkey $find $no $current_stack]
if { $result != "" } {
eval lappend index $subindex $result
return $index
}
incr subindex
}
}
}
}

return ""
    }

    proc GetNodeByIndex {list indices} {
if {$indices == ""} {
return ""
}
set indices [lrange $indices 0 end-1]
if {$indices == {}} {
return $list
} else {
return [eval lindex [list $list] $indices]
}
    }

    proc GetNode {list find {no 0}} {
return [GetNodeByIndex $list [GetNodeIndices $list $find $no]]
    }

    proc GetData {list find {no 0}} {
set node [GetNode $list $find $no]
if {$node != ""} {
foreach { tag attributes children} $node break
foreach child $children {
if {[llength $child] == 2} {
return [string map {"\\\\" "\\" "\\\{" "\{" "\\\}" "\}" } [lindex $child 1]]
}
}
}
return ""
    }
    
    proc GetAttribute { list find attribute_name {no 0}} {
set node [GetNode $list $find $no]
if {$node != ""} {
foreach { tag attributes children} $node break
array set attributes_arr $attributes
if { [info exists attributes_arr($attribute_name)] } {
return [string map {"\\\\" "\\" "\\\{" "\{" "\\\}" "\}" } [set attributes_arr($attribute_name)]]
}
}
return ""
    }
    
    proc ListChildren {list find {no 0}} {
set node [GetNode $list $find $no]
if {$node != ""} {
foreach { tag attributes children} $node break
set tags [list]
foreach child $children {
if {[llength $child] == 3} {
lappend tags [lindex $child 0]
}
}
return $tags
}
return ""
    }

    proc GetAttributes { list find {no 0}} {
set node [GetNode $list $find $no]
if {$node != ""} {
foreach { tag attributes children} $node break
return $attributes
}
return ""
    }
    
    proc ListAttributes { list find {no 0}} {
array set attributes [GetAttributes $list $find $no]
return [array names attributes]
    }

    proc ReplaceNode {list indices node} {
return [__modifyNode $list $indices $node "replace"]
    }

    proc InsertNode {list indices node} {
return [__modifyNode $list $indices $node "insert"]
    }

    proc RemoveNode {list indices} {
return [__modifyNode $list $indices {} "remove"]
    }

    proc GetNodeIndicesByAttribute {list find attribute value {no 0}} {
regsub {:$} $find {} find
regsub {.*:([^:]+)$} $find {\1} tag
set i -1
set index 0
while {1} {
incr i
set n [GetNode $list $find $i]
if {$n == "" } break
set key [GetAttribute $n $tag $attribute]
if {$key == $value} {
if {$no == $index} {
return [GetNodeIndices $list $find $i]
} else {
incr index
}
}
}
return ""
    }
    proc GetNodeByAttribute {list find attribute value {no 0}} {
return [GetNodeByIndex $list [GetNodeIndicesByAttribute $list $find $attribute $value $no]]
    }

    # Helper Functions
    proc __lmod_r {list indices mod {val {}}} {
if { [llength $indices] == 0 } {
return $val
} elseif {[llength $indices] == 1} {
if {$mod == "insert"} {
return [linsert $list [lindex $indices 0] $val]
} elseif {$mod == "remove"} {
return [lreplace $list [lindex $indices 0] [lindex $indices 0]]
} else {
return [lreplace $list [lindex $indices 0] [lindex $indices 0] $val]
}
} else {
return [lreplace $list [lindex $indices 0] [lindex $indices 0] [__lmod_r [lindex $list [lindex $indices 0]] [lrange $indices 1 end] $mod $val]]
}
    }
    proc __modifyNode {list indices node operation} {
set indices [lrange $indices 0 end-1]
if {$indices == {}} {
return $node
} else {
return [__lmod_r $list $indices $operation $node]
}
    }

}
Something went wrong with that request. Please try again.