Esempio n. 1
0
File: player.c Progetto: janisz/unix
int findPlayerIndexWithNick(arraylist *players, char nick[NICK_LENGTH])
{
	fprintf(stderr,"Search for %s\nPlayers count: %d\n", nick, arraylist_size(players));
	for (int i=0; i<arraylist_size(players); i++) {
		Player *p = (Player*)arraylist_get(players, i);
		if (strcmp(p->nick, nick) == 0) {
			return i;
		}
	}
	return -1;
}
Esempio n. 2
0
File: player.c Progetto: janisz/unix
void removePlayer(Player *player)
{
	pthread_mutex_lock(player->players->lock);
	fprintf(stderr,"%s will be removed\nPlayers count: %d\n", player->nick, arraylist_size(player->players));
	int index = findPlayerIndex(player);
	assert(index >= 0);
	disposePlayer(player);
	arraylist_remove(player->players, index);
	fprintf(stderr,"Players count: %d\n", arraylist_size(player->players));
	pthread_mutex_unlock(player->players->lock);
}
Esempio n. 3
0
File: player.c Progetto: janisz/unix
int findPlayerIndex(Player *player)
{
	DBG;
	fprintf(stderr,"Search for %s\nPlayers count: %d\n", player->nick, arraylist_size(player->players));
	for (int i=0; i<arraylist_size(player->players); i++) {
		Player *p = (Player*)arraylist_get(player->players, i);
		if (strcmp(p->nick, player->nick) == 0) {
			return i;
		}
	}
	return -1;
}
Esempio n. 4
0
////////////////////////////
//////Gets all the terminal values and epsilon
/////////////////////////
void passOne(){
	int r,c;//=arraylist_size(arraylist_get(finalString,0));
	int i,j; //counters
	int set=1;
	//printf("\nTHIS IS THE SIZE %d\n", s);
	r=arraylist_size(finalString);//r is row
	for (i=0;i<r;i++){
		c=arraylist_size(arraylist_get(finalString,i));
		for(j=0;j<c;j++){
			char *str=arraylist_get(arraylist_get(finalString,i),j);
			int *num=arraylist_get(arraylist_get(finalInt,i),j);
			//printf("%s",str);

			if (arraylist_get(arraylist_get(finalInt,i),(j+1))==GOES){
				arraylist_add(p1Str,val);
				arraylist_add(p1Int,lookahead);
				arraylist_add(p1Str,":");
				arraylist_add(p1Int,8); //8 Is for :
				printf("\n%s: ",str);
				set=1;
			}
			else if(num==OR){
				printf("");set=1;
			}
			else if(set && num==GOES){
				printf("");
			}
			else if(set && num==NT){
				printf(""); set=0;
			}
			else if(set && num==7){
				arraylist_add(p1Str,val);
				arraylist_add(p1Int,lookahead);
				printf("Epsilon "); set=0;
			}

			else if(set && num==T){
				arraylist_add(p1Str,val);
				arraylist_add(p1Int,lookahead);
				printf("%s ",str);
				set=0;
			}
			else{
				//printf("Inside ELSE value is %s --", str);
			}

		}
	}
}
Esempio n. 5
0
void arraylist_sort(const Arraylist list, const int (*compare)(const Object object_1, const Object object_2))
{
  qsort(list->_data,
	arraylist_size(list),
	sizeof(Object),
	(int (*)())compare);
}
Esempio n. 6
0
boolean arraylist_remove( arraylist* array, const void* object)
{
  int length = arraylist_size(array);
  int last_index = length - 1;
  int new_size, new_capacity;
  int index;
 
  for (index = 0; index < length; index++)
    {
      if ((*array->equals)(arraylist_get(array, index), object))
	{
	  (array->size)--;
	  if (index < last_index)
	    {
	      memmove(array->data + index, array->data + index + 1, object_size * (last_index - index));
	      new_size = array->size;
	      new_capacity = array->current_capacity - ARRAYLIST_CAPACITY_DELTA;
	      if (new_capacity > new_size)
		{
		  array->data = realloc(array->data, object_size * new_capacity);
		  array->current_capacity = new_capacity;
		}
	    }
	  return TRUE;
	}
    }
  return FALSE;
}
Esempio n. 7
0
void arraylist_sort(const arraylist* array, const int (*compare)(const void* obj1, const void* obj2))
{
  qsort(array->data,
	arraylist_size(array),
	sizeof(void*),
	(int (*)())compare);
}
Esempio n. 8
0
Boolean arraylist_remove(const Arraylist list, const Object object)
{
  int length = arraylist_size(list);
  int last_index = length - 1;
  int new_size, new_capacity;
  int index;

  for (index = 0; index < length; index++)
    {
      if ((*list->_equals)(arraylist_get(list, index), object))
	{
	  (list->_size)--;
	  if (index < last_index)
	    {
	      memmove(list->_data + index, list->_data + index + 1, object_size * (last_index - index));
	      new_size = list->_size;
	      new_capacity = list->_current_capacity - ARRAYLIST_CAPACITY_DELTA;
	      if (new_capacity > new_size)
		{
		  list->_data = realloc(list->_data, object_size * new_capacity);
		  list->_current_capacity = new_capacity;
		}
	    }
	  return TRUE;
	}
    }
  return FALSE;
}
Esempio n. 9
0
main(int argc, char**argv) {
	lookahead = yylex();
	stringArray = arraylist_create(objEquals);
	intArray = arraylist_create(objEquals);
	finalString = arraylist_create(objEquals);
	finalInt = arraylist_create(objEquals);
	p1Int = arraylist_create(objEquals);
	p1Str = arraylist_create(objEquals);
	p2Int = arraylist_create(objEquals);
	p2Str = arraylist_create(objEquals);


	prodList();
	if(lookahead!=0){
		printf("error");
		return 0;
	}	

	int r,c;//=arraylist_size(arraylist_get(finalString,0));
	int i,j; //counters

	//printf("\nTHIS IS THE SIZE %d\n", s);
	r=arraylist_size(finalString);//r is row
	for (i=0;i<r;i++){
		c=arraylist_size(arraylist_get(finalString,i));
		for(j=0;j<c;j++){
			char *str=arraylist_get(arraylist_get(finalString,i),j);
			int *num=arraylist_get(arraylist_get(finalInt,i),j);
			//printf("%s",str);

			if (arraylist_get(arraylist_get(finalInt,i),(j+1))==GOES)
				printf("\n%s ",str);
			else if (num==GOES)
				printf("%s\t\t",str);
			else if (num==OR)
				printf("\n\t%s\t",str);
			else if (num==7)
				printf("");
			else printf("%s ",str);
		}
	}

	printf("\n");
	passOne();


}
Esempio n. 10
0
int arraylist_index_of(const arraylist* array, const void* object)
{
  int length = arraylist_size(array);
  int index;
 
  for (index = 0; index < length; index++)
    {
      if ((*array->equals)(arraylist_get(array, index), object))
	{
	  return index;
	}
    }
  return -1;
}
Esempio n. 11
0
int arraylist_index_of(const Arraylist list, const Object object)
{
  int length = arraylist_size(list);
  int index;

  for (index = 0; index < length; index++)
    {
      if ((*list->_equals)(arraylist_get(list, index), object))
	{
	  return index;
	}
    }
  return -1;
}
Esempio n. 12
0
boolean arraylist_add(arraylist* array, void* object)
{
  int old_size = arraylist_size(array);
  int new_capacity;
  void**new_data;
 
  (array->size)++;
  if (old_size == array->current_capacity)
    {
      new_capacity = array->current_capacity + ARRAYLIST_CAPACITY_DELTA;
#ifdef GOK_DEBUG
      new_data = malloc(objectsize * new_capacity);
#else
      new_data = checked_malloc(object_size * new_capacity);
#endif
      memcpy(new_data, array->data, object_size * old_size);
      free(array->data);
      (array->data) = new_data;
      array->current_capacity = new_capacity;
    }
  (array->data)[old_size] = object;
  return TRUE;
}
Esempio n. 13
0
Boolean arraylist_add(const Arraylist list, Object object)
{
  int old_size = arraylist_size(list);
  int new_capacity;
  Object *new_data;

  (list->_size)++;
  if (old_size == list->_current_capacity)
    {
      new_capacity = list->_current_capacity + ARRAYLIST_CAPACITY_DELTA;
#ifdef GOK_DEBUG
      new_data = malloc(object_size * new_capacity);
#else
      new_data = checked_malloc(object_size * new_capacity);
#endif
      memcpy(new_data, list->_data, object_size * old_size);
      free(list->_data);
      (list->_data) = new_data;
      list->_current_capacity = new_capacity;
    }
  (list->_data)[old_size] = object;
  return TRUE;
}
Esempio n. 14
0
boolean arraylist_is_empty(const arraylist* array)
{
  return (0 == arraylist_size(array));
}
Esempio n. 15
0
Boolean arraylist_is_empty(const Arraylist list)
{
  return (0 == arraylist_size(list));
}
Esempio n. 16
0
File: map.c Progetto: janisz/unix
int getRandomRoom(Map *m)
{
	return *((int*)arraylist_get(m->rooms, rand() % arraylist_size(m->rooms)));
}
Esempio n. 17
0
void
test_arraylist (int initial_size, int sorted, int allow_duplicates)
{
    struct t_arraylist *arraylist;
    int i, index, index_insert;
    void *pointer;
    const char *item_aaa = "aaa";
    const char *item_abc = "abc";
    const char *item_DEF = "DEF";
    const char *item_Def = "Def";
    const char *item_def = "def";
    const char *item_xxx = "xxx";
    const char *item_zzz = "zzz";

    /* create arraylist */
    arraylist = arraylist_new (initial_size,
                               sorted,
                               allow_duplicates,
                               &test_cmp_cb, NULL,
                               NULL, NULL);

    /* check values after creation */
    CHECK(arraylist);
    LONGS_EQUAL(0, arraylist->size);
    LONGS_EQUAL(initial_size, arraylist->size_alloc);
    LONGS_EQUAL(initial_size, arraylist->size_alloc_min);
    if (initial_size > 0)
    {
        CHECK(arraylist->data);
        for (i = 0; i < initial_size; i++)
        {
            POINTERS_EQUAL(NULL, arraylist->data[i]);
        }
    }
    else
    {
        POINTERS_EQUAL(NULL, arraylist->data);
    }
    LONGS_EQUAL(sorted, arraylist->sorted);
    LONGS_EQUAL(allow_duplicates, arraylist->allow_duplicates);

    /* check size */
    LONGS_EQUAL(0, arraylist_size (arraylist));

    /* get element (this should always fail, the list is empty!) */
    POINTERS_EQUAL(NULL, arraylist_get (NULL, -1));
    POINTERS_EQUAL(NULL, arraylist_get (NULL, 0));
    POINTERS_EQUAL(NULL, arraylist_get (NULL, 1));
    POINTERS_EQUAL(NULL, arraylist_get (arraylist, -1));
    POINTERS_EQUAL(NULL, arraylist_get (arraylist, 0));
    POINTERS_EQUAL(NULL, arraylist_get (arraylist, 1));

    /* search element (this should always fail, the list is empty!) */
    POINTERS_EQUAL(NULL, arraylist_search (NULL, NULL, NULL, NULL));
    POINTERS_EQUAL(NULL, arraylist_search (arraylist, NULL, NULL, NULL));
    POINTERS_EQUAL(NULL,
                   arraylist_search (NULL, (void *)item_abc, NULL, NULL));
    POINTERS_EQUAL(NULL,
                   arraylist_search (arraylist, (void *)item_abc, NULL, NULL));

    /* invalid add of element */
    LONGS_EQUAL(-1, arraylist_add (NULL, NULL));

    /* add some elements */
    if (sorted)
    {
        TEST_ARRAYLIST_ADD(0, item_zzz);
        TEST_ARRAYLIST_ADD(0, item_xxx);
        TEST_ARRAYLIST_ADD(0, NULL);
        TEST_ARRAYLIST_ADD(1, item_DEF);
        TEST_ARRAYLIST_ADD((allow_duplicates) ? 2 : 1, item_def);
        TEST_ARRAYLIST_ADD((allow_duplicates) ? 3 : 1, item_Def);
        TEST_ARRAYLIST_ADD(1, item_abc);
    }
    else
    {
        TEST_ARRAYLIST_ADD(0, item_zzz);
        TEST_ARRAYLIST_ADD(1, item_xxx);
        TEST_ARRAYLIST_ADD(2, NULL);
        TEST_ARRAYLIST_ADD(3, item_DEF);
        TEST_ARRAYLIST_ADD((allow_duplicates) ? 4 : 3, item_def);
        TEST_ARRAYLIST_ADD((allow_duplicates) ? 5 : 3, item_Def);
        TEST_ARRAYLIST_ADD((allow_duplicates) ? 6 : 4, item_abc);
    }

    /*
     * arraylist is now:
     *   sorted:
     *     dup   : [NULL, "abc", "DEF", "def", "Def", "xxx", "zzz"] + 2 NULL
     *     no dup: [NULL, "abc", "Def", "xxx", "zzz"] + 1 NULL
     *   not sorted:
     *     dup   : ["zzz", "xxx", NULL, "DEF", "def", "Def", "abc"] + 2 NULL
     *     no dup: ["zzz", "xxx", NULL, "Def", "abc"] + 1 NULL
     */

    /* check size after adds */
    LONGS_EQUAL((allow_duplicates) ? 7 : 5, arraylist->size);
    LONGS_EQUAL((allow_duplicates) ? 7 : 5, arraylist_size (arraylist));
    LONGS_EQUAL((allow_duplicates) ? 9 : 6, arraylist->size_alloc);

    /* check content after adds */
    if (sorted)
    {
        if (allow_duplicates)
        {
            POINTERS_EQUAL(NULL, arraylist->data[0]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_DEF, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[4]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[5]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[6]);
            for (i = 7; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            POINTERS_EQUAL(NULL, arraylist->data[0]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[4]);
            for (i = 5; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }
    else
    {
        if (allow_duplicates)
        {
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[1]);
            POINTERS_EQUAL(NULL, arraylist->data[2]);
            STRCMP_EQUAL(item_DEF, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[4]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[5]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[6]);
            for (i = 7; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[1]);
            POINTERS_EQUAL(NULL, arraylist->data[2]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[4]);
            for (i = 5; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }

    /* search elements */
    if (sorted)
    {
        if (allow_duplicates)
        {
            TEST_ARRAYLIST_SEARCH(NULL, 0, 1, NULL);
            TEST_ARRAYLIST_SEARCH(item_abc, 1, 2, item_abc);
            TEST_ARRAYLIST_SEARCH(item_DEF, 2, 5, item_DEF);
            TEST_ARRAYLIST_SEARCH(item_DEF, 2, 5, item_def);
            TEST_ARRAYLIST_SEARCH(item_DEF, 2, 5, item_Def);
            TEST_ARRAYLIST_SEARCH(item_xxx, 5, 6, item_xxx);
            TEST_ARRAYLIST_SEARCH(item_zzz, 6, 7, item_zzz);
        }
        else
        {
            TEST_ARRAYLIST_SEARCH(NULL, 0, 1, NULL);
            TEST_ARRAYLIST_SEARCH(item_abc, 1, 2, item_abc);
            TEST_ARRAYLIST_SEARCH(item_Def, 2, 3, item_DEF);
            TEST_ARRAYLIST_SEARCH(item_Def, 2, 3, item_def);
            TEST_ARRAYLIST_SEARCH(item_Def, 2, 3, item_Def);
            TEST_ARRAYLIST_SEARCH(item_xxx, 3, 4, item_xxx);
            TEST_ARRAYLIST_SEARCH(item_zzz, 4, 5, item_zzz);
        }

        /* search non-existing element */
        TEST_ARRAYLIST_SEARCH(NULL, -1, 1, item_aaa);
    }
    else
    {
        if (allow_duplicates)
        {
            TEST_ARRAYLIST_SEARCH(item_zzz, 0, -1, item_zzz);
            TEST_ARRAYLIST_SEARCH(item_xxx, 1, -1, item_xxx);
            TEST_ARRAYLIST_SEARCH(NULL, 2, -1, NULL);
            TEST_ARRAYLIST_SEARCH(item_DEF, 3, -1, item_DEF);
            TEST_ARRAYLIST_SEARCH(item_DEF, 3, -1, item_def);
            TEST_ARRAYLIST_SEARCH(item_DEF, 3, -1, item_Def);
            TEST_ARRAYLIST_SEARCH(item_abc, 6, -1, item_abc);
        }
        else
        {
            TEST_ARRAYLIST_SEARCH(item_zzz, 0, -1, item_zzz);
            TEST_ARRAYLIST_SEARCH(item_xxx, 1, -1, item_xxx);
            TEST_ARRAYLIST_SEARCH(NULL, 2, -1, NULL);
            TEST_ARRAYLIST_SEARCH(item_Def, 3, -1, item_DEF);
            TEST_ARRAYLIST_SEARCH(item_Def, 3, -1, item_def);
            TEST_ARRAYLIST_SEARCH(item_Def, 3, -1, item_Def);
            TEST_ARRAYLIST_SEARCH(item_abc, 4, -1, item_abc);
        }

        /* search non-existing element */
        TEST_ARRAYLIST_SEARCH(NULL, -1, -1, item_aaa);
    }

    /* invalid remove of elements */
    LONGS_EQUAL(-1, arraylist_remove (NULL, -1));
    LONGS_EQUAL(-1, arraylist_remove (arraylist, -1));
    LONGS_EQUAL(-1, arraylist_remove (NULL, 0));

    /* remove the 3 first elements and check size after each remove */
    LONGS_EQUAL(0, arraylist_remove (arraylist, 0));
    LONGS_EQUAL((allow_duplicates) ? 6 : 4, arraylist->size);
    LONGS_EQUAL((allow_duplicates) ? 6 : 4, arraylist_size (arraylist));
    LONGS_EQUAL((allow_duplicates) ? 9 : 6, arraylist->size_alloc);
    LONGS_EQUAL(0, arraylist_remove (arraylist, 0));
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist->size);
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist_size (arraylist));
    LONGS_EQUAL((allow_duplicates) ? 9 : 6, arraylist->size_alloc);
    LONGS_EQUAL(0, arraylist_remove (arraylist, 0));
    LONGS_EQUAL((allow_duplicates) ? 4 : 2, arraylist->size);
    LONGS_EQUAL((allow_duplicates) ? 4 : 2, arraylist_size (arraylist));
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist->size_alloc);

    /*
     * arraylist is now:
     *   sorted:
     *     dup   : ["def", "Def", "xxx", "zzz"] + 1 NULL
     *     no dup: ["xxx", "zzz"] + 1 NULL
     *   not sorted:
     *     dup   : ["DEF", "def", "Def", "abc"] + 1 NULL
     *     no dup: ["Def", "abc"] + 1 NULL
     */

    /* check content after the 3 deletions */
    if (sorted)
    {
        if (allow_duplicates)
        {
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[3]);
            for (i = 4; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[1]);
            for (i = 2; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }
    else
    {
        if (allow_duplicates)
        {
            STRCMP_EQUAL(item_DEF, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[3]);
            for (i = 4; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[1]);
            for (i = 2; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }

    /* invalid insert of element */
    LONGS_EQUAL(-1, arraylist_insert (NULL, 0, NULL));

    /* insert of one element */
    LONGS_EQUAL(0, arraylist_insert (arraylist, 0, (void *)item_aaa));

    /*
     * arraylist is now:
     *   sorted:
     *     dup   : ["aaa", "def", "Def", "xxx", "zzz"]
     *     no dup: ["aaa", "xxx", "zzz"]
     *   not sorted:
     *     dup   : ["aaa", "DEF", "def", "Def", "abc"]
     *     no dup: ["aaa", "Def", "abc"]
     */

    /* check size after insert */
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist->size);
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist_size (arraylist));
    LONGS_EQUAL((allow_duplicates) ? 5 : 3, arraylist->size_alloc);

    /* check content after the insert */
    if (sorted)
    {
        if (allow_duplicates)
        {
            STRCMP_EQUAL(item_aaa, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[4]);
            for (i = 5; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            STRCMP_EQUAL(item_aaa, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_xxx, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_zzz, (const char *)arraylist->data[2]);
            for (i = 3; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }
    else
    {
        if (allow_duplicates)
        {
            STRCMP_EQUAL(item_aaa, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_DEF, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_def, (const char *)arraylist->data[2]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[3]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[4]);
            for (i = 5; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
        else
        {
            STRCMP_EQUAL(item_aaa, (const char *)arraylist->data[0]);
            STRCMP_EQUAL(item_Def, (const char *)arraylist->data[1]);
            STRCMP_EQUAL(item_abc, (const char *)arraylist->data[2]);
            for (i = 3; i < arraylist->size_alloc; i++)
            {
                POINTERS_EQUAL(NULL, arraylist->data[i]);
            }
        }
    }

    /* clear arraylist */
    LONGS_EQUAL(0, arraylist_clear (NULL));
    LONGS_EQUAL(1, arraylist_clear (arraylist));

    /* check size and data after clear */
    LONGS_EQUAL(0, arraylist->size);
    LONGS_EQUAL(0, arraylist_size (arraylist));
    LONGS_EQUAL(initial_size, arraylist->size_alloc);
    if (initial_size > 0)
    {
        CHECK(arraylist->data);
        for (i = 0; i < initial_size; i++)
        {
            POINTERS_EQUAL(NULL, arraylist->data[i]);
        }
    }
    else
    {
        POINTERS_EQUAL(NULL, arraylist->data);
    }

    /* free arraylist */
    arraylist_free (arraylist);
}
Esempio n. 18
0
int main( int argc, char *argv[] )
{
  /* for parsing args */
  extern char *optarg;
  extern int optind;
  int ch;

  /* vars */
  int i,j;
  int listenfd;

  /* select vars */
  fd_set read_set;
  int fdmax;

  /* client arr */
  Arraylist clientList;
  /* channel arr */
  Arraylist channelList;

  /* servername */
  char servername[MAX_SERVERNAME+1];

  while ((ch = getopt(argc, argv, "hD:")) != -1)
  switch (ch) {
  case 'D':
    if (set_debug(optarg)) {
      exit(0);
    }
    break;
  case 'h':
  default: /* FALLTHROUGH */
    usage();
  }
  argc -= optind;
  argv += optind;

  if (argc < 2) {
    usage();
  }
  signal(SIGPIPE, SIG_IGN);
  init_node(argv[0], argv[1]);

  printf( "I am node %lu and I listen on port %d for new users\n", curr_nodeID, curr_node_config_entry->irc_port );

  /* Start your engines here! */
  if (gethostname(servername,MAX_SERVERNAME) < 0){
    perror("gethostname");
    return EXIT_FAILURE;
  }
  servername[MAX_SERVERNAME] = '\0';

  /* delegate all function calling to setupListenSocket.
        It should print out relevant err messages. */
  listenfd = setupListenSocket(curr_node_config_entry->irc_port);
  if (listenfd < 0){
    return EXIT_FAILURE;
  }

  /* initialize client array */
  clientList = arraylist_create();

  /* initialize channel array */
  channelList = arraylist_create();

  /* prepare for select */
  fdmax = listenfd;
  FD_ZERO(&master_set);
  FD_ZERO(&write_set);
  FD_SET(listenfd,&master_set);

  /* FD_ZERO(&write_set);  initially no data to write */

  /* main loop!! */
  for (;;){
    int retval;
    read_set = master_set;

    /* wait until any sockets become available */
    retval = select(fdmax+1,&read_set,&write_set,NULL,NULL);

    if (retval <= 0){
      if (retval < 0)
      DEBUG_PERROR("select");
      continue; /* handle errors gracefully and wait again if timed out (it shouldn't)*/
    }
    /* at least one socket ready*/
    for (i = 0; i <= fdmax; i++){
      if (FD_ISSET(i, &write_set)) {
        int listIndex = findClientIndexBySockFD(clientList,i);
        client_t *thisClient = (client_t *) CLIENT_GET(clientList,listIndex);
        /*client data ready to be written */
        int nbytes;
        /* iterate through item to be sent, until it will block */
        Arraylist outbuf = thisClient->outbuf;

        while (!arraylist_is_empty(outbuf))
        {
          /* write */
          char *dataToSend = (char *) (arraylist_get(outbuf,0)) + thisClient->outbuf_offset;
          size_t sizeToSend = strlen(dataToSend);
          nbytes = send(thisClient->sock, dataToSend, sizeToSend, 0);
          if (nbytes <0){
            /* error */
            if (errno == EPIPE || errno == ECONNRESET){
              /* connection lost or closed by the peer */

              DPRINTF(DEBUG_SOCKETS,"send: client %d hungup\n",i);
              remove_client(clientList,listIndex);
              continue;
            }
          }
          else if (nbytes == sizeToSend){
            /* current line completely sent */
            char *toRemove = (char *) (arraylist_get(outbuf,0));
            arraylist_removeIndex(outbuf,0);
            free(toRemove);
          }
          else{
            /* partial send */
            thisClient->outbuf_offset += nbytes;
            break;
          }
        }
        if (arraylist_is_empty(outbuf))
          FD_CLR(i, &write_set);
      }
      if (FD_ISSET(i, &read_set)) {
        if (i == listenfd){
          /* incoming connection */
          int newindex = handle_incoming_conn(clientList,servername,listenfd);
          if (newindex < 0){
            continue;
          }
          client_t *newClient = CLIENT_GET(clientList,newindex);
          int newfd = newClient->sock;
          FD_SET(newfd,&master_set);
          if (newfd > fdmax){
            fdmax = newfd;
          }
        }
        else{
          /* client data ready */
          char *tempPtr;
          int listIndex = findClientIndexBySockFD(clientList,i);

          /* for split function */
          char** tokenArr;
          int numToken;
          int lastTokenTerminated;

          if (listIndex < 0){
            close(i);
            FD_CLR(i,&master_set);
            continue;
          }

          int nbytes = recv(i, CLIENT_GET(clientList,listIndex)->inbuf + CLIENT_GET(clientList,listIndex)->inbuf_size,
          MAX_MSG_LEN - CLIENT_GET(clientList,listIndex)->inbuf_size, 0);

          /* recv failed. Either client left or error */
          if (nbytes <= 0){
            if (nbytes == 0){
              DPRINTF(DEBUG_SOCKETS,"recv: client %d hungup\n",i);
              remove_client(clientList, listIndex);

            }
            else if (errno == ECONNRESET || errno == EPIPE){
              DPRINTF(DEBUG_SOCKETS,"recv: client %d connection reset \n",i);
              remove_client(clientList, listIndex);

            }
            else{
              perror("recv");
            }
            continue;
          }

          /* NULL terminate to use strpbrk */
          CLIENT_GET(clientList,listIndex)->inbuf_size += nbytes;
          CLIENT_GET(clientList,listIndex)->inbuf[CLIENT_GET(clientList,listIndex)->inbuf_size] = '\0';
          tempPtr = strpbrk(CLIENT_GET(clientList,listIndex)->inbuf,"\r\n");
          if (!tempPtr){
            if (CLIENT_GET(clientList,listIndex)->inbuf_size == MAX_MSG_LEN){
              /* Message too long. Dump the content */
              DPRINTF(DEBUG_INPUT,"recv: message longer than MAX_MESSAGE detected. The message will be discarded\n");
              CLIENT_GET(clientList,listIndex)->inbuf_size = 0;
            }
            continue;
          }

          tokenArr = splitByDelimStr(CLIENT_GET(clientList,listIndex)->inbuf,"\r\n",&numToken,&lastTokenTerminated);
          /* since we have checked if there's delimeter beforehand, there should be at least one terminated token available*/
          if (!tokenArr){
            DPRINTF(DEBUG_INPUT,"splitByDelimStr: failed to split inputToken\n");
            CLIENT_GET(clientList,listIndex)->inbuf_size = 0;
            continue;
          }
          if (!lastTokenTerminated){
            CLIENT_GET(clientList,listIndex)->inbuf_size = strlen(tokenArr[numToken-1]);
            memcpy(CLIENT_GET(clientList,listIndex)->inbuf,tokenArr[numToken-1],CLIENT_GET(clientList,listIndex)->inbuf_size);
            numToken--;
          }

          for (j=0;j<numToken;j++){
            handle_line(clientList,listIndex,channelList,servername,tokenArr[j]);
          }

          for (j = 0; j < arraylist_size(clientList); j++){
            client_t *client = CLIENT_GET(clientList,j);
            if (arraylist_size(client->outbuf)){
                FD_SET(client->sock,&write_set);
            }
          }
          freeTokens(&tokenArr,numToken);
        }
      }
    }
  }

  return 0;
}
Esempio n. 19
0
File: map.c Progetto: janisz/unix
void printRooms(Map *map)
{
	for (int i=0; i<arraylist_size(map->rooms); i++) {
		printf("#%d\t%d\n", i, *((int*)arraylist_get(map->rooms, i)));
	}
}