socsvn commit: r272610 - soc2014/ghostmansd/head/lib/libcolldb

ghostmansd at FreeBSD.org ghostmansd at FreeBSD.org
Mon Aug 18 14:38:54 UTC 2014


Author: ghostmansd
Date: Mon Aug 18 14:38:52 2014
New Revision: 272610
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=272610

Log:
  writing colldb man page; modifying Makefile

Added:
  soc2014/ghostmansd/head/lib/libcolldb/colldb.py
     - copied unchanged from r272540, soc2014/ghostmansd/head/lib/libcolldb/colldb
Deleted:
  soc2014/ghostmansd/head/lib/libcolldb/colldb
Modified:
  soc2014/ghostmansd/head/lib/libcolldb/Makefile
  soc2014/ghostmansd/head/lib/libcolldb/colldb.h

Modified: soc2014/ghostmansd/head/lib/libcolldb/Makefile
==============================================================================
--- soc2014/ghostmansd/head/lib/libcolldb/Makefile	Mon Aug 18 14:24:58 2014	(r272609)
+++ soc2014/ghostmansd/head/lib/libcolldb/Makefile	Mon Aug 18 14:38:52 2014	(r272610)
@@ -2,9 +2,13 @@
 LIB=	colldb
 SRCS=	colldb.c
 INCS=	colldb.h
+MAN+=	colldb.3
 
 CFLAGS+= -I${COLLDB}
 
+.colldb.py
+	install -m644 /usr/share/examples/collation
+
 WARNS?=	3
 
 .include <bsd.lib.mk>

Modified: soc2014/ghostmansd/head/lib/libcolldb/colldb.h
==============================================================================
--- soc2014/ghostmansd/head/lib/libcolldb/colldb.h	Mon Aug 18 14:24:58 2014	(r272609)
+++ soc2014/ghostmansd/head/lib/libcolldb/colldb.h	Mon Aug 18 14:38:52 2014	(r272610)
@@ -35,7 +35,7 @@
  * between host and network byte order is done implicitly.
  * Each database must contain at least "TYPE" and "VERSION" keys, where
  * the first one must always be "COLLATION", the last one must be a version
- * as unsigned 32-bit integer. When use creates a new collation database,
+ * as unsigned 32-bit integer. When user creates a new collation database,
  * these keys are initialized automatically.
  *
  * colldb_put() accepts database, key and value. If necessary, it converts

Copied: soc2014/ghostmansd/head/lib/libcolldb/colldb.py (from r272540, soc2014/ghostmansd/head/lib/libcolldb/colldb)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2014/ghostmansd/head/lib/libcolldb/colldb.py	Mon Aug 18 14:38:52 2014	(r272610, copy of r272540, soc2014/ghostmansd/head/lib/libcolldb/colldb)
@@ -0,0 +1,220 @@
+#!/usr/bin/env python
+# *-* coding: utf-8 *-*
+"""Python bindings for libcolldb"""
+
+import codecs
+import ctypes
+import os
+import re
+import sys
+
+
+__author__ = "Dmitry Selyutin"
+__copyright__ = "Copyright 2007"
+__license__ = "BSD 2-Clause license"
+__maintainer__ = "Dmitry Selyutin"
+
+
+_dll_ = ctypes.CDLL("libcolldb.so")
+
+
+class _CollDB_Weight_(ctypes.Structure):
+	"""struct colldb_weight {
+		uint8_t alternate;
+		uint32_t level1;
+		uint32_t level2;
+		uint32_t level3;
+		uint32_t level4;
+	};"""
+	_fields_ = [
+		("alternate",	ctypes.c_uint8),
+		("level1",	ctypes.c_uint32),
+		("level2",	ctypes.c_uint32),
+		("level3",	ctypes.c_uint32),
+		("level4",	ctypes.c_uint32),
+	]
+
+
+class _CollDB_Key_(ctypes.Structure):
+	"""struct colldb_key {
+		uint8_t count;
+		uint32_t *chars;
+	};"""
+	_fields_ = [
+		("count",	ctypes.c_uint8),
+		("chars",	ctypes.POINTER(ctypes.c_uint32)),
+	]
+
+
+class _CollDB_Value_(ctypes.Structure):
+	"""struct colldb_value {
+		uint8_t count;
+		struct colldb_weight *weights;
+	};"""
+	_fields_ = [
+		("count",	ctypes.c_uint8),
+		("weights",	ctypes.POINTER(_CollDB_Weight_)),
+	]
+
+
+_create_ = _dll_.colldb_create
+_create_.argtypes = [ctypes.c_char_p, ctypes.c_int]
+_create_.restype = ctypes.c_void_p
+
+_open_ = _dll_.colldb_open
+_open_.argtypes = [ctypes.c_char_p]
+_open_.restype = ctypes.c_void_p
+
+_sync_ = _dll_.colldb_sync
+_sync_.argtypes = [ctypes.c_void_p]
+_sync_.restype = ctypes.c_int
+
+_close_ = _dll_.colldb_close
+_close_.argtypes = [ctypes.c_void_p]
+_close_.restype = ctypes.c_int
+
+_get_ = _dll_.colldb_get
+_get_.argtypes = [
+	ctypes.c_void_p,
+	ctypes.POINTER(_CollDB_Key_),
+	ctypes.POINTER(_CollDB_Value_),
+]
+_get_.restype = ctypes.c_int
+
+_put_ = _dll_.colldb_put
+_put_.argtypes = [
+	ctypes.c_void_p,
+	ctypes.POINTER(_CollDB_Key_),
+	ctypes.POINTER(_CollDB_Value_),
+]
+_put_.restype = ctypes.c_int
+
+
+class CollDB(object):
+	"""CollDB object manages collation databases using C API."""
+
+	def __init__(self, path, mode):
+		"""CollDB.__init__(path, mode) -> Collation"""
+		path = path.encode("UTF-8")
+		if mode == "w":
+			self._db_ = _create_(path, 511)
+		elif mode == "r":
+			self._db_ = _open_(path)
+		else:
+			raise ValueError("mode must be either 'r' or 'w'")
+		if not self._db_:
+			raise OSError(os.strerror(ctypes.get_errno()))
+
+	def __enter__(self):
+		return self
+
+	def __exit__(self, exc_type, exc_val, exc_tb):
+		self.close()
+
+	def close(self):
+		state = _close_(self._db_)
+		if state != 0:
+			raise OSError(os.strerror(ctypes.get_errno()))
+
+	def get(self, key):
+		"""Get the value which corresponds to the given key."""
+		ckey = _CollDB_Key_()
+		cvalue = _CollDB_Value_()
+		weights = ([_CollDB_Weight_()] * 16)
+		ckey.count = len(key)
+		cvalue.count = len(weights)
+		ckey.chars = (ctypes.c_uint32 * len(key))(*key)
+		cvalue.weights = (_CollDB_Weight_ * len(weights))(*weights)
+		state = _get_(
+			self._db_,
+			ctypes.byref(ckey),
+			ctypes.byref(cvalue)
+		)
+		if state == -1:
+			raise OSError(os.strerror(ctypes.get_errno()))
+		elif state == +1:
+			return KeyError("".join([chr(i) for i in key]))
+		value = []
+		for i in range(cvalue.count):
+			alt = cvalue.weights[i][0]
+			level1 = cvalue.weights[i][1]
+			level2 = cvalue.weights[i][2]
+			level3 = cvalue.weights[i][3]
+			level4 = cvalue.weights[i][4]
+			value.append([alt, level1, level2, level3, level4])
+		return value
+
+	def put(self, key, value):
+		"""Associate the given key with the given value."""
+		weights = []
+		ckey = _CollDB_Key_()
+		cvalue = _CollDB_Value_()
+		ckey.count = len(key)
+		cvalue.count = len(value)
+		for i in range(len(value)):
+			weight = _CollDB_Weight_()
+			weight.alternate = value[i][0]
+			weight.level1 = value[i][1]
+			weight.level2 = value[i][2]
+			weight.level3 = value[i][3]
+			weight.level4 = value[i][4]
+			weights.append(weight)
+		ckey.chars = (ctypes.c_uint32 * len(key))(*key)
+		cvalue.weights = (_CollDB_Weight_ * len(value))(*weights)
+		state = _put_(
+			self._db_,
+			ctypes.byref(ckey),
+			ctypes.byref(cvalue)
+		)
+		if state != 0:
+			raise OSError(os.strerror(ctypes.get_errno()))
+
+
+pattern = re.compile(r"""
+    \[
+    (\*|\.)
+    ([0-9A-Fa-f]{4,8})
+    \.
+    ([0-9A-Fa-f]{4,8})
+    \.
+    ([0-9A-Fa-f]{4,8})
+    (?:\.([0-9A-Fa-f]{4,8}))?
+\]
+""", re.X)
+
+
+def colldb(src, dst):
+	collation = CollDB(dst, "w")
+	stream = codecs.open(src, "rb", "UTF-8")
+	for line in stream:
+		line = line.strip()
+		if not line or line.startswith("#") or line.startswith("@"):
+			continue # skip blank lines and comments
+		sequence, elements = line.split(";")
+		sequence = sequence.strip()
+		elements = elements.strip()
+		sequence = sequence.split(" ")
+		if "#" in elements:
+			elements = elements[:elements.find("#")]
+		sequence = [int(i.strip(), 16) for i in sequence if i.strip()]
+		elements = [list(i.groups()) for i in pattern.finditer(elements)]
+		elements = [
+			[
+				int(i[0] == "*"),
+				int(i[1], 16),
+				int(i[2], 16),
+				int(i[3], 16),
+				int(i[4], 16),
+			] for i in elements
+		]
+		collation.put(sequence, elements)
+	collation.close()
+	stream.close()
+
+
+if __name__ == "__main__":
+	if len(sys.argv) != 3:
+		sys.stderr.write("usage: colldb <src> <dst>\n")
+		exit(-1)
+	colldb(sys.argv[1], sys.argv[2])
+	exit(0)


More information about the svn-soc-all mailing list