;;; ;;; odbm.stub - original dbm interface ;;; ;;; Copyright (c) 2000-2007 Shiro Kawai ;;; ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; ;;; 1. Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; ;;; 2. Redistributions in binary form must reproduce the above copyright ;;; notice, this list of conditions and the following disclaimer in the ;;; documentation and/or other materials provided with the distribution. ;;; ;;; 3. Neither the name of the authors nor the names of its contributors ;;; may be used to endorse or promote products derived from this ;;; software without specific prior written permission. ;;; ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;;; ;;; $Id: odbm.stub,v 1.18 2007/03/22 11:20:18 shirok Exp $ ;;; " #include #include \"dbmconf.h\" #if HAVE_DBM_H #include #elif HAVE_GDBM_SLASH_DBM_H #include #elif HAVE_GDBM_MINUS_DBM_H #include #endif #define CHECK_ODBM() if (!odbm_opened) Scm_Error(\"odbm file already closed\") /* Hack for initialization stub */ static void Scm_odbm_internal_init(ScmModule*); void Scm_Init_odbm(void) { SCM_INIT_EXTENSION(odbm); Scm_odbm_internal_init(SCM_FIND_MODULE(\"dbm.odbm\", SCM_FIND_MODULE_CREATE)); } #define Scm_Init_odbm Scm_odbm_internal_init #define TO_DATUM(datum_, scm_) \\ do { \\ const ScmStringBody *b = SCM_STRING_BODY(scm_);\\ datum_.dptr = (char*)SCM_STRING_BODY_START(b);\\ datum_.dsize = SCM_STRING_BODY_SIZE(b); \\ } while (0) #define FROM_DATUM(scm_, datum_) \\ do { \\ if (datum_.dptr) { \\ scm_ = Scm_MakeString(datum_.dptr, datum_.dsize, -1, SCM_STRING_COPYING); \\ } else { \\ scm_ = SCM_FALSE; \\ } \\ } while (0) " ;; Original dbm allows to open only one file at a time. ;; The static variable odbm_opened tracks the status. ;; TODO: MT SAFENESS "static int odbm_opened = FALSE;" (define-cproc odbm-init (name::) (body "if (odbm_opened) Scm_Error(\"dbm file is already opened.\");" "SCM_RESULT = dbminit(Scm_GetString(name));" "if (SCM_RESULT < 0) {" " Scm_SysError(\"couldn't open dbm database %S\", name);" "}" "odbm_opened = TRUE;")) (define-cproc odbm-close () (body "if (odbm_opened) {" " dbmclose();" " odbm_opened = FALSE;" "}")) (define-cproc odbm-closed? () (expr "!odbm_opened")) (define-cproc odbm-store (key:: val::) (body "datum dkey, dval;" "CHECK_ODBM();" "TO_DATUM(dkey, key);" "TO_DATUM(dval, val);" "SCM_RESULT = store(dkey, dval);")) (define-cproc odbm-fetch (key::) (body "datum dkey, dval;" "CHECK_ODBM();" "TO_DATUM(dkey, key);" "dval = fetch(dkey);" "FROM_DATUM(SCM_RESULT, dval);")) (define-cproc odbm-delete (key::) (body "datum dkey;" "CHECK_ODBM();" "TO_DATUM(dkey, key);" "SCM_RESULT = delete(dkey);")) (define-cproc odbm-firstkey () (body "datum dkey;" "CHECK_ODBM();" "dkey = firstkey();" "FROM_DATUM(SCM_RESULT, dkey);")) (define-cproc odbm-nextkey (key::) (body "datum dkey, dnkey;" "CHECK_ODBM();" "TO_DATUM(dkey, key);" "dnkey = nextkey(dkey);" "FROM_DATUM(SCM_RESULT, dnkey);")) ;; Local variables: ;; mode: scheme ;; end: