dba_qdbm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if DBA_QDBM
  23. #include "php_qdbm.h"
  24. #ifdef QDBM_INCLUDE_FILE
  25. #include QDBM_INCLUDE_FILE
  26. #endif
  27. #define QDBM_DATA dba_qdbm_data *dba = info->dbf
  28. typedef struct {
  29. DEPOT *dbf;
  30. } dba_qdbm_data;
  31. DBA_OPEN_FUNC(qdbm)
  32. {
  33. DEPOT *dbf;
  34. switch(info->mode) {
  35. case DBA_READER:
  36. dbf = dpopen(info->path, DP_OREADER, 0);
  37. break;
  38. case DBA_WRITER:
  39. dbf = dpopen(info->path, DP_OWRITER, 0);
  40. break;
  41. case DBA_CREAT:
  42. dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
  43. break;
  44. case DBA_TRUNC:
  45. dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
  46. break;
  47. default:
  48. return FAILURE;
  49. }
  50. if (dbf) {
  51. info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
  52. memset(info->dbf, 0, sizeof(dba_qdbm_data));
  53. ((dba_qdbm_data *) info->dbf)->dbf = dbf;
  54. return SUCCESS;
  55. }
  56. *error = (char *) dperrmsg(dpecode);
  57. return FAILURE;
  58. }
  59. DBA_CLOSE_FUNC(qdbm)
  60. {
  61. QDBM_DATA;
  62. dpclose(dba->dbf);
  63. pefree(dba, info->flags & DBA_PERSISTENT);
  64. }
  65. DBA_FETCH_FUNC(qdbm)
  66. {
  67. QDBM_DATA;
  68. char *value, *new = NULL;
  69. int value_size;
  70. value = dpget(dba->dbf, key, keylen, 0, -1, &value_size);
  71. if (value) {
  72. if (newlen) *newlen = value_size;
  73. new = estrndup(value, value_size);
  74. free(value);
  75. }
  76. return new;
  77. }
  78. DBA_UPDATE_FUNC(qdbm)
  79. {
  80. QDBM_DATA;
  81. if (dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER)) {
  82. return SUCCESS;
  83. }
  84. if (dpecode != DP_EKEEP) {
  85. php_error_docref2(NULL, key, val, E_WARNING, "%s", dperrmsg(dpecode));
  86. }
  87. return FAILURE;
  88. }
  89. DBA_EXISTS_FUNC(qdbm)
  90. {
  91. QDBM_DATA;
  92. char *value;
  93. value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
  94. if (value) {
  95. free(value);
  96. return SUCCESS;
  97. }
  98. return FAILURE;
  99. }
  100. DBA_DELETE_FUNC(qdbm)
  101. {
  102. QDBM_DATA;
  103. return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
  104. }
  105. DBA_FIRSTKEY_FUNC(qdbm)
  106. {
  107. QDBM_DATA;
  108. int value_size;
  109. char *value, *new = NULL;
  110. dpiterinit(dba->dbf);
  111. value = dpiternext(dba->dbf, &value_size);
  112. if (value) {
  113. if (newlen) *newlen = value_size;
  114. new = estrndup(value, value_size);
  115. free(value);
  116. }
  117. return new;
  118. }
  119. DBA_NEXTKEY_FUNC(qdbm)
  120. {
  121. QDBM_DATA;
  122. int value_size;
  123. char *value, *new = NULL;
  124. value = dpiternext(dba->dbf, &value_size);
  125. if (value) {
  126. if (newlen) *newlen = value_size;
  127. new = estrndup(value, value_size);
  128. free(value);
  129. }
  130. return new;
  131. }
  132. DBA_OPTIMIZE_FUNC(qdbm)
  133. {
  134. QDBM_DATA;
  135. dpoptimize(dba->dbf, 0);
  136. return SUCCESS;
  137. }
  138. DBA_SYNC_FUNC(qdbm)
  139. {
  140. QDBM_DATA;
  141. dpsync(dba->dbf);
  142. return SUCCESS;
  143. }
  144. DBA_INFO_FUNC(qdbm)
  145. {
  146. return estrdup(dpversion);
  147. }
  148. #endif
  149. /*
  150. * Local variables:
  151. * tab-width: 4
  152. * c-basic-offset: 4
  153. * End:
  154. * vim600: sw=4 ts=4 fdm=marker
  155. * vim<600: sw=4 ts=4
  156. */