Beispiel #1
0
void out_addUnits(jmp_buf jmpBuf, struct Output* output, struct Units* units) {
	struct AddUnitData data = {
		.list = &output->out[ALIGN_LEFT]
	};
	vector_foreach(jmpBuf, &units->left, vecAddUnit, &data);
	data.list = &output->out[ALIGN_CENTER];
	vector_foreach(jmpBuf, &units->center, vecAddUnit, &data);
	data.list = &output->out[ALIGN_RIGHT];
	vector_foreach(jmpBuf, &units->right, vecAddUnit, &data);
}

struct PrintUnitData {
	bool first;
	char* sep;
	size_t sepLen;
	Vector* vec;
};
static bool vecPrintUnit(jmp_buf jmpBuf, void* elem, void* userdata) {
	struct PrintUnitData* data = (struct PrintUnitData*)userdata;
	struct Unit* unit = *(struct Unit**)elem;
	if(!unit->render)
		return true;
	vector_putListBack(jmpBuf, data->vec, "%{F-}%{B-}%{T-}", 15);
	if(!data->first)
		vector_putListBack(jmpBuf, data->vec, data->sep, data->sepLen);
	vector_putListBack(jmpBuf, data->vec, "%{F-}%{B-}%{T-}", 15);
	vector_putListBack(jmpBuf, data->vec, unit->buffer, strlen(unit->buffer));
	data->first = false;
	return true;
}

//REMEMBER TO FREE THE STRING
char* out_format(jmp_buf jmpBuf, struct Output* output, struct Unit* unit) {
	Vector vec;
	vector_init(jmpBuf, &vec, sizeof(char), 128);
	for(int mon = 0; mon < output->maxMon; mon++) {
		char monStr[33];
		int monStrLen = snprintf(monStr, 33, "%d", mon);
		vector_putListBack(jmpBuf, &vec, "%{S", 3);
		vector_putListBack(jmpBuf, &vec, monStr, monStrLen);
		vector_putListBack(jmpBuf, &vec, "}", 1);
		for(int i = ALIGN_FIRST; i <= ALIGN_LAST; i++) {
			vector_putListBack(jmpBuf, &vec, AlignStr[i], strlen(AlignStr[i]));
			struct PrintUnitData data = {
				.first = true,
				.sep = output->separator,
				.sepLen = strlen(output->separator),
				.vec = &vec,
			};
			vector_foreach(jmpBuf, &output->out[i], vecPrintUnit, &data);
		}
	}
	//Remember to add the terminator back on
	static char term = '\0';
	vector_putBack(jmpBuf, &vec, &term);
	//Copy into new buffer owned by calling function
	char* buff = vector_detach(&vec);
	return buff;
}
Beispiel #2
0
int
main(int argc, char **argv)
{
	int *a, *b, *c, *d, *e, *f, *g;
	a = (int *)malloc(sizeof(int));
	b = (int *)malloc(sizeof(int));
	c = (int *)malloc(sizeof(int));
	d = (int *)malloc(sizeof(int));
	e = (int *)malloc(sizeof(int));
	f = (int *)malloc(sizeof(int));
	g = (int *)malloc(sizeof(int));
	*a = 6;
	*b = 1;
	*c = 99;
	*d = -17;
	*e = 22;
	*f = 9;
	*g = 6;

	struct Vector *iv = create_vector(2, free);
	vector_push_back(iv, a);
	vector_push_back(iv, b);
	vector_push_back(iv, c);
	vector_push_back(iv, d);
	vector_push_front(iv, e);
	vector_push_front(iv, f);
	vector_insert(iv, g, 5);

	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	printf("%d\n", *f);
	vector_remove(iv, f, compare, TRUE);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	vector_sort(iv, compare);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	printf("\nBsearch: %d, Lower: %d, Upper: %d\n", vector_bsearch(iv, a, compare), 
			vector_lower(iv, a, compare), vector_upper(iv, a, compare));

	vector_shuffle(iv);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	destroy_vector(iv, TRUE);
}
Beispiel #3
0
void test4(void)
{
    vector_t vec = vector_from_string_list(NULL_VECTOR, "1920x1080,1280x720,720x576,720x480,704x576", NULL);
    vector_foreach(vec, test4_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_clear(vec);
    vector_destroy(vec);
}
Beispiel #4
0
void receive_user_data(void)
{
    char buf[BUF_SIZE] = {0};
    char message[BUF_SIZE] = {0};
    char *name;
    Client *client;

    int read_bytes = read(0, buf, BUF_SIZE);

    assert(read_bytes < BUF_SIZE);
    if (read_bytes == -1) {
        warn("Could not read from STDIN data.");
        return;
    }
    if (read_bytes == 1) {
        /* read only LF symbol */
        info("Could not send empty message.");
        return;
    }
    /* remove last LF symbol readed from stdin */
    buf[read_bytes - 1] = '\0';

    switch(parse_user_command(buf)) {
    case KILL:
        name = get_command_argument(buf);
        if (!name) {
            warn("Name of client should be provided.");
            break;
        }
        client = vector_delete_first_equal(_s_clients, name, cmp_clients_by_name);
        info("Server has '%d' clients.", vector_total(_s_clients));
        if (client) {
            sprintf(message, "The client '%s' left the chat.", client->name);
            send_broadcast_msg(message);
            info("%s", message);
            /* send info data to client */
            sprintf(message, "server: You was killed.");
            send_msg(client, message);
            client_destroy(client);
        } else {
            info("Client '%s' was not found.", name);
        }
    break;
    case SHUTDOWN:
        sprintf(message, "The server is going shutdown. Bye.");
        send_broadcast_msg(message);
        _s_runing = 0;
        info("%s", message);
    break;
    case WHO:
        vector_foreach(_s_clients, message, collect_all_names);
        info("All clients: '%s'", message);
    break;
    default:
        warn("Unknown command. Allowed commands: _kill <name>, _shutdown, _who");
    break;
    }
}
Beispiel #5
0
void test1(void)
{
    vector_t vec = vector_create2(0, 4);
    int value = 1;
    for (int i = 0; i < 1000; i++)
        vector_push_back(vec, &value);
    vector_foreach(vec, test1_walker, NULL);
    vector_destroy(vec);
}
Beispiel #6
0
void vector_clear(vector_t *v, void (*df)(void *)) {
    assert(v);
    if (df == NULL) {
        memset(v->data, 0, sizeof(void *) * v->count);
    } else {
        vector_foreach(v, df);
    }
    v->count = 0;
}
Beispiel #7
0
void
free_mapnode_list(vector_t *list)
{
	if (list)
	{
		vector_foreach(list, free_mapnode_st);
		free(list);
	}
	max_id = min_id = 0;
}
Beispiel #8
0
void test2(void)
{
    vector_t vec = vector_create2(0, 256);
    vector_set_option(vec, VECTOR_ELEMENT_COPY, (void*)strncpy);
    for (int i = 0; i < 10000; i++)
        vector_push_back(vec, "Hello, vector");
    vector_foreach(vec, test2_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_add_flags(vec, VECTOR_FLAG_FASTCLEAR);
    vector_clear(vec);
    vector_destroy(vec);
}
Beispiel #9
0
void handle_client_data(Client *a_client, char *a_data, const char *a_address)
{
    char *name = NULL;
    char message[BUF_SIZE] = {0};
    char buf[BUF_SIZE] = {0};

    ServerCommand client_cmd = parse_client_command(a_data);

    if (a_client->connected == 0 && client_cmd != CONNECT) {
        warn("Client with addres '%s' is not connected."
                        "Message '%s' not allowed.", a_address, a_data);
        return;
    } else if (a_client->connected == 1 && client_cmd == CONNECT) {
        warn("Cannot add client from address '%s'. Client with the same"
               "address already connected.", a_address);
        return;
    }

    switch (client_cmd) {
    case CONNECT:
        name = get_command_argument(a_data);
        verificate_name(&name);
        a_client->name = name;
        sprintf(message, "New client '%s' was connected.", a_client->name);
        send_broadcast_msg(message);
        info("%s", message);
        a_client->connected = 1;
    break;
    case QUIT:
        vector_delete_first_equal(_s_clients, a_client, cmp_clients_by_ptr);
        sprintf(message, "The client '%s' left the chat.", a_client->name);
        send_broadcast_msg(message);
        info("%s", message);
        client_destroy(a_client);
        break;
    case WHO:
        vector_foreach(_s_clients, buf, collect_all_names);
        sprintf(message, "All clients: %s", buf);
        info("Send info about all clients to '%s'.", a_client->name);
        send_msg(a_client, message);
        break;
    case UNDEFINED:
        /* send received message to all clients */
        info("Send '%s' to all clients from '%s'.", a_data, a_client->name);
        sprintf(message, "%s: %s", a_client->name, a_data);
        send_broadcast_msg(message);
        break;
    default:
        assert("Unknown command " == NULL);
        break;
    }
}
Beispiel #10
0
void test3(void)
{
    char buffer[8192];
    vector_t vec = vector_create_on_buffer(20, buffer, sizeof(buffer));
    vector_set_option(vec, VECTOR_ELEMENT_COPY, (void*)strncpy);
    for (int i = 0; i < 10000; i++)
        vector_push_back(vec, "Hello, vector on buffer");
    vector_foreach(vec, test2_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_add_flags(vec, VECTOR_FLAG_FASTCLEAR);
    vector_clear(vec);
    vector_destroy(vec);
}
Beispiel #11
0
Datei: gc.c Projekt: mtmiron/toi
VALUE
gc_never_free(VALUE obj)
{
    if (!obj)
        return Qnil;
    BASIC(obj).refcount = GC_NEVER_FREE;

    if (TYPE(obj) == T_ARRAY)
        vector_foreach(ARRAY(obj)->ptr, gc_never_free0);
    else if (TYPE(obj) == T_HASH)
        st_foreach(HASH(obj)->ptr, gc_never_free_hash0, 0);

    return obj;
}
Beispiel #12
0
Datei: gc.c Projekt: mtmiron/toi
VALUE
gc_s_run(VALUE self)
{
    VALUE sav_cur, thr = cur_thr;

    sav_cur = cur_thr;
    while (thr)
    {
        vector_foreach(THREAD(thr)->obj_stk, object_refcount_check);
        thr = cur_thr = THREAD(thr)->up;
    }
    cur_thr = sav_cur;

    return Qnil;
}
Beispiel #13
0
static void
thread_reap(VALUE thr)
{
	Thread *th;
	vector_t *v;

	th = THREAD(thr);
	v = th->obj_stk;
	vector_foreach(v, thread_reap_check);

	st_free_table(th->env_tbl);
	vector_free(th->obj_stk);
	vector_free(th->env_stk);
	vector_free(th->self_stk);
	vector_free(th->tok_stk);
	vector_free(th->stack);
#ifdef SYMBOL_CACHE
	vector_free(th->modified_syms);
#endif
}
Beispiel #14
0
int test_run (void)
   {
   SCL_vector_t     vector;
   SCL_iterator_t   iterator;

   void*   data;
   int     stat;
   int     i;

   printf ("Create vector: ");
   fflush (stdout);
   vector = vector_new();
   printf ("(%p = vector_new()) != NULL ... ", vector);
   fflush (stdout);
   printf ("%s\n", ((vector!=NULL) ? "PASS" : "FAIL"));

   printf (
          "(%ld = vector_size (vector)) == 0 ..................... %s\n",
          vector_size(vector),
          ((vector_size(vector)==0) ? "PASS" : "FAIL")
          );
   printf (
          "(%ld = vector_count (vector)) == 0 .................... %s\n",
          vector_count(vector),
          ((vector_count(vector)==0) ? "PASS" : "FAIL")
          );

   printf ("Push data in back vector.\n");
   for (i=0 ; i<VECTOR_SIZE ; i++)
      {
      stat = vector_push_back (vector, (void*)g_data1[i]);
      printf (
             "[%d,%s] = vector_push_back (vector, %08X) .... %s\n",
             stat, scl_statstr(stat), g_data1[i],
             ((stat==SCL_OK) ? "PASS" : "FAIL")
             );
      }

   printf ("Iterate back through vector, changing data.\n");
   for (i=7, iterator=vector_end(vector) ; iterator!=NULL ; iterator=vector_prev(iterator))
      {
      data = vector_data_get (iterator);
      printf (
             "%08X = vector_data_get (iterator=>%p) .... %s\n",
             data, iterator,
             (((unsigned long)data==g_data1[--i]) ? "PASS" : "FAIL")
             );
      vector_data_set (iterator, iterator);
      printf ("%08X -> %08X\n", data, vector_data_get(iterator));
      }

   printf (
          "(%ld = vector_size (vector)) == 16 ................... %s\n",
          vector_size(vector),
          ((vector_size(vector)==16) ? "PASS" : "FAIL")
          );
   printf (
          "(%ld = vector_count (vector)) == 7 .................... %s\n",
          vector_count(vector),
          ((vector_count(vector)==7) ? "PASS" : "FAIL")
          );

   printf ("Foreach on vector.\n");
   for (i=0 ; i<VECTOR_SIZE ; i++) g_flag[i] = 0;
   g_vector = vector;
   vector_foreach (vector, cbfn, NULL);

   printf ("Erase vector.\n");
   vector_erase (vector);

   printf (
          "(%ld = vector_size (vector)) == 0 ..................... %s\n",
          vector_size(vector),
          ((vector_size(vector)==0) ? "PASS" : "FAIL")
          );
   printf (
          "(%ld = vector_count (vector)) == 0 .................... %s\n",
          vector_count(vector),
          ((vector_count(vector)==0) ? "PASS" : "FAIL")
          );

   printf ("Delete vector.\n");
   vector_del (vector);

   printf ("\n");

   return 0;
   }
Beispiel #15
0
static inline void send_broadcast_msg(char *a_message)
{
    vector_foreach(_s_clients, a_message,
                   (void (*)(void *, void *))send_msg_if_connected);
}