PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/dba/dba_inifile.c

http://github.com/php/php-src
C | 181 lines | 129 code | 35 blank | 17 comment | 14 complexity | 275f5ee0e724d51e545e0dfdd3da641f MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Marcus Boerger <helly@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #if DBA_INIFILE
  21. #include "php_inifile.h"
  22. #include "libinifile/inifile.h"
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #define INIFILE_DATA \
  30. inifile *dba = info->dbf
  31. #define INIFILE_GKEY \
  32. key_type ini_key; \
  33. if (!key) { \
  34. php_error_docref(NULL, E_WARNING, "No key specified"); \
  35. return 0; \
  36. } \
  37. ini_key = inifile_key_split((char*)key) /* keylen not needed here */
  38. #define INIFILE_DONE \
  39. inifile_key_free(&ini_key)
  40. DBA_OPEN_FUNC(inifile)
  41. {
  42. info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT);
  43. return info->dbf ? SUCCESS : FAILURE;
  44. }
  45. DBA_CLOSE_FUNC(inifile)
  46. {
  47. INIFILE_DATA;
  48. inifile_free(dba, info->flags&DBA_PERSISTENT);
  49. }
  50. DBA_FETCH_FUNC(inifile)
  51. {
  52. val_type ini_val;
  53. INIFILE_DATA;
  54. INIFILE_GKEY;
  55. ini_val = inifile_fetch(dba, &ini_key, skip);
  56. *newlen = ini_val.value ? strlen(ini_val.value) : 0;
  57. INIFILE_DONE;
  58. return ini_val.value;
  59. }
  60. DBA_UPDATE_FUNC(inifile)
  61. {
  62. val_type ini_val;
  63. int res;
  64. INIFILE_DATA;
  65. INIFILE_GKEY;
  66. ini_val.value = val;
  67. if (mode == 1) {
  68. res = inifile_append(dba, &ini_key, &ini_val);
  69. } else {
  70. res = inifile_replace(dba, &ini_key, &ini_val);
  71. }
  72. INIFILE_DONE;
  73. switch(res) {
  74. case -1:
  75. php_error_docref1(NULL, key, E_WARNING, "Operation not possible");
  76. return FAILURE;
  77. default:
  78. case 0:
  79. return SUCCESS;
  80. case 1:
  81. return FAILURE;
  82. }
  83. }
  84. DBA_EXISTS_FUNC(inifile)
  85. {
  86. val_type ini_val;
  87. INIFILE_DATA;
  88. INIFILE_GKEY;
  89. ini_val = inifile_fetch(dba, &ini_key, 0);
  90. INIFILE_DONE;
  91. if (ini_val.value) {
  92. inifile_val_free(&ini_val);
  93. return SUCCESS;
  94. }
  95. return FAILURE;
  96. }
  97. DBA_DELETE_FUNC(inifile)
  98. {
  99. int res;
  100. zend_bool found = 0;
  101. INIFILE_DATA;
  102. INIFILE_GKEY;
  103. res = inifile_delete_ex(dba, &ini_key, &found);
  104. INIFILE_DONE;
  105. return (res == -1 || !found ? FAILURE : SUCCESS);
  106. }
  107. DBA_FIRSTKEY_FUNC(inifile)
  108. {
  109. INIFILE_DATA;
  110. if (inifile_firstkey(dba)) {
  111. char *result = inifile_key_string(&dba->curr.key);
  112. *newlen = strlen(result);
  113. return result;
  114. } else {
  115. return NULL;
  116. }
  117. }
  118. DBA_NEXTKEY_FUNC(inifile)
  119. {
  120. INIFILE_DATA;
  121. if (!dba->curr.key.group && !dba->curr.key.name) {
  122. return NULL;
  123. }
  124. if (inifile_nextkey(dba)) {
  125. char *result = inifile_key_string(&dba->curr.key);
  126. *newlen = strlen(result);
  127. return result;
  128. } else {
  129. return NULL;
  130. }
  131. }
  132. DBA_OPTIMIZE_FUNC(inifile)
  133. {
  134. /* dummy */
  135. return SUCCESS;
  136. }
  137. DBA_SYNC_FUNC(inifile)
  138. {
  139. /* dummy */
  140. return SUCCESS;
  141. }
  142. DBA_INFO_FUNC(inifile)
  143. {
  144. return estrdup(inifile_version());
  145. }
  146. #endif