dba_qdbm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Marcin Gibula <mg@iceni.pl> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if DBA_QDBM
  24. #include "php_qdbm.h"
  25. #ifdef QDBM_INCLUDE_FILE
  26. #include QDBM_INCLUDE_FILE
  27. #endif
  28. #define QDBM_DATA dba_qdbm_data *dba = info->dbf
  29. typedef struct {
  30. DEPOT *dbf;
  31. } dba_qdbm_data;
  32. DBA_OPEN_FUNC(qdbm)
  33. {
  34. DEPOT *dbf;
  35. switch(info->mode) {
  36. case DBA_READER:
  37. dbf = dpopen(info->path, DP_OREADER, 0);
  38. break;
  39. case DBA_WRITER:
  40. dbf = dpopen(info->path, DP_OWRITER, 0);
  41. break;
  42. case DBA_CREAT:
  43. dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
  44. break;
  45. case DBA_TRUNC:
  46. dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
  47. break;
  48. default:
  49. return FAILURE;
  50. }
  51. if (dbf) {
  52. info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
  53. memset(info->dbf, 0, sizeof(dba_qdbm_data));
  54. ((dba_qdbm_data *) info->dbf)->dbf = dbf;
  55. return SUCCESS;
  56. }
  57. *error = (char *) dperrmsg(dpecode);
  58. return FAILURE;
  59. }
  60. DBA_CLOSE_FUNC(qdbm)
  61. {
  62. QDBM_DATA;
  63. dpclose(dba->dbf);
  64. pefree(dba, info->flags & DBA_PERSISTENT);
  65. }
  66. DBA_FETCH_FUNC(qdbm)
  67. {
  68. QDBM_DATA;
  69. char *value, *new = NULL;
  70. int value_size;
  71. value = dpget(dba->dbf, key, keylen, 0, -1, &value_size);
  72. if (value) {
  73. if (newlen) *newlen = value_size;
  74. new = estrndup(value, value_size);
  75. free(value);
  76. }
  77. return new;
  78. }
  79. DBA_UPDATE_FUNC(qdbm)
  80. {
  81. QDBM_DATA;
  82. if (dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER)) {
  83. return SUCCESS;
  84. }
  85. if (dpecode != DP_EKEEP) {
  86. php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", dperrmsg(dpecode));
  87. }
  88. return FAILURE;
  89. }
  90. DBA_EXISTS_FUNC(qdbm)
  91. {
  92. QDBM_DATA;
  93. char *value;
  94. value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
  95. if (value) {
  96. free(value);
  97. return SUCCESS;
  98. }
  99. return FAILURE;
  100. }
  101. DBA_DELETE_FUNC(qdbm)
  102. {
  103. QDBM_DATA;
  104. return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
  105. }
  106. DBA_FIRSTKEY_FUNC(qdbm)
  107. {
  108. QDBM_DATA;
  109. int value_size;
  110. char *value, *new = NULL;
  111. dpiterinit(dba->dbf);
  112. value = dpiternext(dba->dbf, &value_size);
  113. if (value) {
  114. if (newlen) *newlen = value_size;
  115. new = estrndup(value, value_size);
  116. free(value);
  117. }
  118. return new;
  119. }
  120. DBA_NEXTKEY_FUNC(qdbm)
  121. {
  122. QDBM_DATA;
  123. int value_size;
  124. char *value, *new = NULL;
  125. value = dpiternext(dba->dbf, &value_size);
  126. if (value) {
  127. if (newlen) *newlen = value_size;
  128. new = estrndup(value, value_size);
  129. free(value);
  130. }
  131. return new;
  132. }
  133. DBA_OPTIMIZE_FUNC(qdbm)
  134. {
  135. QDBM_DATA;
  136. dpoptimize(dba->dbf, 0);
  137. return SUCCESS;
  138. }
  139. DBA_SYNC_FUNC(qdbm)
  140. {
  141. QDBM_DATA;
  142. dpsync(dba->dbf);
  143. return SUCCESS;
  144. }
  145. DBA_INFO_FUNC(qdbm)
  146. {
  147. return estrdup(dpversion);
  148. }
  149. #endif
  150. /*
  151. * Local variables:
  152. * tab-width: 4
  153. * c-basic-offset: 4
  154. * End:
  155. * vim600: sw=4 ts=4 fdm=marker
  156. * vim<600: sw=4 ts=4
  157. */