PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/dba - dba_gdbm.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 69 88 78.4 %
Date: 2014-12-13 Functions: 9 11 81.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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: Sascha Schumann <sascha@schumann.cx>                         |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : 
      25             : #include "php.h"
      26             : 
      27             : #if DBA_GDBM
      28             : #include "php_gdbm.h"
      29             : 
      30             : #ifdef GDBM_INCLUDE_FILE
      31             : #include GDBM_INCLUDE_FILE
      32             : #endif
      33             : 
      34             : #define GDBM_DATA dba_gdbm_data *dba = info->dbf
      35             : #define GDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
      36             : 
      37             : typedef struct {
      38             :         GDBM_FILE dbf;
      39             :         datum nextkey;
      40             : } dba_gdbm_data;
      41             : 
      42           5 : DBA_OPEN_FUNC(gdbm)
      43             : {
      44             :         GDBM_FILE dbf;
      45           5 :         int gmode = 0;
      46           5 :         int filemode = 0644;
      47             : 
      48           6 :         gmode = info->mode == DBA_READER ? GDBM_READER :
      49             :                 info->mode == DBA_WRITER ? GDBM_WRITER : 
      50             :                 info->mode == DBA_CREAT  ? GDBM_WRCREAT : 
      51           1 :                 info->mode == DBA_TRUNC ? GDBM_NEWDB : -1;
      52             :                 
      53           5 :         if(gmode == -1) 
      54           0 :                 return FAILURE; /* not possible */
      55             : 
      56           5 :         if(info->argc > 0) {
      57           0 :                 convert_to_long_ex(&info->argv[0]);
      58           0 :                 filemode = Z_LVAL(info->argv[0]);
      59             :         }
      60             : 
      61           5 :         dbf = gdbm_open(info->path, 0, gmode, filemode, NULL);
      62             :         
      63           5 :         if(dbf) {
      64          10 :                 info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);
      65           5 :                 memset(info->dbf, 0, sizeof(dba_gdbm_data));
      66           5 :                 ((dba_gdbm_data *) info->dbf)->dbf = dbf;
      67           5 :                 return SUCCESS;
      68             :         }
      69           0 :         *error = gdbm_strerror(gdbm_errno);
      70           0 :         return FAILURE;
      71             : }
      72             : 
      73           5 : DBA_CLOSE_FUNC(gdbm)
      74             : {
      75           5 :         GDBM_DATA;
      76             :         
      77           5 :         if(dba->nextkey.dptr) free(dba->nextkey.dptr);
      78           5 :         gdbm_close(dba->dbf);
      79           5 :         pefree(dba, info->flags&DBA_PERSISTENT);
      80           5 : }
      81             : 
      82           7 : DBA_FETCH_FUNC(gdbm)
      83             : {
      84           7 :         GDBM_DATA;
      85             :         datum gval;
      86           7 :         char *new = NULL;
      87             : 
      88           7 :         GDBM_GKEY;
      89           7 :         gval = gdbm_fetch(dba->dbf, gkey);
      90           7 :         if(gval.dptr) {
      91           7 :                 if(newlen) *newlen = gval.dsize;
      92           7 :                 new = estrndup(gval.dptr, gval.dsize);
      93           7 :                 free(gval.dptr);
      94             :         }
      95           7 :         return new;
      96             : }
      97             : 
      98           9 : DBA_UPDATE_FUNC(gdbm)
      99             : {
     100             :         datum gval;
     101           9 :         GDBM_DATA;
     102             : 
     103           9 :         GDBM_GKEY;
     104           9 :         gval.dptr = (char *) val;
     105           9 :         gval.dsize = vallen;
     106             : 
     107           9 :         switch (gdbm_store(dba->dbf, gkey, gval, mode == 1 ? GDBM_INSERT : GDBM_REPLACE)) {
     108             :                 case 0:
     109           8 :                         return SUCCESS;
     110             :                 case 1:
     111           1 :                         return FAILURE;
     112             :                 case -1:
     113           0 :                         php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", gdbm_strerror(gdbm_errno));
     114           0 :                         return FAILURE;
     115             :                 default:
     116           0 :                         php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "Unknown return value");
     117           0 :                         return FAILURE;
     118             :         }
     119             : }
     120             : 
     121           5 : DBA_EXISTS_FUNC(gdbm)
     122             : {
     123           5 :         GDBM_DATA;
     124           5 :         GDBM_GKEY;
     125             : 
     126           5 :         return gdbm_exists(dba->dbf, gkey) ? SUCCESS : FAILURE;
     127             : }
     128             : 
     129           3 : DBA_DELETE_FUNC(gdbm)
     130             : {
     131           3 :         GDBM_DATA;
     132           3 :         GDBM_GKEY;
     133             :         
     134           3 :         return gdbm_delete(dba->dbf, gkey) == -1 ? FAILURE : SUCCESS;
     135             : }
     136             : 
     137           2 : DBA_FIRSTKEY_FUNC(gdbm)
     138             : {
     139           2 :         GDBM_DATA;
     140             :         datum gkey;
     141           2 :         char *key = NULL;
     142             : 
     143           2 :         if(dba->nextkey.dptr) {
     144           0 :                 free(dba->nextkey.dptr);
     145             :         }
     146             :         
     147           2 :         gkey = gdbm_firstkey(dba->dbf);
     148           2 :         if(gkey.dptr) {
     149           2 :                 key = estrndup(gkey.dptr, gkey.dsize);
     150           2 :                 if(newlen) *newlen = gkey.dsize;
     151           2 :                 dba->nextkey = gkey;
     152             :         } else {
     153           0 :                 dba->nextkey.dptr = NULL;
     154             :         }
     155           2 :         return key;
     156             : }
     157             : 
     158           6 : DBA_NEXTKEY_FUNC(gdbm)
     159             : {
     160           6 :         GDBM_DATA;
     161           6 :         char *nkey = NULL;
     162             :         datum gkey;
     163             : 
     164           6 :         if(!dba->nextkey.dptr) return NULL;
     165             :         
     166           6 :         gkey = gdbm_nextkey(dba->dbf, dba->nextkey);
     167           6 :         free(dba->nextkey.dptr);
     168           6 :         if(gkey.dptr) {
     169           4 :                 nkey = estrndup(gkey.dptr, gkey.dsize);
     170           4 :                 if(newlen) *newlen = gkey.dsize;
     171           4 :                 dba->nextkey = gkey;
     172             :         } else {
     173           2 :                 dba->nextkey.dptr = NULL;
     174             :         }
     175           6 :         return nkey;
     176             : }
     177             : 
     178           0 : DBA_OPTIMIZE_FUNC(gdbm)
     179             : {
     180           0 :         GDBM_DATA;
     181           0 :         gdbm_reorganize(dba->dbf);
     182           0 :         return SUCCESS;
     183             : }
     184             : 
     185           0 : DBA_SYNC_FUNC(gdbm) 
     186             : {
     187           0 :         GDBM_DATA;
     188             : 
     189           0 :         gdbm_sync(dba->dbf);
     190           0 :         return SUCCESS;
     191             : }
     192             : 
     193           2 : DBA_INFO_FUNC(gdbm)
     194             : {
     195           2 :         return estrdup(gdbm_version);
     196             : }
     197             : 
     198             : #endif
     199             : 
     200             : /*
     201             :  * Local variables:
     202             :  * tab-width: 4
     203             :  * c-basic-offset: 4
     204             :  * End:
     205             :  * vim600: sw=4 ts=4 fdm=marker
     206             :  * vim<600: sw=4 ts=4
     207             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:11 +0000 (9 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.