about summary refs log tree commit diff stats
path: root/src/config/mimetypes.nim
blob: 2133d863cedb4cfa21a614d6f577f0a32799da0d (plain) (blame)
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
import streams
import tables

import utils/twtstr

# extension -> type
type MimeTypes* = Table[string, string]

# Add mime types found in stream to mimeTypes.
# No error handling for now.
proc parseMimeTypes*(mimeTypes: var MimeTypes, stream: Stream) =
  while not stream.atEnd():
    let line = stream.readLine()
    if line.len == 0:
      continue
    if line[0] == '#':
      continue
    var t = ""
    var i = 0
    while i < line.len and line[i] notin AsciiWhitespace:
      t &= line[i].tolower()
      inc i
    if t == "": continue
    while i < line.len:
      while i < line.len and line[i] in AsciiWhitespace:
        inc i
      var ext = ""
      while i < line.len and line[i] notin AsciiWhitespace:
        ext &= line[i].tolower()
        inc i
      if ext == "": continue
      discard mimeTypes.hasKeyOrPut(ext, t)
  stream.close()

proc parseMimeTypes*(stream: Stream): MimeTypes =
  var mimeTypes: MimeTypes
  mimeTypes.parseMimeTypes(stream)
  return mimeTypes