Ejemplo n.º 1
0
/* Iterate over the given php zval** and transform into protobuf compatible values.
   Write those into the given protobuf message pointer. */
int
php_message (const ProtobufCMessage* message, zval* val)
{
    HashPosition pos;
    HashTable* hash_table = Z_ARRVAL_P(val);
    zval** data;
    char* key;
    int i, j, key_len;
    long index;

    // check if all required fields are existent in the given php array.
    // NULL default values if not
    for (j=0 ; j < message->descriptor->n_fields ; ++j) {
        const ProtobufCFieldDescriptor* tmp_field = message->descriptor->fields + j;

        if (! zend_symtable_exists(hash_table, (char*)tmp_field->name,
                strlen((char*)(tmp_field->name)) + 1)) {
            if (tmp_field->label == PROTOBUF_C_LABEL_REQUIRED) {
                zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
                    "cannot find required field '%s'", tmp_field->name);
                return 1;
            } else {
                null_field(message, tmp_field);
            }
        }
    }

    // copy php values to proto
    zend_hash_internal_pointer_reset_ex(hash_table, &pos);
    for (;; zend_hash_move_forward_ex(hash_table, &pos)) {
        zend_hash_get_current_data_ex(hash_table, (void**)&data, &pos);

        i = zend_hash_get_current_key_ex(hash_table, &key, &key_len, &index, 0, &pos);
        
        if (i == HASH_KEY_NON_EXISTANT) {
            break;
        } else if (i == HASH_KEY_IS_STRING) {

            const ProtobufCFieldDescriptor* field = find_field(message, key);
            if (field == NULL)
                continue;

            if (write_field(message, field, data) != 0) {
                zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
                    "unable to pack field '%s'", field->name);
                return 1;
            }
        }
    }

    return 0;
}
Ejemplo n.º 2
0
int step(const char **field,const long int X, const long int Y, int *rule_born, int *rule_alive, ERROR *error) {
    static char ****neighbors = 0;
    static char **neighbors_num_field = 0;
    char *_cell = 0;
    long int x=0, y=0, offset = 0, _x=0, _y=0;

    if (0 == neighbors_num_field) {
        neighbors_num_field = get_field(X,Y,error);
        if (0 == neighbors_num_field) {
            error->error = MEMORY_ALLOCATE_ERROR;
            strcpy(error->value,"\0");
            return -1; } }

    if (0 == neighbors) {
        neighbors = get_neighbors((const char **) neighbors_num_field, X, Y, error);
        if (0 == neighbors) {
            error->error = MEMORY_ALLOCATE_ERROR;
            strcpy(error->value,"\0");
            return -1; } }

    null_field((const char **)neighbors_num_field, X, Y);

    for (x=0;x<X;x++) {
        for (y=0;y<Y;y++) {
            for (offset=0;offset<8;offset++) {
                /* считаем кол-во соседей; результат оказывается в neigbor_field */
                if (field[x][y]) {
                    (*neighbors[x][y][offset])++; }

#ifdef DEBUG_NEIGHBORS_STEP
                printf("[%li,%li]+%li\n",x,y,offset);

                for (_y=0;_y<Y;_y++) {
                    for (_x=0;_x<X;_x++) {
                        printf("%2d,",*(*(neighbors_field+_x)+_y)); }
                    printf("\n"); }
                printf("\n");
#endif
            } } }


    /* Game Step */
    for (x=0;x<X;x++) {
        for (y=0;y<Y;y++) {

            _cell = (char *)(field[x] + y);

            switch (*_cell) {
            case 0:
                if (num_in_rule(neighbors_num_field[x][y],rule_born)) {
                    *_cell = 1; }
                break;
            case 1:
                if (!num_in_rule(neighbors_num_field[x][y],rule_alive)) {
                    *_cell = 0; }
                break;
            default:
                break; } } }

//    print_field_num((const char **) neighbors_field,X,Y);
    return 0;
}