@@ -1,9 +1,18 @@ #! /usr/bin/env tclsh +if {[llength $argv] != 1} { + puts stderr "Usage: generate-mime-type " + + exit 1 +} + +set mimeinfofile [lindex $argv 0] + set modulus 16777259 +# Must match what is in filed.c proc filed_hash {str mod} { set retval [expr {$mod - 1}] set prev [expr {$mod % 255}] for {set idx 0} {$idx < [string length $str]} {incr idx} { set curr [string index $str $idx] @@ -32,16 +41,16 @@ return $retval } -set mimeinfofile "/etc/httpd/mime.types" - +# Read contents of mime types file set fd [open $mimeinfofile] set mimeinfo [read $fd] close $fd +# Parse into type and extensions pairs foreach line [split $mimeinfo "\n"] { regsub {#.*} $line {} line set line [string trim $line] if {$line == ""} { @@ -60,16 +69,20 @@ set extensioninfo($extension) $mime } } +# For every extension, generate a hash +# For every hash, note the extension foreach extension [array names extensioninfo] { set hash_id [filed_hash $extension $modulus] lappend hashinfo($hash_id) $extension } + +# Emit a C fragment to take a pointer (p) to an extension determine the mime type puts "\tswitch (filed_hash((const unsigned char *) p, $modulus)) \{" foreach hash [lsort -integer -increasing [array names hashinfo]] { puts "\t\tcase $hash:" foreach extension $hashinfo($hash) { puts "\t\t\tif (strcmp(p, \"$extension\") == 0) \{" @@ -77,5 +90,8 @@ puts "\t\t\t\}" } puts "\t\t\treturn(FILED_DEFAULT_TYPE);" } puts "\t\}" + +# Declare victory +exit 0