home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / SYS_DBM.C < prev    next >
C/C++ Source or Header  |  1996-06-16  |  4KB  |  155 lines

  1. /* Copyright by Denys Duchier, Jul 1995
  2.    Simon Fraser University
  3.  
  4.   DBM INTERFACE
  5.   */
  6. /*    $Id: sys_dbm.c,v 1.4 1996/01/17 00:34:34 duchier Exp $    */
  7.  
  8. #include "extern.h"
  9. #ifndef OS2_PORT
  10. #include "built_ins.h"
  11.  
  12. #include "modules.h"
  13. #include "sys.h"
  14. #include <dbm.h>
  15.  
  16. static long
  17. dbminit_internal(args,result,funct)
  18.      ptr_psi_term args[],result,funct;
  19. {
  20.   if (dbminit((char*)args[0]->value)<0) return FALSE;
  21.   else {
  22.     unify_bool_result(result,TRUE);
  23.     return TRUE;
  24.   }
  25. }
  26.  
  27. static long
  28. c_dbminit()
  29. {
  30.   psi_arg args[1];
  31.   SETARG(args,0,"1",quoted_string,REQUIRED);
  32.   return call_primitive(dbminit_internal,NARGS(args),args,0);
  33. }
  34.  
  35. static long
  36. dbmfetch_internal(args,result,funct)
  37.      ptr_psi_term args[],result,funct;
  38. {
  39.   datum d;
  40.   d.dptr  = (char*)args[0]->value;
  41.   d.dsize = strlen(d.dptr);
  42.   d = fetch(d);
  43.   if (d.dptr == NULL) return FALSE;
  44.   else {
  45.     ptr_psi_term bytes = stack_bytes(d.dptr,d.dsize);
  46.     push_goal(unify,bytes,result,NULL);
  47.     return TRUE;
  48.   }
  49. }
  50.  
  51. static long
  52. c_dbmfetch()
  53. {
  54.   psi_arg args[1];
  55.   SETARG(args,0,"1",quoted_string,REQUIRED);
  56.   return call_primitive(dbmfetch_internal,NARGS(args),args,0);
  57. }
  58.  
  59. static long
  60. dbmstore_internal(args,result,funct)
  61.      ptr_psi_term args[],result,funct;
  62. {
  63.   datum key,content;
  64.   key.dptr  = (char*)args[0]->value;
  65.   key.dsize = strlen(key.dptr);
  66.   content.dptr  = (char*)args[1]->value;
  67.   content.dsize = strlen(content.dptr);
  68.   if (store(key,content)<0) return FALSE;
  69.   else return TRUE;
  70. }
  71.  
  72. static long
  73. c_dbmstore()
  74. {
  75.   psi_arg args[2];
  76.   SETARG(args,0,"1",quoted_string,REQUIRED);
  77.   SETARG(args,1,"2",quoted_string,REQUIRED);
  78.   return call_primitive(dbmstore_internal,NARGS(args),args,0);
  79. }
  80.  
  81. static long
  82. dbmdelete_internal(args,result,funct)
  83.      ptr_psi_term args[],result,funct;
  84. {
  85.   datum key;
  86.   key.dptr  = (char*)args[0]->value;
  87.   key.dsize = strlen(key.dptr);
  88.   if (delete(key)<0) return FALSE;
  89.   else return TRUE;
  90. }
  91.  
  92. static long
  93. c_dbmdelete()
  94. {
  95.   psi_arg args[1];
  96.   SETARG(args,0,"1",quoted_string,REQUIRED);
  97.   return call_primitive(dbmdelete_internal,NARGS(args),args,0);
  98. }
  99.  
  100. static long
  101. dbmfirstkey_internal(args,result,funct)
  102.      ptr_psi_term args[],result,funct;
  103. {
  104.   datum key;
  105.   key=firstkey();
  106.   if (key.dptr==NULL) return FALSE;
  107.   else {
  108.     ptr_psi_term bytes = stack_bytes(key.dptr,key.dsize);
  109.     push_goal(unify,result,bytes,NULL);
  110.     return TRUE;
  111.   }
  112. }
  113.  
  114. static long
  115. c_dbmfirstkey()
  116. {
  117.   return call_primitive(dbmfirstkey_internal,0,NULL,0);
  118. }
  119.  
  120. static long
  121. dbmnextkey_internal(args,result,funct)
  122.      ptr_psi_term args[],result,funct;
  123. {
  124.   datum key;
  125.   key.dptr  = (char*)args[0]->value;
  126.   key.dsize = strlen(key.dptr);
  127.   key = nextkey(key);
  128.   if (key.dptr==NULL) return FALSE;
  129.   else  {
  130.     ptr_psi_term bytes = stack_bytes(key.dptr,key.dsize);
  131.     push_goal(unify,result,bytes,NULL);
  132.     return TRUE;
  133.   }
  134. }
  135.  
  136. static long
  137. c_dbmnextkey()
  138. {
  139.   psi_arg args[1];
  140.   SETARG(args,0,"1",quoted_string,REQUIRED);
  141.   return call_primitive(dbmnextkey_internal,NARGS(args),args,0);
  142. }
  143.  
  144. void
  145. insert_dbm_builtins()
  146. {
  147.   new_built_in(sys_module,"dbm_init",function,c_dbminit);
  148.   new_built_in(sys_module,"dbm_fetch",function,c_dbmfetch);
  149.   new_built_in(sys_module,"dbm_store",predicate,c_dbmstore);
  150.   new_built_in(sys_module,"dbm_delete",predicate,c_dbmdelete);
  151.   new_built_in(sys_module,"dbm_firstkey",function,c_dbmfirstkey);
  152.   new_built_in(sys_module,"dbm_nextkey",function,c_dbmnextkey);
  153. }
  154. #endif
  155.