|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010 - 2011 Shuhei Tanuma |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "php_git.h" |
| 26 | +#include <spl/spl_array.h> |
| 27 | +#include <zend_interfaces.h> |
| 28 | +#include <string.h> |
| 29 | +#include <time.h> |
| 30 | + |
| 31 | + |
| 32 | +PHPAPI zend_class_entry *git_index_iterator_class_entry; |
| 33 | +extern void php_git_index_entry_create(zval **index, git_index_entry *entry); |
| 34 | + |
| 35 | +static void php_git_index_iterator_free_storage(php_git_index_iterator_t *obj TSRMLS_DC) |
| 36 | +{ |
| 37 | + zend_object_std_dtor(&obj->zo TSRMLS_CC); |
| 38 | + obj->index = NULL; |
| 39 | + efree(obj); |
| 40 | +} |
| 41 | + |
| 42 | +zend_object_value php_git_index_iterator_new(zend_class_entry *ce TSRMLS_DC) |
| 43 | +{ |
| 44 | + zend_object_value retval; |
| 45 | + php_git_index_iterator_t *obj; |
| 46 | + zval *tmp; |
| 47 | + |
| 48 | + obj = ecalloc(1, sizeof(*obj)); |
| 49 | + zend_object_std_init( &obj->zo, ce TSRMLS_CC ); |
| 50 | + zend_hash_copy(obj->zo.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); |
| 51 | + |
| 52 | + retval.handle = zend_objects_store_put(obj, |
| 53 | + (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 54 | + (zend_objects_free_object_storage_t)php_git_index_iterator_free_storage, |
| 55 | + NULL TSRMLS_CC); |
| 56 | + retval.handlers = zend_get_std_object_handlers(); |
| 57 | + return retval; |
| 58 | +} |
| 59 | + |
| 60 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_git_index_iterator__construct, 0, 0, 1) |
| 61 | + ZEND_ARG_INFO(0, index) |
| 62 | +ZEND_END_ARG_INFO() |
| 63 | + |
| 64 | +PHP_METHOD(git_index_iterator, current) |
| 65 | +{ |
| 66 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 67 | + git_index_entry *entry; |
| 68 | + zval *git_index_entry; |
| 69 | + |
| 70 | + entry = git_index_get(this->index,this->offset); |
| 71 | + if(entry == NULL){ |
| 72 | + zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, |
| 73 | + "specified offset does not exist. %d"); |
| 74 | + RETURN_FALSE; |
| 75 | + } |
| 76 | + php_git_index_entry_create(&git_index_entry, entry); |
| 77 | + RETURN_ZVAL(git_index_entry,0,0); |
| 78 | +} |
| 79 | + |
| 80 | +PHP_METHOD(git_index_iterator, key) |
| 81 | +{ |
| 82 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 83 | + RETURN_LONG(this->offset); |
| 84 | +} |
| 85 | + |
| 86 | +PHP_METHOD(git_index_iterator, next) |
| 87 | +{ |
| 88 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 89 | + this->offset++; |
| 90 | + |
| 91 | + RETURN_TRUE; |
| 92 | +} |
| 93 | + |
| 94 | +PHP_METHOD(git_index_iterator, rewind) |
| 95 | +{ |
| 96 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 97 | + this->offset = 0; |
| 98 | +} |
| 99 | + |
| 100 | +PHP_METHOD(git_index_iterator, valid) |
| 101 | +{ |
| 102 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 103 | + int entry_count = git_index_entrycount(this->index); |
| 104 | + if(this->offset < entry_count && this->offset >= 0){ |
| 105 | + RETURN_TRUE; |
| 106 | + }else{ |
| 107 | + RETURN_FALSE; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +PHP_METHOD(git_index_iterator, __construct) |
| 112 | +{ |
| 113 | + php_git_index_iterator_t *this = (php_git_index_iterator_t *) zend_object_store_get_object(getThis() TSRMLS_CC); |
| 114 | + zval *php_git_index; |
| 115 | + |
| 116 | + if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, |
| 117 | + "z", &php_git_index) == FAILURE){ |
| 118 | + return; |
| 119 | + } |
| 120 | + php_git_index_t *idx = (php_git_index_t *) zend_object_store_get_object(php_git_index TSRMLS_CC); |
| 121 | + |
| 122 | + this->index = idx->index; |
| 123 | + this->offset = 0; |
| 124 | +} |
| 125 | + |
| 126 | +PHPAPI function_entry php_git_index_iterator_methods[] = { |
| 127 | + PHP_ME(git_index_iterator, __construct, arginfo_git_index_iterator__construct,ZEND_ACC_PUBLIC) |
| 128 | + PHP_ME(git_index_iterator, current, NULL, ZEND_ACC_PUBLIC) |
| 129 | + PHP_ME(git_index_iterator, key, NULL, ZEND_ACC_PUBLIC) |
| 130 | + PHP_ME(git_index_iterator, next, NULL, ZEND_ACC_PUBLIC) |
| 131 | + PHP_ME(git_index_iterator, rewind, NULL, ZEND_ACC_PUBLIC) |
| 132 | + PHP_ME(git_index_iterator, valid, NULL, ZEND_ACC_PUBLIC) |
| 133 | +}; |
| 134 | + |
| 135 | +void git_index_iterator_init(TSRMLS_D) |
| 136 | +{ |
| 137 | + zend_class_entry git_index_iterator_ce; |
| 138 | + INIT_NS_CLASS_ENTRY(git_index_iterator_ce, PHP_GIT_NS,"IndexIterator", php_git_index_iterator_methods); |
| 139 | + git_index_iterator_class_entry = zend_register_internal_class(&git_index_iterator_ce TSRMLS_CC); |
| 140 | + git_index_iterator_class_entry->create_object = php_git_index_iterator_new; |
| 141 | + zend_class_implements(git_index_iterator_class_entry TSRMLS_CC, 1, spl_ce_Iterator); |
| 142 | +} |
0 commit comments