summaryrefslogtreecommitdiff
path: root/src/valueiterator.cpp
blob: 08520d5d29be00da6a9a1094b10da9013f8b7172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 *  ValueIterator.cpp
 *
 *  Implementation of the value iterator
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */
#include "includes.h"

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Constructor
 *  @param  hashtable       The hashtable to iterate over
 *  @param  first           Should it start at the first position?
 */
ValueIterator::ValueIterator(HashTable *hashtable, bool first) : _table(hashtable)
{
    // reset the hash pointer to the internal position
    if (hashtable && first) 
    {
        // move to first position
        zend_hash_internal_pointer_reset_ex(_table, &_position);
        
        // read current data
        read();
    }
    else
    {
        // start with invalid data
        invalidate();
    }
}

/**
 *  Increment position
 *  @return ValueIterator
 */
ValueIterator &ValueIterator::operator++()
{
    // leap out if already on an invalid pos (behind the last pos)
    if (!_position) return *this;
    
    // move the iterator forward
    if (zend_hash_move_forward_ex(_table, &_position) == SUCCESS)
    {
        // read current key and value
        return read();
    }
    else
    {
        // invalidate current position
        return invalidate();
    }
}

/**
 *  Decrement position
 *  @return ValueIterator
 */
ValueIterator &ValueIterator::operator--()
{
    // leap out if we're not even iterating over a hash table
    if (!_table) return *this;
    
    // if position is invalid, it is one position behind the last position
    if (!_position)
    {
        // move to last position
        zend_hash_internal_pointer_end_ex(_table, &_position);
    }
    else if (zend_hash_move_backwards_ex(_table, &_position) == FAILURE)
    {
        // invalidate current position
        return invalidate();
    }

    // read current key and value
    return read();
}

/**
 *  Read current key and value
 *  @return ValueIterator
 */
ValueIterator &ValueIterator::read()
{
    // zval to read the current key in
    Value key;

#if PHP_VERSION_ID >= 50500

    // read in the current key
    zend_hash_get_current_key_zval_ex(_table, key._val, &_position);
    
    // if the key is set to NULL, it means that the object is not at a valid position
    if (key.isNull()) return invalidate();
    
#else

    // php 5.3 and php 5.4 need a different implementation because the function
    // zend_hash_get_current_key_zval_ex is missing in php 5.3, declare variables
    // we need for storing the key in
    char *string_key;
    unsigned int str_len;
    unsigned long num_key;
    
    // get the current key
    int type = zend_hash_get_current_key_ex(_table, &string_key, &str_len, &num_key, 0, &_position);
    
    // if key is not found, the iterator is at an invalid position
    if (type == HASH_KEY_NON_EXISTANT) return invalidate();

    // numeric keys are the easiest ones
    if (type == HASH_KEY_NON_EXISTANT) key = (int64_t)num_key;
    else key = string_key;

#endif

    // iterator is at a valid position, go fetch the data
    // this is the variable we need for fetching the data
    zval **value;
            
    // retrieve data
    zend_hash_get_current_data_ex(_table, (void **) &value, &_position);
    
    // we can now update the current data
    _current = std::make_pair<Value,Value>(std::move(key), *value);
    
    // done
    return *this;
}

/**
 *  Invalidate the iterator
 *  @return ValueIterator
 */
ValueIterator &ValueIterator::invalidate()
{
    // forget current position
    _position = nullptr;
    
    // make the data a pair of null ptrs
    _current = std::make_pair<Value,Value>(nullptr,nullptr);
    
    // done
    return *this;
}

/**
 *  End namespace
 */
}