Monday, January 26, 2009

UTF-8 strings in trivial-ldap field values

Everybody using LDAP and Common Lisp probably knows the trivial-ldap library. It's a nifty, simple and concise implementation of the LDAP protocol in pure Common Lisp - i.e. no foreign library is needed to access your directory using this package. Trivial-ldap documentation states that UTF-8 strings are not supported, although the non-ascii string attribute value is properly passed in trivial-ldap:entry slots. Here is how you can easily get UTF-8 string attribute in Allegro CL:
(defun ldap-utf-8 (user pass base filter attribute)
  (let ((ldap (trivial-ldap:new-ldap :user user
                                     :pass pass
                                     :base base)))
    (trivial-ldap:bind ldap)
    (unwind-protect
         (when (trivial-ldap:search ldap filter)
           (loop for res = (trivial-ldap:next-search-result ldap)
                 while res
                 do (return-from ldap-utf-8
                      (octets-to-string
                       (string-to-octets
                        (car (trivial-ldap:attr-value res attribute))
                        :external-format :8-bit)
                       :external-format :utf8))))
      (trivial-ldap:unbind ldap))))
The arguments are self explanatory.

No comments:

Post a Comment