Esempio n. 1
0
File: server.c Progetto: ktosiu/zdia
static void *server_thread(void *args) {
    server_t *server = (server_t *)args;

    if(conn_serv_listen(server->conn)) {
        LOG_ERROR("fail to listen.");
        return NULL;
    }

    do {
        conn_t *conn;
        receiver_t *receiver;

        conn = conn_serv_accept(server->conn);
        if (conn == NULL) {
            LOG_ERROR("fail to accept new conenction");
            continue;
        }

        receiver = receiver_new(server, conn);
        if (receiver == NULL) {
            LOG_ERROR("fail to create receiver.");
            continue;
        }

        array_push_back(server->receivers, &receiver);
    } while (1);

    return NULL;
}
Esempio n. 2
0
void array_concat(DynamicArray *array, DynamicArray *array_to_add)
{
	int i ;
	for ( i = 0 ; i < array_to_add->nb_elts ; ++i )
	{
		array_push_back( array, array_get( array_to_add, i ) );
	}
}
Esempio n. 3
0
void test_array() {
    struct array* a = array_new(10);
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    array_debug_print(a);
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    for (int i = 0; i < 10; ++i) {
        array_push_back(a, (void*)i);
    }
    array_debug_print(a);
}
Esempio n. 4
0
void client_list_add(Basic* basic, ClientList* list, LoginClient* client)
{
    ClientByAccountId add;
    
    add.accountId   = login_client_account_id(client);
    add.client      = client;
    
    array_push_back(basic, &list->array, &add);
}
Esempio n. 5
0
void sprite_add_child(struct sprite* self, struct sprite* child) {
    s_assert(child);
    child->child_index = array_size(self->children);
    
    // TODO: when we add the child, search the first NULL position.
    // TODO: consider the ZORDER
    array_push_back(self->children, child);
    child->parent = self;
}
Esempio n. 6
0
void systick_sched_task(task_cb cb, uint32_t period)
{
#if 0
    struct systick_task *task;

    task = malloc(sizeof(struct systick_task));
    task->cb = cb;
    task->period = period;
    array_push_back(task_list, task);
#endif
}
Esempio n. 7
0
void spellbook_add_from_database(ZC* zc, Spellbook* spellbook, uint32_t slotId, uint32_t spellId)
{
    SpellbookSlot slot;
    
    slot.slotId     = slotId;
    slot.spellId    = spellId;
    
    if (!spellbook->knownSpells)
        spellbook->knownSpells = array_create_type(B(zc), SpellbookSlot);
    
    array_push_back(B(zc), &spellbook->knownSpells, &slot);
}
Esempio n. 8
0
File: server.c Progetto: ktosiu/zdia
server_t *server_new() {
    server_t *s = (server_t *)zd_malloc(sizeof(server_t));

    s->conn = NULL;
    s->endpoints = array_new(sizeof(endpoint_t));
    s->receivers = array_new(sizeof(receiver_t *));
    s->workers   = array_new(sizeof(worker_t *));

    int i;
    for (i = 0; i < 4; i++) {
        worker_t *worker = worker_new();
        worker->server = s;
        array_push_back(s->workers, &worker);
    }
    s->curr_worker = 0;

    return s;
}
Esempio n. 9
0
void amule_obj_array_create(const char *class_name, PHP_VALUE_NODE *result)
{
	if ( !result ) {
		return;
	}
	C *container = T::GetContainerInstance();

	container->ReQuery();

	typename std::list<T>::const_iterator it = container->GetBeginIterator();
	while ( it != container->GetEndIterator()) {
		PHP_VAR_NODE *var = array_push_back(result);
		var->value.type = PHP_VAL_OBJECT;
		var->value.obj_val.class_name = class_name;
		const T *cur_item = &(*it);
		var->value.obj_val.inst_ptr = (void *)cur_item;
		it++;
	}
}
Esempio n. 10
0
int lsprite_set_anim(lua_State* L) {
    struct sprite* self = (struct sprite*)lua_touserdata(L, 1);
    luaL_checktype(L, 2, LUA_TTABLE);
    
    int len = luaL_len(L, 2);
    struct array* frames = array_new(len);
    for (int i = 1; i <= len; ++i) {
        lua_rawgeti(L, -1, i);
        struct sprite_frame* frame = lua_touserdata(L, -1);
        array_push_back(frames, frame);
        lua_pop(L, 1);
    }
    
    struct anim* anim = anim_new(frames);
    sprite_set_anim(self, anim);
    
    array_free(frames);
    return 0;
}
Esempio n. 11
0
static void cluster_points(array_t *clusters, array_t *points)
{
    int n = array_length(points);
    int k = array_length(clusters);

    for (int i=0; i<n; i++) {
        float distance = FLT_MAX;
        cluster_t *cl_nearest = NULL;
        point_t *p = array_at(points, i);

        for (int j=0; j<k; j++) {
            cluster_t *cl = array_at(clusters, j);
            float d = point_distance(p, &cl->centroid);
            if (d < distance) {
                distance = d;
                cl_nearest = cl;
            }
        }
        if (cl_nearest == NULL) {
            __asm__ volatile ("BKPT");
        }
        /* copy point and add to cluster */
        array_push_back(cl_nearest->points, p);
    }
Esempio n. 12
0
array_t *imlib_detect_objects(image_t *image, cascade_t *cascade, rectangle_t *roi)
{
    // Integral images
    mw_image_t sum;
    mw_image_t ssq;

    // Detected objects array
    array_t *objects;

    // Allocate the objects array
    array_alloc(&objects, xfree);

    // Set cascade image pointers
    cascade->img = image;
    cascade->sum = &sum;
    cascade->ssq = &ssq;

    // Set scanning step.
    // Viola and Jones achieved best results using a scaling factor
    // of 1.25 and a scanning factor proportional to the current scale.
    // Start with a step of 5% of the image width and reduce at each scaling step
    cascade->step = (roi->w*50)/1000;

    // Make sure step is less than window height + 1
    if (cascade->step > cascade->window.h) {
        cascade->step = cascade->window.h;
    }

    // Allocate integral images
    imlib_integral_mw_alloc(&sum, roi->w, cascade->window.h+1);
    imlib_integral_mw_alloc(&ssq, roi->w, cascade->window.h+1);

    // Iterate over the image pyramid
    for(float factor=1.0f; ; factor *= cascade->scale_factor) {
        // Set the scaled width and height
        int szw = roi->w/factor;
        int szh = roi->h/factor;

        // Break if scaled image is smaller than feature size
        if (szw < cascade->window.w || szh < cascade->window.h) {
            break;
        }

        // Set the integral images scale
        imlib_integral_mw_scale(roi, &sum, szw, szh);
        imlib_integral_mw_scale(roi, &ssq, szw, szh);

        // Compute new scaled integral images
        imlib_integral_mw_ss(image, &sum, &ssq, roi);

        // Scale the scanning step
        cascade->step = cascade->step/factor;
        cascade->step = (cascade->step == 0) ? 1 : cascade->step;

        // Process image at the current scale
        // When filter window shifts to borders, some margin need to be kept
        int y2 = szh - cascade->window.h;
        int x2 = szw - cascade->window.w;

        // Shift the filter window over the image.
        for (int y=0; y<y2; y+=cascade->step) {
            for (int x=0; x<x2; x+=cascade->step) {
                point_t p = {x, y};
                // If an object is detected, record the coordinates of the filter window
                if (run_cascade_classifier(cascade, p) > 0) {
                    array_push_back(objects,
                        rectangle_alloc(fast_roundf(x*factor) + roi->x, fast_roundf(y*factor) + roi->y,
                        fast_roundf(cascade->window.w*factor), fast_roundf(cascade->window.h*factor)));
                }
            }

            // If not last line, shift integral images
            if ((y+cascade->step) < y2) {
                imlib_integral_mw_shift_ss(image, &sum, &ssq, roi, cascade->step);
            }
        }
    }

    imlib_integral_mw_free(&ssq);
    imlib_integral_mw_free(&sum);

    if (array_length(objects) > 1)   {
        // Merge objects detected at different scales
        objects = rectangle_merge(objects);
    }

    return objects;
}
Esempio n. 13
0
void php_native_split(PHP_VALUE_NODE *result)
{
	if ( result ) {
		cast_value_array(result);
	} else {
		return;
	}
	PHP_VALUE_NODE *pattern, *string_to_split, *split_limit;
	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");
	if ( si ) {
		pattern = &si->var->value;
		cast_value_str(pattern);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument: pattern");
		return;
	}
	si = get_scope_item(g_current_scope, "__param_1");
	if ( si ) {
		string_to_split = &si->var->value;
		cast_value_str(string_to_split);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument: string");
		return;
	}
	si = get_scope_item(g_current_scope, "__param_2");
	if ( si ) {
		split_limit = &si->var->value;
		cast_value_dnum(split_limit);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument: string");
		return;
	}
#ifdef PHP_STANDALONE_EN
	regex_t preg;
	char error_buff[256];
	int reg_result = regcomp(&preg, pattern->str_val, REG_EXTENDED);
	if ( reg_result ) {
		regerror(reg_result, &preg, error_buff, sizeof(error_buff));
		php_report_error(PHP_ERROR, "Failed in regcomp: %s", error_buff);
#else
	wxRegEx preg;
	if (!preg.Compile(wxString(char2unicode(pattern->str_val)), wxRE_EXTENDED)) {
		php_report_error(PHP_ERROR, "Failed in Compile of: %s", pattern->str_val);
#endif
		return;
	}

#ifdef PHP_STANDALONE_EN
	size_t nmatch = strlen(string_to_split->str_val);
	regmatch_t *pmatch = new regmatch_t[nmatch];
#endif
	char *str_2_match = string_to_split->str_val;
	char *tmp_buff = new char[strlen(string_to_split->str_val)+1];

	while ( 1 ) {
//		printf("matching: %s\n", str_2_match);
#ifdef PHP_STANDALONE_EN
		reg_result = regexec(&preg, str_2_match, nmatch, pmatch, 0);
		if ( reg_result ) {
#else
		if (!preg.Matches(wxString(char2unicode(str_2_match)))) {
#endif
			// no match
			break;
		}
#ifndef PHP_STANDALONE_EN
		// get matching position
		size_t start, len;
		if (!preg.GetMatch(&start, &len)) {
			break;	// shouldn't happen
		}
#endif
		/*
		 * I will use only first match, since I don't see any sense to have more
		 * then 1 match in split() call
		 */
#ifdef PHP_STANDALONE_EN
		for(int i = 0; i < pmatch[0].rm_so; i++) {
#else
		for(size_t i = 0; i < start; i++) {
#endif
			tmp_buff[i] = str_2_match[i];
		}
#ifdef PHP_STANDALONE_EN
		tmp_buff[pmatch[0].rm_so] = 0;
#else
		tmp_buff[start] = 0;
#endif
//		printf("Match added [%s]\n", tmp_buff);

		PHP_VAR_NODE *match_val = array_push_back(result);
		match_val->value.type = PHP_VAL_STRING;
		match_val->value.str_val = strdup(tmp_buff);

#ifdef PHP_STANDALONE_EN
		str_2_match += pmatch[0].rm_eo;
#else
		str_2_match += start + len;
#endif
	}

	PHP_VAR_NODE *match_val = array_push_back(result);
	match_val->value.type = PHP_VAL_STRING;
	match_val->value.str_val = strdup(str_2_match);

	delete [] tmp_buff;
#ifdef PHP_STANDALONE_EN
	delete [] pmatch;
	regfree(&preg);
#endif
}

#ifdef ENABLE_NLS

void php_native_gettext(PHP_VALUE_NODE *result)
{
	PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0");
	PHP_VALUE_NODE *str = &si_str->var->value;
	if ( si_str ) {
		cast_value_str(str);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext'");
		return;
	}
	if ( result ) {
		cast_value_dnum(result);
		result->type = PHP_VAL_STRING;
		result->str_val = strdup(gettext(str->str_val));
	}
}

void php_native_gettext_noop(PHP_VALUE_NODE *result)
{
	PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0");
	PHP_VALUE_NODE *str = &si_str->var->value;
	if ( si_str ) {
		cast_value_str(str);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext_noop'");
		return;
	}
	if ( result ) {
		cast_value_dnum(result);
		result->type = PHP_VAL_STRING;
		result->str_val = strdup(str->str_val);
	}
}

void php_native_ngettext(PHP_VALUE_NODE *result)
{
	PHP_SCOPE_ITEM *si_msgid = get_scope_item(g_current_scope, "__param_0");
	PHP_VALUE_NODE *msgid = &si_msgid->var->value;
	if ( si_msgid ) {
		cast_value_str(msgid);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'ngettext'");
		return;
	}
	PHP_SCOPE_ITEM *si_msgid_plural = get_scope_item(g_current_scope, "__param_1");
	PHP_VALUE_NODE *msgid_plural = &si_msgid_plural->var->value;
	if ( si_msgid_plural ) {
		cast_value_str(msgid_plural);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid_plural' for 'ngettext'");
		return;
	}
	PHP_SCOPE_ITEM *si_count = get_scope_item(g_current_scope, "__param_2");
	PHP_VALUE_NODE *count = &si_count->var->value;
	if ( si_count ) {
		cast_value_dnum(count);
	} else {
		php_report_error(PHP_ERROR, "Invalid or missing argument 'count' for 'ngettext'");
		return;
	}
	if ( result ) {
		cast_value_dnum(result);
		result->type = PHP_VAL_STRING;
		result->str_val = strdup(ngettext(msgid->str_val, msgid_plural->str_val, count->int_val));
	}
}
Esempio n. 14
0
void test_array(void)
{
  int i;
  double* d;
  void* A = array_create(0);
  fprintf(stdout, "call function : %s\n", __func__);
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_push_back ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_pop_back ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_pop_back(A);
    fprintf(stdout, "  the pop element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_erase ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_erase(A, array_begin(A));
    fprintf(stdout, "  the erased begin element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);
    d = (double*)array_erase(A, array_end(A));
    fprintf(stdout, "  the erased end element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_remove ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_remove(A, 3);
    fprintf(stdout, "  the removed [3] -> {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_copy ===>\n");
  {
    void* copy_A;
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    copy_A = array_copy(A, 10);
    array_object_show(copy_A);
    array_for_each(copy_A, array_element_display, NULL);
    array_release(&copy_A);

    array_for_each(A, array_element_destroy, NULL);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_resize ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_resize(A, 20);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_release ===>\n");
  array_release(&A);
  array_object_show(A);
}