📄 stafutil.tcl
字号:
set colonIndex2 [string first ":" $md [expr $colonIndex + 1]] set mapClassNameLength [string range $md [expr $colonIndex + 1] [expr $colonIndex2 - 1]] set mapClassName [string range $md [expr $colonIndex2 + 1] [expr $colonIndex2 + $mapClassNameLength]] set dataIndex [expr $colonIndex2 + 1 + $mapClassNameLength] set map($STAF::MAP_CLASS_NAME_KEY) $mapClassName set mapClass [STAF::mcontext getMapClassDefinition $context $mapClassName] set keys [STAF::datatype getValue [STAF::mapclassdef getKeys $mapClass]] set keyIndex 0 set dataLength [string length $md] while {$dataIndex < $dataLength} { set colonIndex [string first ":" $md $dataIndex] set colonIndex2 [string first ":" $md [expr $colonIndex + 1]] set itemLength [string range $md [expr $colonIndex + 1] [expr $colonIndex2 - 1]] set dataToUnmarshall [string range $md $dataIndex [expr $colonIndex2 + $itemLength]] set outMC [eval {STAF::unmarshall} -context {$context} $flags {$dataToUnmarshall}] array set theKey [STAF::datatype getValue [lindex $keys $keyIndex]] set map($theKey(key)) [STAF::mcontext getPrimaryObject $outMC] set dataIndex [expr $colonIndex2 + $itemLength + 1] incr keyIndex } return [STAF::mcontext create [STAF::datatype createMap [array get map]]] } elseif {[STAF::stringStartsWith $md $STAF::CONTEXT_MARKER]} { set colonIndex [string first ":" $md] set contextIndex [expr [string first ":" $md [expr $colonIndex + 1]] + 1] set contextLength [string range $md [expr $colonIndex + 1] [expr $contextIndex - 2]] set colonIndex [string first ":" $md $contextIndex] set mapIndex $contextIndex set mapDataIndex [expr [string first ":" $md [expr $colonIndex + 1]] + 1] set mapLength [string range $md [expr $colonIndex + 1] [expr $mapDataIndex - 2]] set dataToUnmarshall [string range $md $mapIndex [expr $mapDataIndex + $mapLength - 1]] set outMC [eval {STAF::unmarshall} -context {$context} $flags {$dataToUnmarshall}] set contextMapObj [STAF::mcontext getPrimaryObject $outMC] array set contextMap [STAF::datatype getValue $contextMapObj] set mapClassMapObj $contextMap($STAF::MAP_CLASS_MAP_KEY) set newContext [STAF::mcontext create [STAF::datatype createNone] $mapClassMapObj] set colonIndex [string first ":" $md [expr $mapDataIndex + $mapLength]] set rootObjIndex [expr $mapDataIndex + $mapLength] set rootObjDataIndex [expr [string first ":" $md [expr $colonIndex + 1]] + 1] set rootObjLength [string range $md [expr $colonIndex + 1] [expr $rootObjDataIndex - 2]] set dataToUnmarshall [string range $md $rootObjIndex [expr $rootObjDataIndex + $rootObjLength - 1]] set outMC [eval {STAF::unmarshall} -context {$newContext} $flags {$dataToUnmarshall}] STAF::mcontext setRootObject newContext [STAF::mcontext getPrimaryObject $outMC] return $newContext } else { return [STAF::mcontext create $md] }}proc STAF::formatObject {args} { # options: ?-context <context>? ?-indentLevel <indentLevel>? # args: object # Note that the optional -indentLevel option is not meant to be used # by a user calling the formatObject procedure, thus we don't document # it externally. It's meant to be used internally by the formatObject # method when recursively calling itself. set myOptions [list [list context ""] [list indentLevel 0]] set myArgs [list obj] set checkResult [STAF::internalCheckCommandInput $args $myOptions $myArgs] if {[lindex [lindex $checkResult 0] 0] != 0} { error [lindex [lindex $checkResult 0] 1] } array set options [lindex $checkResult 1] array set input [lindex $checkResult 2] set obj $input(obj) set context $options(context) set indentLevel $options(indentLevel) # Begin formatting set output "" set lineSep "\n" if {[STAF::datatype getType $obj] == $STAF::ListType} { set theList [STAF::datatype getValue $obj] set theListLength [llength $theList] append output "\[" incr indentLevel 1 if {$theListLength > 0} { append output $lineSep } # Format each object in the list set index 1 foreach item $theList { set indent [expr ($indentLevel * $STAF::INDENT_DELTA) - 1] append output [string range $STAF::SPACES 0 $indent] if {[STAF::datatype getType $item] == $STAF::ListType || \ [STAF::datatype getType $item] == $STAF::MapType || \ [STAF::datatype getType $item] == $STAF::ContextType} { append output [eval {STAF::formatObject} \ -context {$context} -indentLevel $indentLevel {$item}] } elseif {[STAF::datatype getType $item] == $STAF::NoneType} { append output $STAF::NONE_STRING } else { append output [STAF::datatype getValue $item] } if {$index < $theListLength} { append output $STAF::ENTRY_SEPARATOR } append output $lineSep incr index 1 } incr indentLevel -1 if {$theListLength > 0} { set indent [expr ($indentLevel * $STAF::INDENT_DELTA) - 1] append output [string range $STAF::SPACES 0 $indent] } append output "]" } elseif {[STAF::datatype getType $obj] == $STAF::MapType} { array set map [STAF::datatype getValue $obj] append output "\{" incr indentLevel 1 set mapNameList [array names map] set mapSize [llength $mapNameList] if {$mapSize > 0} { append output $lineSep } if {$context != "" && \ [STAF::datatype getType $context] == $STAF::ContextType && \ [array names map $STAF::MAP_CLASS_NAME_KEY] != "" && \ [STAF::mcontext hasMapClassDefinition $context \ $map($STAF::MAP_CLASS_NAME_KEY)]} { set mapClassName $map($STAF::MAP_CLASS_NAME_KEY) set mapClass [STAF::mcontext getMapClassDefinition $context \ $mapClassName] set keyListObj [STAF::mapclassdef getKeys $mapClass] set keyList [STAF::datatype getValue $keyListObj] # Determine maximum key length set maxKeyLength 0 foreach keyObj $keyList { array set key [STAF::datatype getValue $keyObj] set keyName $key(key) if {[array names key $STAF::DISPLAY_NAME_KEY] != ""} { set keyName $key($STAF::DISPLAY_NAME_KEY) } set keyLength [string length $keyName] if {$keyLength > $maxKeyLength} { set maxKeyLength $keyLength } } # Now print each object in the map set index 1 set keyListLength [llength $keyList] foreach keyObj $keyList { array set key [STAF::datatype getValue $keyObj] set keyName $key(key) if {[array names key $STAF::DISPLAY_NAME_KEY] != ""} { set keyName $key($STAF::DISPLAY_NAME_KEY) } set indent [expr ($indentLevel * $STAF::INDENT_DELTA) - 1] append output [string range $STAF::SPACES 0 $indent] append output $keyName set indent [expr $maxKeyLength - [string length $keyName] - 1] append output [string range $STAF::SPACES 0 $indent] append output ": " set theObj $map($key(key)) if {[STAF::datatype getType $theObj] == $STAF::ListType || \ [STAF::datatype getType $theObj] == $STAF::MapType || \ [STAF::datatype getType $theObj] == $STAF::ContextType} { append output [eval {STAF::formatObject} \ -context {$context} -indentLevel $indentLevel \ {$theObj}] } elseif {[STAF::datatype getType $theObj] == $STAF::NoneType} { append output $STAF::NONE_STRING } else { append output [STAF::datatype getValue $theObj] } if {$index < $keyListLength} { append output $STAF::ENTRY_SEPARATOR } append output $lineSep incr index 1 } } else { # Determine maximum key length set maxKeyLength 0 foreach key [array names map] { set keyLength [string length $key] if {$keyLength > $maxKeyLength} { set maxKeyLength $keyLength } } # Now print each object in the map set index 1 foreach key [array names map] { set keyLength [string length $key] set indent [expr ($indentLevel * $STAF::INDENT_DELTA) - 1] append output [string range $STAF::SPACES 0 $indent] append output $key set indent [expr $maxKeyLength - $keyLength - 1] append output [string range $STAF::SPACES 0 $indent] append output ": " set theObj $map($key) if {[STAF::datatype getType $theObj] == $STAF::ListType || \ [STAF::datatype getType $theObj] == $STAF::MapType || \ [STAF::datatype getType $theObj] == $STAF::ContextType} { append output [eval {STAF::formatObject} \ -context {$context} -indentLevel $indentLevel \ {$theObj}] } elseif {[STAF::datatype getType $theObj] == $STAF::NoneType} { append output $STAF::NONE_STRING } else { append output [STAF::datatype getValue $theObj] } if {$index < $mapSize} { append output $STAF::ENTRY_SEPARATOR } append output $lineSep incr index 1 } } incr indentLevel -1 if {$mapSize > 0} { set indent [expr ($indentLevel * $STAF::INDENT_DELTA) - 1] append output [string range $STAF::SPACES 0 $indent] } append output "\}" } elseif {[STAF::datatype getType $obj] == $STAF::ContextType} { return [eval {STAF::formatObject} \ -context {$obj} -indentLevel $indentLevel \ {[STAF::mcontext getRootObject $obj]}] } elseif {[STAF::datatype getType $obj] == $STAF::NoneType} { return $STAF::NONE_STRING } else { # Treat this as a scalar return [STAF::datatype getValue $obj] } return $output}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -