door_nick_lookup_zmq written
[svn42.git] / go / door_nick_lookup_zmq / keys_file.go
diff --git a/go/door_nick_lookup_zmq/keys_file.go b/go/door_nick_lookup_zmq/keys_file.go
new file mode 100644 (file)
index 0000000..4cdb294
--- /dev/null
@@ -0,0 +1,54 @@
+// (c) Bernhard Tittelbach, 2013
+
+package main
+
+import (
+    "log"
+    "regexp"
+    "strconv"
+    "errors"
+    "os"
+    "bufio"
+)
+
+var re_keynickline *regexp.Regexp = regexp.MustCompile("^\\s*([0-9a-fA-F]+)\\s+((?:\\p{Latin}|\\d)+).*")
+
+type KeyNickStore map[uint64]string
+
+func (key_nick_map *KeyNickStore) LoadKeysFile(filename string) error {
+    keysfile, err := os.OpenFile(filename, os.O_RDONLY, 0400) // For read access.
+    defer keysfile.Close()
+    if err != nil {
+        return err
+    }
+    
+    //clear map
+    *key_nick_map = make(KeyNickStore)
+    
+    linescanner := bufio.NewScanner(keysfile)
+    linescanner.Split(bufio.ScanLines)
+    for linescanner.Scan() {
+        m := re_keynickline.FindStringSubmatch(linescanner.Text())
+        if len(m) > 2 {
+            if kuint, err := strconv.ParseUint(m[1], 16, 64); err == nil {
+                (*key_nick_map)[kuint] = m[2]
+            } else {
+                log.Print("Error converting hex-cardid:",m[0])
+            }
+        }
+    }
+    return nil
+}
+
+func (key_nick_map *KeyNickStore) LookupHexKeyNick( key string ) (string, error) {
+    kuint, err := strconv.ParseUint(key, 16, 64)
+    if err != nil {
+        return "", errors.New("Invalid Hex-Card-Id")
+    }
+    nick, present := (*key_nick_map)[kuint]
+    if present {
+        return nick, nil
+    } else {
+        return "", errors.New("Key Unknown")
+    }
+}
\ No newline at end of file