47dcf5fc27 2019-05-01 rkeene: #! /usr/bin/env tclsh
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: namespace eval ::xvfs {}
0e8537c65f 2019-09-20 rkeene: namespace eval ::xvfs::callback {}
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: set ::xvfs::_xvfsDir [file dirname [info script]]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: # Functions
0bdbe4333e 2019-09-20 rkeene: proc ::xvfs::_emitLine {line} {
eebfe1f40f 2020-03-25 rkeene: lappend ::xvfs::_emitLine $line
0bdbe4333e 2019-09-20 rkeene: }
0bdbe4333e 2019-09-20 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::printHelp {channel {errors ""}} {
47dcf5fc27 2019-05-01 rkeene: if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01 rkeene: foreach error $errors {
702c74c153 2019-09-20 rkeene: puts $channel "error: $error"
47dcf5fc27 2019-05-01 rkeene: }
702c74c153 2019-09-20 rkeene: puts $channel ""
47dcf5fc27 2019-05-01 rkeene: }
702c74c153 2019-09-20 rkeene: puts $channel "Usage: dir2c \[--help\] \[--output <filename>\] --directory <rootDirectory> --name <fsName>"
47dcf5fc27 2019-05-01 rkeene: flush $channel
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::sanitizeCString {string} {
47dcf5fc27 2019-05-01 rkeene: set output [join [lmap char [split $string ""] {
47dcf5fc27 2019-05-01 rkeene: if {![regexp {[A-Za-z0-9./-]} $char]} {
47dcf5fc27 2019-05-01 rkeene: binary scan $char H* char
47dcf5fc27 2019-05-01 rkeene: set char "\\[format %03o 0x$char]"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set char
47dcf5fc27 2019-05-01 rkeene: }] ""]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: return $output
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
32b55a907b 2019-05-02 rkeene: proc ::xvfs::sanitizeCStringList {list {prefix ""} {width 80}} {
32b55a907b 2019-05-02 rkeene: set lines [list]
32b55a907b 2019-05-02 rkeene: set row [list]
32b55a907b 2019-05-02 rkeene: foreach item $list {
32b55a907b 2019-05-02 rkeene: lappend row "\"[sanitizeCString $item]\""
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: set rowString [join $row {, }]
32b55a907b 2019-05-02 rkeene: set rowString "${prefix}${rowString}"
32b55a907b 2019-05-02 rkeene: if {[string length $rowString] > $width} {
32b55a907b 2019-05-02 rkeene: set row [list]
d961175fd9 2019-09-20 rkeene: lappend lines "${rowString},"
32b55a907b 2019-05-02 rkeene: unset rowString
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: if {[info exists rowString]} {
32b55a907b 2019-05-02 rkeene: lappend lines $rowString
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: return [join $lines "\n"]
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::binaryToCHex {binary {prefix ""} {width 10}} {
2176e9cacf 2019-09-18 rkeene: set binary [binary encode hex $binary]
47dcf5fc27 2019-05-01 rkeene: set output [list]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set width [expr {$width * 2}]
47dcf5fc27 2019-05-01 rkeene: set stopAt [expr {$width - 1}]
47dcf5fc27 2019-05-01 rkeene:
2176e9cacf 2019-09-18 rkeene: set offset 0
2176e9cacf 2019-09-18 rkeene: while 1 {
2176e9cacf 2019-09-18 rkeene: set row [string range $binary $offset [expr {$offset + $stopAt}]]
2176e9cacf 2019-09-18 rkeene: if {[string length $row] == 0} {
2176e9cacf 2019-09-18 rkeene: break
2176e9cacf 2019-09-18 rkeene: }
2176e9cacf 2019-09-18 rkeene: incr offset [string length $row]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set rowOutput [list]
47dcf5fc27 2019-05-01 rkeene: while {$row ne ""} {
47dcf5fc27 2019-05-01 rkeene: set value [string range $row 0 1]
47dcf5fc27 2019-05-01 rkeene: set row [string range $row 2 end]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: lappend rowOutput "\\x$value"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: set rowOutput [join $rowOutput {}]
47dcf5fc27 2019-05-01 rkeene: set rowOutput "${prefix}\"${rowOutput}\""
47dcf5fc27 2019-05-01 rkeene: lappend output $rowOutput
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {[llength $output] == 0} {
47dcf5fc27 2019-05-01 rkeene: return "${prefix}\"\""
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set output [join $output "\n"]
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::processFile {fsName inputFile outputFile fileInfoDict} {
47dcf5fc27 2019-05-01 rkeene: array set fileInfo $fileInfoDict
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: switch -exact -- $fileInfo(type) {
47dcf5fc27 2019-05-01 rkeene: "file" {
47dcf5fc27 2019-05-01 rkeene: set type "XVFS_FILE_TYPE_REG"
d8e00cd4a3 2019-09-20 rkeene: if {[info exists fileInfo(fileContents)]} {
d8e00cd4a3 2019-09-20 rkeene: set data $fileInfo(fileContents)
d8e00cd4a3 2019-09-20 rkeene: } else {
d8e00cd4a3 2019-09-20 rkeene: set fd [open $inputFile]
d8e00cd4a3 2019-09-20 rkeene: fconfigure $fd -encoding binary -translation binary -blocking true
d8e00cd4a3 2019-09-20 rkeene: set data [read $fd]
d8e00cd4a3 2019-09-20 rkeene: close $fd
d8e00cd4a3 2019-09-20 rkeene: }
47dcf5fc27 2019-05-01 rkeene: set size [string length $data]
47dcf5fc27 2019-05-01 rkeene: set data [string trimleft [binaryToCHex $data "\t\t\t"]]
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "directory" {
47dcf5fc27 2019-05-01 rkeene: set type "XVFS_FILE_TYPE_DIR"
32b55a907b 2019-05-02 rkeene: set children $fileInfo(children)
32b55a907b 2019-05-02 rkeene: set size [llength $children]
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: if {$size == 0} {
32b55a907b 2019-05-02 rkeene: set children "NULL"
32b55a907b 2019-05-02 rkeene: } else {
32b55a907b 2019-05-02 rkeene: set children [string trimleft [sanitizeCStringList $children "\t\t\t"]]
32b55a907b 2019-05-02 rkeene: # This initializes it using a C99 compound literal, C99 is required
32b55a907b 2019-05-02 rkeene: set children "(const char *\[\]) \{$children\}"
32b55a907b 2019-05-02 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: default {
47dcf5fc27 2019-05-01 rkeene: return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "\t\{"
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "\t\t.name = \"[sanitizeCString $outputFile]\","
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "\t\t.type = $type,"
32b55a907b 2019-05-02 rkeene: switch -exact -- $fileInfo(type) {
32b55a907b 2019-05-02 rkeene: "file" {
eebfe1f40f 2020-03-25 rkeene: ::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data,"
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: "directory" {
eebfe1f40f 2020-03-25 rkeene: ::xvfs::_emitLine "\t\t.data.dirChildren = $children,"
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene: }
eebfe1f40f 2020-03-25 rkeene: ::xvfs::_emitLine "\t\t.size = $size"
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "\t\},"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
47dcf5fc27 2019-05-01 rkeene: set subDirectories [list]
47dcf5fc27 2019-05-01 rkeene: set outputFiles [list]
47dcf5fc27 2019-05-01 rkeene: set workingDirectory [file join $directory $subDirectory]
47dcf5fc27 2019-05-01 rkeene: set outputDirectory $subDirectory
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$subDirectory eq ""} {
47dcf5fc27 2019-05-01 rkeene: set isTopLevel true
47dcf5fc27 2019-05-01 rkeene: } else {
47dcf5fc27 2019-05-01 rkeene: set isTopLevel false
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$isTopLevel} {
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "static const struct xvfs_file_data xvfs_${fsName}_data\[\] = \{"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: # XXX:TODO: Include hidden files ?
32b55a907b 2019-05-02 rkeene: set children [list]
47dcf5fc27 2019-05-01 rkeene: foreach file [glob -nocomplain -tails -directory $workingDirectory *] {
47dcf5fc27 2019-05-01 rkeene: if {$file in {. ..}} {
47dcf5fc27 2019-05-01 rkeene: continue
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set inputFile [file join $workingDirectory $file]
d99958bdd3 2019-05-03 rkeene: set outputFile [file join $outputDirectory [encoding convertto utf-8 $file]]
30c469fcf7 2019-09-20 rkeene: set subDirectoryName [file join $outputDirectory $file]
0e8537c65f 2019-09-20 rkeene:
0e8537c65f 2019-09-20 rkeene: if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
ed3da129b8 2019-09-20 rkeene: set outputFile [::xvfs::callback::setOutputFileName $file $workingDirectory $inputFile $outputDirectory $outputFile]
e592c85e70 2019-09-20 rkeene: if {$outputFile eq "/"} {
0e8537c65f 2019-09-20 rkeene: continue
0e8537c65f 2019-09-20 rkeene: }
0e8537c65f 2019-09-20 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: unset -nocomplain fileInfo
47dcf5fc27 2019-05-01 rkeene: catch {
47dcf5fc27 2019-05-01 rkeene: file lstat $inputFile fileInfo
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: if {![info exists fileInfo]} {
30ffb49c05 2019-09-20 rkeene: puts stderr "warning: Unable to access $inputFile, skipping"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$fileInfo(type) eq "directory"} {
30c469fcf7 2019-09-20 rkeene: lappend subDirectories $subDirectoryName
32b55a907b 2019-05-02 rkeene: continue
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: processFile $fsName $inputFile $outputFile [array get fileInfo]
47dcf5fc27 2019-05-01 rkeene: lappend outputFiles $outputFile
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: foreach subDirectory $subDirectories {
47dcf5fc27 2019-05-01 rkeene: lappend outputFiles {*}[processDirectory $fsName $directory $subDirectory]
47dcf5fc27 2019-05-01 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: set inputFile $directory
32b55a907b 2019-05-02 rkeene: set outputFile $outputDirectory
e592c85e70 2019-09-20 rkeene: if {[info command ::xvfs::callback::setOutputFileName] ne ""} {
e592c85e70 2019-09-20 rkeene: set outputFile [::xvfs::callback::setOutputFileName $directory $directory $inputFile $outputDirectory $outputFile]
e592c85e70 2019-09-20 rkeene: }
32b55a907b 2019-05-02 rkeene:
e592c85e70 2019-09-20 rkeene: if {$outputFile ne "/"} {
e592c85e70 2019-09-20 rkeene: unset -nocomplain fileInfo
e592c85e70 2019-09-20 rkeene: file stat $inputFile fileInfo
30c469fcf7 2019-09-20 rkeene: set children [list]
30c469fcf7 2019-09-20 rkeene: set outputFileLen [string length $outputFile]
30c469fcf7 2019-09-20 rkeene: foreach child $outputFiles {
30c469fcf7 2019-09-20 rkeene: if {[string range /$child 0 $outputFileLen] eq "/${outputFile}"} {
30c469fcf7 2019-09-20 rkeene: set child [string trimleft [string range $child $outputFileLen end] /]
30c469fcf7 2019-09-20 rkeene: if {![string match "*/*" $child]} {
30c469fcf7 2019-09-20 rkeene: lappend children $child
30c469fcf7 2019-09-20 rkeene: }
30c469fcf7 2019-09-20 rkeene: }
30c469fcf7 2019-09-20 rkeene: }
e592c85e70 2019-09-20 rkeene: set fileInfo(children) $children
e592c85e70 2019-09-20 rkeene:
e592c85e70 2019-09-20 rkeene: processFile $fsName $inputFile $outputFile [array get fileInfo]
e592c85e70 2019-09-20 rkeene: lappend outputFiles $outputFile
e592c85e70 2019-09-20 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {$isTopLevel} {
d8e00cd4a3 2019-09-20 rkeene: if {[info command ::xvfs::callback::addOutputFiles] ne ""} {
e592c85e70 2019-09-20 rkeene: lappend outputFiles {*}[::xvfs::callback::addOutputFiles $fsName]
d8e00cd4a3 2019-09-20 rkeene: }
d8e00cd4a3 2019-09-20 rkeene:
0bdbe4333e 2019-09-20 rkeene: ::xvfs::_emitLine "\};"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: return $outputFiles
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: proc ::xvfs::main {argv} {
47dcf5fc27 2019-05-01 rkeene: # Main entry point
47dcf5fc27 2019-05-01 rkeene: ## 1. Parse arguments
47dcf5fc27 2019-05-01 rkeene: if {[llength $argv] % 2 != 0} {
47dcf5fc27 2019-05-01 rkeene: lappend argv ""
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: foreach {arg val} $argv {
47dcf5fc27 2019-05-01 rkeene: switch -exact -- $arg {
47dcf5fc27 2019-05-01 rkeene: "--help" {
47dcf5fc27 2019-05-01 rkeene: printHelp stdout
47dcf5fc27 2019-05-01 rkeene: exit 0
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "--directory" {
47dcf5fc27 2019-05-01 rkeene: set rootDirectory $val
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: "--name" {
47dcf5fc27 2019-05-01 rkeene: set fsName $val
0bdbe4333e 2019-09-20 rkeene: }
09e53d3c38 2019-09-20 rkeene: "--output" - "--header" {
0bdbe4333e 2019-09-20 rkeene: # Ignored, handled as part of some other process
32b55a907b 2019-05-02 rkeene: }
47dcf5fc27 2019-05-01 rkeene: default {
47dcf5fc27 2019-05-01 rkeene: printHelp stderr [list "Invalid option: $arg $val"]
47dcf5fc27 2019-05-01 rkeene: exit 1
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: ## 2. Validate arguments
47dcf5fc27 2019-05-01 rkeene: set errors [list]
47dcf5fc27 2019-05-01 rkeene: if {![info exists rootDirectory]} {
47dcf5fc27 2019-05-01 rkeene: lappend errors "--directory must be specified"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene: if {![info exists fsName]} {
47dcf5fc27 2019-05-01 rkeene: lappend errors "--name must be specified"
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: if {[llength $errors] != 0} {
47dcf5fc27 2019-05-01 rkeene: printHelp stderr $errors
47dcf5fc27 2019-05-01 rkeene: exit 1
47dcf5fc27 2019-05-01 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: ## 3. Start processing directory and producing initial output
32b55a907b 2019-05-02 rkeene: set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: set ::xvfs::fsName $fsName
47dcf5fc27 2019-05-01 rkeene: set ::xvfs::rootDirectory $rootDirectory
eebfe1f40f 2020-03-25 rkeene:
eebfe1f40f 2020-03-25 rkeene: # Return the output
eebfe1f40f 2020-03-25 rkeene: return [join $::xvfs::_emitLine "\n"]
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
e592c85e70 2019-09-20 rkeene: proc ::xvfs::run {args} {
d36db7c01b 2019-09-20 rkeene: uplevel #0 { package require minirivet }
3cb72a0d20 2019-09-20 rkeene:
e592c85e70 2019-09-20 rkeene: set ::xvfs::argv $args
2b7fa3a8fa 2019-09-20 rkeene: ::minirivet::parse [file join $::xvfs::_xvfsDir xvfs.c.rvt]
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
d36db7c01b 2019-09-20 rkeene: proc ::xvfs::setOutputChannel {channel} {
d36db7c01b 2019-09-20 rkeene: uplevel #0 { package require minirivet }
d36db7c01b 2019-09-20 rkeene: tailcall ::minirivet::setOutputChannel $channel
d36db7c01b 2019-09-20 rkeene: }
d36db7c01b 2019-09-20 rkeene:
d36db7c01b 2019-09-20 rkeene: proc ::xvfs::setOutputVariable {variable} {
d36db7c01b 2019-09-20 rkeene: uplevel #0 { package require minirivet }
d36db7c01b 2019-09-20 rkeene: tailcall ::minirivet::setOutputVariable $variable
09e53d3c38 2019-09-20 rkeene: }
09e53d3c38 2019-09-20 rkeene:
09e53d3c38 2019-09-20 rkeene: proc ::xvfs::staticIncludeHeaderData {headerData} {
09e53d3c38 2019-09-20 rkeene: set ::xvfs::xvfsCoreH $headerData
09e53d3c38 2019-09-20 rkeene: }
09e53d3c38 2019-09-20 rkeene:
09e53d3c38 2019-09-20 rkeene: proc ::xvfs::staticIncludeHeader {pathToHeaderFile} {
09e53d3c38 2019-09-20 rkeene: set fd [open $pathToHeaderFile]
09e53d3c38 2019-09-20 rkeene: ::xvfs::staticIncludeHeaderData [read $fd]
09e53d3c38 2019-09-20 rkeene: close $fd
a719156faf 2019-10-09 rkeene: }
a719156faf 2019-10-09 rkeene:
37d00c3cfb 2019-11-04 rkeene: proc ::xvfs::_tryFit {list} {
37d00c3cfb 2019-11-04 rkeene: set idx -1
37d00c3cfb 2019-11-04 rkeene: set lastItem -100000
37d00c3cfb 2019-11-04 rkeene: foreach item $list {
37d00c3cfb 2019-11-04 rkeene: incr idx
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$item <= $lastItem} {
37d00c3cfb 2019-11-04 rkeene: return ""
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set difference [expr {$item - $idx}]
37d00c3cfb 2019-11-04 rkeene: if {$idx != 0} {
37d00c3cfb 2019-11-04 rkeene: set divisor [expr {$item / $idx}]
37d00c3cfb 2019-11-04 rkeene: } else {
37d00c3cfb 2019-11-04 rkeene: set divisor 1
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: lappend differences $difference
37d00c3cfb 2019-11-04 rkeene: lappend divisors $divisor
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set lastItem $item
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: foreach divisor [lrange $divisors 1 end] {
37d00c3cfb 2019-11-04 rkeene: incr divisorCount
37d00c3cfb 2019-11-04 rkeene: incr divisorValue $divisor
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: set divisor [expr {$divisorValue / $divisorCount}]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: for {set i 0} {$i < [llength $list]} {incr i} {
37d00c3cfb 2019-11-04 rkeene: lappend outList $i
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set mapFunc " - ${difference}"
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set newList [lmap v $list { expr "\$v${mapFunc}" }]
37d00c3cfb 2019-11-04 rkeene: if {$newList eq $outList} {
37d00c3cfb 2019-11-04 rkeene: return $mapFunc
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$divisor != 1} {
37d00c3cfb 2019-11-04 rkeene: set mapFunc " / ${divisor}"
37d00c3cfb 2019-11-04 rkeene: set newList [lmap v $list { expr "\$v${mapFunc}" }]
37d00c3cfb 2019-11-04 rkeene: if {$newList eq $outList} {
37d00c3cfb 2019-11-04 rkeene: return $mapFunc
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set subMapFunc [_tryFit $newList]
37d00c3cfb 2019-11-04 rkeene: if {$subMapFunc != ""} {
37d00c3cfb 2019-11-04 rkeene: return " / ${divisor}${subMapFunc}"
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: return ""
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
f615eecc64 2019-10-10 rkeene: proc ::xvfs::generatePerfectHashFunctionCall {cVarName cVarLength invalidValue nameList args} {
37d00c3cfb 2019-11-04 rkeene: # Manage config
37d00c3cfb 2019-11-04 rkeene: ## Default config
f615eecc64 2019-10-10 rkeene: array set config {
37d00c3cfb 2019-11-04 rkeene: useCacheFirst false
37d00c3cfb 2019-11-04 rkeene: cacheValue true
37d00c3cfb 2019-11-04 rkeene: enableCache false
f615eecc64 2019-10-10 rkeene: }
37d00c3cfb 2019-11-04 rkeene: set config(cacheFile) [file join [file normalize ~/.cache] xvfs phf-cache.db]
f615eecc64 2019-10-10 rkeene:
37d00c3cfb 2019-11-04 rkeene: ## User config
f615eecc64 2019-10-10 rkeene: foreach {configKey configVal} $args {
f615eecc64 2019-10-10 rkeene: if {![info exists config($configKey)]} {
f615eecc64 2019-10-10 rkeene: error "Invalid option: $configKey"
f615eecc64 2019-10-10 rkeene: }
f615eecc64 2019-10-10 rkeene: }
f615eecc64 2019-10-10 rkeene: array set config $args
f615eecc64 2019-10-10 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$config(enableCache)} {
37d00c3cfb 2019-11-04 rkeene: package require sqlite3
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: # Adjustment for computing the expense of a function call by its length
37d00c3cfb 2019-11-04 rkeene: # Calls that take longer should be made longer, so make CRC32 longer
37d00c3cfb 2019-11-04 rkeene: # than Adler32
37d00c3cfb 2019-11-04 rkeene: set lengthAdjustment [list Tcl_ZlibCRC32 Tcl_CRCxxx32]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: # Check for a cached entry
37d00c3cfb 2019-11-04 rkeene: if {$config(enableCache) && $config(useCacheFirst)} {
37d00c3cfb 2019-11-04 rkeene: catch {
37d00c3cfb 2019-11-04 rkeene: set hashKey $nameList
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: sqlite3 ::xvfs::phfCache $config(cacheFile)
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache eval {CREATE TABLE IF NOT EXISTS cache(hashKey PRIMARY KEY, function BLOB);}
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache eval {SELECT function FROM cache WHERE hashKey = $hashKey LIMIT 1;} cacheRow {}
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: catch {
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache close
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {[info exists cacheRow(function)]} {
37d00c3cfb 2019-11-04 rkeene: set phfCall $cacheRow(function)
37d00c3cfb 2019-11-04 rkeene: set phfCall [string map [list @@CVARNAME@@ $cVarName @@CVARLENGTH@@ $cVarLength @@INVALIDVALUE@@ $invalidValue] $phfCall]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: return $phfCall
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set minVal 0
37d00c3cfb 2019-11-04 rkeene: set maxVal [llength $nameList]
37d00c3cfb 2019-11-04 rkeene: set testExpr_(0) {[zlib adler32 $nameItem $alpha] % $gamma}
37d00c3cfb 2019-11-04 rkeene: set testExpr(1) {[zlib crc32 $nameItem $alpha] % $gamma}
37d00c3cfb 2019-11-04 rkeene: set testExpr_(2) {[zlib adler32 $nameItem [zlib crc32 $nameItem $alpha]] % $gamma}
37d00c3cfb 2019-11-04 rkeene: set testExpr_(3) {[zlib crc32 $nameItem [zlib adler32 $nameItem $alpha]] % $gamma}
37d00c3cfb 2019-11-04 rkeene: set testExprC(0) {((Tcl_ZlibAdler32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04 rkeene: set testExprC(1) {((Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04 rkeene: set testExprC(2) {((Tcl_ZlibAdler32(Tcl_ZlibCRC32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@), (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04 rkeene: set testExprC(3) {((Tcl_ZlibCRC32(Tcl_ZlibAdler32(${alpha}LU, (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@), (unsigned char *) @@CVARNAME@@, @@CVARLENGTH@@) % ${gamma}LU)${fitMod})}
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: # Short-circuit for known cases
37d00c3cfb 2019-11-04 rkeene: if {$maxVal == 1} {
37d00c3cfb 2019-11-04 rkeene: return 0
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set round -1
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: while true {
37d00c3cfb 2019-11-04 rkeene: incr round
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set gamma [expr {$maxVal + ($round % ($maxVal * 128))}]
37d00c3cfb 2019-11-04 rkeene: set alpha [expr {$round / 6}]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: foreach {testExprID testExprContents} [array get testExpr] {
37d00c3cfb 2019-11-04 rkeene: set unFitList [list]
37d00c3cfb 2019-11-04 rkeene: foreach nameItem $nameList {
37d00c3cfb 2019-11-04 rkeene: set testExprVal [expr $testExprContents]
37d00c3cfb 2019-11-04 rkeene: lappend unFitList $testExprVal
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set failed false
37d00c3cfb 2019-11-04 rkeene: set fitMod [_tryFit $unFitList]
37d00c3cfb 2019-11-04 rkeene: if {$fitMod eq ""} {
37d00c3cfb 2019-11-04 rkeene: set failed true
f615eecc64 2019-10-10 rkeene: }
f615eecc64 2019-10-10 rkeene:
f615eecc64 2019-10-10 rkeene: if {!$failed} {
f615eecc64 2019-10-10 rkeene: break
f615eecc64 2019-10-10 rkeene: }
a719156faf 2019-10-09 rkeene: }
a719156faf 2019-10-09 rkeene:
a719156faf 2019-10-09 rkeene: if {!$failed} {
a719156faf 2019-10-09 rkeene: break
a719156faf 2019-10-09 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set phfCall [string map [list { - 0LU} ""] [subst $testExprC($testExprID)]]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: # Check cache for a better answer
37d00c3cfb 2019-11-04 rkeene: if {$config(enableCache)} {
37d00c3cfb 2019-11-04 rkeene: catch {
37d00c3cfb 2019-11-04 rkeene: set hashKey $nameList
37d00c3cfb 2019-11-04 rkeene: set cacheDir [file dirname $config(cacheFile)]
37d00c3cfb 2019-11-04 rkeene: file mkdir $cacheDir
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: unset -nocomplain cacheRow
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: sqlite3 ::xvfs::phfCache $config(cacheFile)
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache eval {CREATE TABLE IF NOT EXISTS cache(hashKey PRIMARY KEY, function BLOB);}
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache eval {SELECT function FROM cache WHERE hashKey = $hashKey LIMIT 1;} cacheRow {}
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set updateCache false
37d00c3cfb 2019-11-04 rkeene: if {[info exists cacheRow(function)]} {
37d00c3cfb 2019-11-04 rkeene: if {[string length [string map $lengthAdjustment $cacheRow(function)]] <= [string length [string map $lengthAdjustment $phfCall]]} {
37d00c3cfb 2019-11-04 rkeene: # Use the cached value since it is better
37d00c3cfb 2019-11-04 rkeene: set phfCall $cacheRow(function)
37d00c3cfb 2019-11-04 rkeene: } else {
37d00c3cfb 2019-11-04 rkeene: set updateCache true
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: } else {
37d00c3cfb 2019-11-04 rkeene: set updateCache true
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$updateCache && $config(cacheValue)} {
37d00c3cfb 2019-11-04 rkeene: # Save to cache
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache eval {INSERT OR REPLACE INTO cache (hashKey, function) VALUES ($hashKey, $phfCall);}
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: catch {
37d00c3cfb 2019-11-04 rkeene: ::xvfs::phfCache close
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set phfCall [string map [list @@CVARNAME@@ $cVarName @@CVARLENGTH@@ $cVarLength @@INVALIDVALUE@@ $invalidValue] $phfCall]
a719156faf 2019-10-09 rkeene:
a719156faf 2019-10-09 rkeene: return $phfCall
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: proc ::xvfs::generateHashTable {outCVarName cVarName cVarLength invalidValue nameList args} {
37d00c3cfb 2019-11-04 rkeene: # Manage config
37d00c3cfb 2019-11-04 rkeene: ## Default config
37d00c3cfb 2019-11-04 rkeene: array set config {
37d00c3cfb 2019-11-04 rkeene: prefix ""
37d00c3cfb 2019-11-04 rkeene: hashTableSize 10
37d00c3cfb 2019-11-04 rkeene: validate 0
37d00c3cfb 2019-11-04 rkeene: onValidated "break;"
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: ## User config
37d00c3cfb 2019-11-04 rkeene: foreach {configKey configVal} $args {
37d00c3cfb 2019-11-04 rkeene: if {![info exists config($configKey)]} {
37d00c3cfb 2019-11-04 rkeene: error "Invalid option: $configKey"
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: array set config $args
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {[llength $nameList] < $config(hashTableSize)} {
37d00c3cfb 2019-11-04 rkeene: set config(hashTableSize) [llength $nameList]
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set maxLength 0
37d00c3cfb 2019-11-04 rkeene: set index -1
37d00c3cfb 2019-11-04 rkeene: foreach name $nameList {
37d00c3cfb 2019-11-04 rkeene: incr index
37d00c3cfb 2019-11-04 rkeene: set length [string length $name]
37d00c3cfb 2019-11-04 rkeene: set hash [expr {[zlib adler32 $name 0] % $config(hashTableSize)}]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: lappend indexesAtLength($length) $index
37d00c3cfb 2019-11-04 rkeene: lappend indexesAtHash($hash) $index
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$length > $maxLength} {
37d00c3cfb 2019-11-04 rkeene: set maxLength $length
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: set maxIndexes 0
37d00c3cfb 2019-11-04 rkeene: foreach {hash indexes} [array get indexesAtHash] {
37d00c3cfb 2019-11-04 rkeene: set indexesCount [llength $indexes]
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: if {$indexesCount > $maxIndexes} {
37d00c3cfb 2019-11-04 rkeene: set maxIndexes $indexesCount
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}long ${outCVarName}_idx;"
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}int ${outCVarName}_hash;"
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: for {set hash 0} {$hash < $config(hashTableSize)} {incr hash} {
37d00c3cfb 2019-11-04 rkeene: if {[info exists indexesAtHash($hash)]} {
37d00c3cfb 2019-11-04 rkeene: set indexes $indexesAtHash($hash)
37d00c3cfb 2019-11-04 rkeene: } else {
37d00c3cfb 2019-11-04 rkeene: set indexes [list]
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
3c8c52a9f8 2019-11-04 rkeene: if {[llength $indexes] != $maxIndexes} {
3c8c52a9f8 2019-11-04 rkeene: lappend indexes $invalidValue
3c8c52a9f8 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}static const long ${outCVarName}_hashTable_${hash}\[\] = \{"
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}\t[join $indexes {, }]"
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}\};"
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}static const long * const ${outCVarName}_hashTable\[${config(hashTableSize)}\] = \{"
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: for {set hash 0} {$hash < $config(hashTableSize)} {incr hash} {
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}\t${outCVarName}_hashTable_${hash},"
37d00c3cfb 2019-11-04 rkeene: }
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: lappend outputHeader "${config(prefix)}\};"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}${outCVarName}_hash = Tcl_ZlibAdler32(0, (unsigned char *) ${cVarName}, ${cVarLength}) % ${config(hashTableSize)};"
3c8c52a9f8 2019-11-04 rkeene: lappend outputBody "${config(prefix)}for (${outCVarName}_idx = 0; ${outCVarName}_idx < ${maxIndexes}; ${outCVarName}_idx++) \{"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\t${outCVarName} = ${outCVarName}_hashTable\[${outCVarName}_hash\]\[${outCVarName}_idx\];"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\tif (${outCVarName} == $invalidValue) \{"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\t\tbreak;"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\t\}"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody ""
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\tif (${config(validate)}) \{"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\t\t${config(onValidated)}"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\t\}"
37d00c3cfb 2019-11-04 rkeene: lappend outputBody "${config(prefix)}\}"
37d00c3cfb 2019-11-04 rkeene:
37d00c3cfb 2019-11-04 rkeene: return [dict create header [join $outputHeader "\n"] body [join $outputBody "\n"]]
d36db7c01b 2019-09-20 rkeene: }
47dcf5fc27 2019-05-01 rkeene:
47dcf5fc27 2019-05-01 rkeene: package provide xvfs 1