Shop OBEX P1 Docs P2 Docs Learn Events
File extension lookup table - advice needed — Parallax Forums

File extension lookup table - advice needed

Mike GMike G Posts: 2,702
edited 2011-02-17 19:11 in Propeller 1
This is for a Spinneret web server project. The client requests a file resource via HTTP. It could be .htm, gif, whatever. In the response, I send back the proper content-type header. Currently, the logic is wrapped in a bunch of SPIN < if string1 == string2 …elseif…> logic blocks. I want to replace the IF blocks.

I started looking at a hash table but I would need 26^3 rows for 3 lowercase alpha characters. I only need around 30ish rows. I would like to do the same kind of look-up table method for HTTP response codes like “200 OK”.

If someone can point me in the right direction, that would be much appreciated.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-16 12:40
    Are all extensions 3 letters? Then simply treat em like longs (4 bytes). Have a look at the SPIN instructions lookup/lookdown. You can use one of them (don't remember which one) to give a list of longs and it gives back the index of your current value inside of the list.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-02-16 12:45
    If the requested filenames all derive from links in your webpages, you do not have to use standard file extensions. You could simply use .A, .B, .C, etc., if you like. That way the extension itself (after subtracting an offset) can index into a list of string addresses, each pointing to a different content-type.

    -Phil
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-16 13:01
    Here is what I mean:
    con
      HTML = "H"<<16+"T"<<8+"M"
      GIF  = "G"<<16+"I"<<8+"F"
      
    pub testit | ext,idx
    
      ' this should copy the extension from wherever you have it in your code into a known long
      byte[@ext+3]:=0
      byte[@ext+2]:="H"
      byte[@ext+1]:="T"
      byte[@ext+0]:="M"
    
      ' here you will then find either an index of 1 to 2 in this case
      ' or a 0 if the extension is not found in the list
      idx:=lookdown( ext: HTML, GIF )
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-16 13:38
    After the lookdown you can do a lookup to get the right content-string:
      tmp:=lookup( idx: string("text/html"), string("media/gif",13) )
    
  • Mike GMike G Posts: 2,702
    edited 2011-02-17 19:11
    After some thinking, I came up with the attached hashing scheme. A little re-factoring is in order but it works.

    Thanks for your input!
Sign In or Register to comment.