dba_flatfile.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if DBA_FLATFILE
  24. #include "php_flatfile.h"
  25. #include "libflatfile/flatfile.h"
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #define FLATFILE_DATA flatfile *dba = info->dbf
  33. #define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
  34. DBA_OPEN_FUNC(flatfile)
  35. {
  36. info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
  37. memset(info->dbf, 0, sizeof(flatfile));
  38. ((flatfile*)info->dbf)->fp = info->fp;
  39. return SUCCESS;
  40. }
  41. DBA_CLOSE_FUNC(flatfile)
  42. {
  43. FLATFILE_DATA;
  44. if (dba->nextkey.dptr) {
  45. efree(dba->nextkey.dptr);
  46. }
  47. pefree(dba, info->flags&DBA_PERSISTENT);
  48. }
  49. DBA_FETCH_FUNC(flatfile)
  50. {
  51. datum gval;
  52. char *new = NULL;
  53. FLATFILE_DATA;
  54. FLATFILE_GKEY;
  55. gval = flatfile_fetch(dba, gkey TSRMLS_CC);
  56. if (gval.dptr) {
  57. if (newlen) {
  58. *newlen = gval.dsize;
  59. }
  60. new = estrndup(gval.dptr, gval.dsize);
  61. efree(gval.dptr);
  62. }
  63. return new;
  64. }
  65. DBA_UPDATE_FUNC(flatfile)
  66. {
  67. datum gval;
  68. FLATFILE_DATA;
  69. FLATFILE_GKEY;
  70. gval.dptr = (char *) val;
  71. gval.dsize = vallen;
  72. switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE TSRMLS_CC)) {
  73. case 0:
  74. return SUCCESS;
  75. case 1:
  76. return FAILURE;
  77. case -1:
  78. php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
  79. return FAILURE;
  80. default:
  81. php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "Unknown return value");
  82. return FAILURE;
  83. }
  84. }
  85. DBA_EXISTS_FUNC(flatfile)
  86. {
  87. datum gval;
  88. FLATFILE_DATA;
  89. FLATFILE_GKEY;
  90. gval = flatfile_fetch(dba, gkey TSRMLS_CC);
  91. if (gval.dptr) {
  92. efree(gval.dptr);
  93. return SUCCESS;
  94. }
  95. return FAILURE;
  96. }
  97. DBA_DELETE_FUNC(flatfile)
  98. {
  99. FLATFILE_DATA;
  100. FLATFILE_GKEY;
  101. return(flatfile_delete(dba, gkey TSRMLS_CC) == -1 ? FAILURE : SUCCESS);
  102. }
  103. DBA_FIRSTKEY_FUNC(flatfile)
  104. {
  105. FLATFILE_DATA;
  106. if (dba->nextkey.dptr) {
  107. efree(dba->nextkey.dptr);
  108. }
  109. dba->nextkey = flatfile_firstkey(dba TSRMLS_CC);
  110. if (dba->nextkey.dptr) {
  111. if (newlen) {
  112. *newlen = dba->nextkey.dsize;
  113. }
  114. return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
  115. }
  116. return NULL;
  117. }
  118. DBA_NEXTKEY_FUNC(flatfile)
  119. {
  120. FLATFILE_DATA;
  121. if (!dba->nextkey.dptr) {
  122. return NULL;
  123. }
  124. if (dba->nextkey.dptr) {
  125. efree(dba->nextkey.dptr);
  126. }
  127. dba->nextkey = flatfile_nextkey(dba TSRMLS_CC);
  128. if (dba->nextkey.dptr) {
  129. if (newlen) {
  130. *newlen = dba->nextkey.dsize;
  131. }
  132. return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
  133. }
  134. return NULL;
  135. }
  136. DBA_OPTIMIZE_FUNC(flatfile)
  137. {
  138. /* dummy */
  139. return SUCCESS;
  140. }
  141. DBA_SYNC_FUNC(flatfile)
  142. {
  143. /* dummy */
  144. return SUCCESS;
  145. }
  146. DBA_INFO_FUNC(flatfile)
  147. {
  148. return estrdup(flatfile_version());
  149. }
  150. #endif
  151. /*
  152. * Local variables:
  153. * tab-width: 4
  154. * c-basic-offset: 4
  155. * End:
  156. * vim600: sw=4 ts=4 fdm=marker
  157. * vim<600: sw=4 ts=4
  158. */