Back to index

php5  5.3.10
Classes | Defines | Functions
flatfile.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  datum
struct  flatfile

Defines

#define FLATFILE_INSERT   1
#define FLATFILE_REPLACE   0

Functions

int flatfile_store (flatfile *dba, datum key_datum, datum value_datum, int mode TSRMLS_DC)
datum flatfile_fetch (flatfile *dba, datum key_datum TSRMLS_DC)
int flatfile_delete (flatfile *dba, datum key_datum TSRMLS_DC)
int flatfile_findkey (flatfile *dba, datum key_datum TSRMLS_DC)
datum flatfile_firstkey (flatfile *dba TSRMLS_DC)
datum flatfile_nextkey (flatfile *dba TSRMLS_DC)
char * flatfile_version ()

Class Documentation

struct datum

Definition at line 24 of file flatfile.h.

Class Members
char * dptr
size_t dsize
struct flatfile

Definition at line 29 of file flatfile.h.

Collaboration diagram for flatfile:
Class Members
size_t CurrentFlatFilePos
php_stream * fp
int lockfd
char * lockfn
datum nextkey

Define Documentation

#define FLATFILE_INSERT   1

Definition at line 37 of file flatfile.h.

#define FLATFILE_REPLACE   0

Definition at line 38 of file flatfile.h.


Function Documentation

int flatfile_delete ( flatfile dba,
datum key_datum  TSRMLS_DC 
)

Definition at line 107 of file flatfile.c.

                                                              {
       char *key = key_datum.dptr;
       size_t size = key_datum.dsize;
       size_t buf_size = FLATFILE_BLOCK_SIZE;
       char *buf = emalloc(buf_size);
       size_t num;
       size_t pos;

       php_stream_rewind(dba->fp);
       while(!php_stream_eof(dba->fp)) {
              /* read in the length of the key name */
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              pos = php_stream_tell(dba->fp);

              /* read in the key name */
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0)  {
                     break;
              }

              if (size == num && !memcmp(buf, key, size)) {
                     php_stream_seek(dba->fp, pos, SEEK_SET);
                     php_stream_putc(dba->fp, 0);
                     php_stream_flush(dba->fp);
                     php_stream_seek(dba->fp, 0L, SEEK_END);
                     efree(buf);
                     return SUCCESS;
              }      

              /* read in the length of the value */
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              /* read in the value */
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
       }
       efree(buf);
       return FAILURE;
}      

Here is the caller graph for this function:

datum flatfile_fetch ( flatfile dba,
datum key_datum  TSRMLS_DC 
)

Definition at line 87 of file flatfile.c.

                                                               {
       datum value_datum = {NULL, 0};
       char buf[16];

       if (flatfile_findkey(dba, key_datum TSRMLS_CC)) {
              if (php_stream_gets(dba->fp, buf, sizeof(buf))) {
                     value_datum.dsize = atoi(buf);
                     value_datum.dptr = safe_emalloc(value_datum.dsize, 1, 1);
                     value_datum.dsize = php_stream_read(dba->fp, value_datum.dptr, value_datum.dsize);
              } else {
                     value_datum.dptr = NULL;
                     value_datum.dsize = 0;
              }
       }
       return value_datum;
}

Here is the call graph for this function:

int flatfile_findkey ( flatfile dba,
datum key_datum  TSRMLS_DC 
)

Definition at line 165 of file flatfile.c.

                                                               {
       size_t buf_size = FLATFILE_BLOCK_SIZE;
       char *buf = emalloc(buf_size);
       size_t num;
       int ret=0;
       void *key = key_datum.dptr;
       size_t size = key_datum.dsize;

       php_stream_rewind(dba->fp);
       while (!php_stream_eof(dba->fp)) {
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
              if (size == num) {
                     if (!memcmp(buf, key, size)) {
                            ret = 1;
                            break;
                     }
              }      
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
       }
       efree(buf);
       return ret;
}

Here is the caller graph for this function:

datum flatfile_firstkey ( flatfile *dba  TSRMLS_DC)

Definition at line 213 of file flatfile.c.

                                                 {
       datum res;
       size_t num;
       size_t buf_size = FLATFILE_BLOCK_SIZE;
       char *buf = emalloc(buf_size);

       php_stream_rewind(dba->fp);
       while(!php_stream_eof(dba->fp)) {
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
              if (*(buf) != 0) {
                     dba->CurrentFlatFilePos = php_stream_tell(dba->fp);
                     res.dptr = buf;
                     res.dsize = num;
                     return res;
              }
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
       }
       efree(buf);
       res.dptr = NULL;
       res.dsize = 0;
       return res;
}
datum flatfile_nextkey ( flatfile *dba  TSRMLS_DC)

Definition at line 261 of file flatfile.c.

                                                {
       datum res;
       size_t num;
       size_t buf_size = FLATFILE_BLOCK_SIZE;
       char *buf = emalloc(buf_size);

       php_stream_seek(dba->fp, dba->CurrentFlatFilePos, SEEK_SET);
       while(!php_stream_eof(dba->fp)) {
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0)  {
                     break;
              }
              if (!php_stream_gets(dba->fp, buf, 15)) {
                     break;
              }
              num = atoi(buf);
              if (num >= buf_size) {
                     buf_size = num + FLATFILE_BLOCK_SIZE;
                     buf = erealloc(buf, buf_size);
              }
              num = php_stream_read(dba->fp, buf, num);
              if (num < 0) {
                     break;
              }
              if (*(buf)!=0) {
                     dba->CurrentFlatFilePos = php_stream_tell(dba->fp);
                     res.dptr = buf;
                     res.dsize = num;
                     return res;
              }
       }
       efree(buf);
       res.dptr = NULL;
       res.dsize = 0;
       return res;
}      
int flatfile_store ( flatfile dba,
datum  key_datum,
datum  value_datum,
int mode  TSRMLS_DC 
)

Definition at line 51 of file flatfile.c.

                                                                                          {
       if (mode == FLATFILE_INSERT) {
              if (flatfile_findkey(dba, key_datum TSRMLS_CC)) {
                     return 1;
              }
              php_stream_seek(dba->fp, 0L, SEEK_END);
              php_stream_printf(dba->fp TSRMLS_CC, "%zu\n", key_datum.dsize);
              php_stream_flush(dba->fp);
              if (php_stream_write(dba->fp, key_datum.dptr, key_datum.dsize) < key_datum.dsize) {
                     return -1;
              }
              php_stream_printf(dba->fp TSRMLS_CC, "%zu\n", value_datum.dsize);
              php_stream_flush(dba->fp);
              if (php_stream_write(dba->fp, value_datum.dptr, value_datum.dsize) < value_datum.dsize) {
                     return -1;
              }
       } else { /* FLATFILE_REPLACE */
              flatfile_delete(dba, key_datum TSRMLS_CC);
              php_stream_printf(dba->fp TSRMLS_CC, "%zu\n", key_datum.dsize);
              php_stream_flush(dba->fp);
              if (php_stream_write(dba->fp, key_datum.dptr, key_datum.dsize) < key_datum.dsize) {
                     return -1;
              }
              php_stream_printf(dba->fp TSRMLS_CC, "%zu\n", value_datum.dsize);
              if (php_stream_write(dba->fp, value_datum.dptr, value_datum.dsize) < value_datum.dsize) {
                     return -1;
              }
       }

       php_stream_flush(dba->fp);
       return 0;
}

Here is the call graph for this function:

char* flatfile_version ( )

Definition at line 308 of file flatfile.c.

{
       return "1.0, $Revision: 306527 $";
}