int list_remove_next(linkedList *list, listElement *element, void **data) { listElement* oldElement; /* Can't remove from an empty list */ if ( list_get_size(list) == 0 ) { printf("list_remove_next : cannot remove element from empty list.\n"); return -1; } /* Continue to remove element from list */ if ( element == NULL ) { /* Remove element at head of list */ *data = list->head->data; oldElement = list->head; list->head = list->head->next; if ( list_get_size(list) == 1 ) { list->tail = NULL; } } else { /* Remove element from anywhere else in the list */ if ( element->next == NULL ) { /*If there is no element after the current one, fail */ printf("list_remove_next : no element after tail\n"); return -1; } *data = element->data; oldElement = element->next; element->next = element->next->next; if ( element->next == NULL ) { /* Setting new tail of list */ list->tail = element; } } /* Free the storage which was allocated in the list for old element. */ free(oldElement); list->size--; return 0; }
t_bool list_add_elem_at_position(t_list *front_ptr, void *elem, unsigned int position) { t_list new_elem; t_list tmp; unsigned int i; i = 0; tmp = *front_ptr; if ((new_elem = malloc(sizeof(t_node))) == NULL || position > list_get_size(*front_ptr)) return (FALSE); if (*front_ptr == NULL || position == 0) return (list_add_elem_at_front(front_ptr, elem)); else { new_elem->value = elem; while (tmp->next && i < position - 1) { tmp = tmp->next; i++; } new_elem->next = tmp->next; tmp->next = new_elem; return (TRUE); } return (FALSE); }
unsigned int list_get_size(t_list list) { if (list == NULL) return (0); else return (1 + list_get_size(list->next)); }
static int send_broadcast(t_server *server, t_player *player, t_list tmp, char *params) { t_player *tmp_player; int tile; char buffer[4096]; unsigned int i; i = -1; while (++i < list_get_size(tmp)) { if ((tmp_player = list_get_elem_at_position(tmp, i)) != NULL && tmp_player->sock != player->sock) { tile = determine_best_way(server, player, tmp_player); if (memset(buffer, 0, 1024) == NULL || snprintf(buffer, 1024, BROADCAST, tile, params) == -1) return (-1); if (store_answer_p(tmp_player, strdup(buffer), 0) == -1) return (-1); } } return (pbc(server, player)); }
void list_print(list_t list){ int size = list_get_size(list); for(int i =0;i<size;i++) film_print(list->filmArray[i]); }
int check_and_write_players(fd_set *writef, t_list players, unsigned int i) { t_player *player; int ret; char *buff; while (++i < list_get_size(players)) { if ((player = list_get_elem_at_position(players, i)) != NULL) { if (FD_ISSET(player->sock, writef) && player->buff != NULL) { buff = player->buff; if ((ret = write(player->sock, buff, strlen(buff))) == -1) { player->off = 1; return (fprintf(stderr, ERR_ANSWER), 0); } else if ((unsigned int)ret != strlen(buff)) fprintf(stderr, ERR_WRITE); free(player->buff); player->buff = NULL; } } } return (0); }
int init_teams_max_players(t_list teams, int const max_players) { unsigned int i; t_team *tmp_team; i = 0; while (i < list_get_size(teams)) { if ((tmp_team = list_get_elem_at_position(teams, i)) != NULL) { if (strlen(tmp_team->name) > 512) { fprintf(stderr, ERR_TEAM_NAME); return (-1); } tmp_team->max_players = max_players; } i++; } if (i <= 1) { fprintf(stderr, ERR_NBTEAMS); return (-1); } return (0); }
int free_clients(t_list list, int const type, int const ret_value) { unsigned int i; t_list tmp; if (!list) return (ret_value); i = -1; while (++i < list_get_size(list)) { tmp = list; list = list->next; if (type == 0) free_client_buffers(tmp->value); else { free_player_buffers(tmp->value); free_player_structure(tmp->value); } free(tmp->value); tmp->value = NULL; free(tmp); tmp = NULL; } free(list); list = NULL; return (ret_value); }
void free_player_structure(t_player *player) { unsigned int i; t_task *task; i = -1; if (player == NULL) return ; while (++i < list_get_size(player->queue_tasks)) { if ((task = list_get_elem_at_position(player->queue_tasks, i)) != NULL) { if (task->cmd) free(task->cmd); if (task->params) free(task->params); if (task->real) free(task->real); task->cmd = NULL; task->params = NULL; task->real = NULL; free(task); } } }
int check_and_read_clients(fd_set *readf, t_list list, unsigned int i) { t_client *client; int ret; while (++i < list_get_size(list)) { if ((client = list_get_elem_at_position(list, i)) != NULL) { client->buffs.cmds ? free_content(client->buffs.cmds) : 0; client->buffs.cmds ? client->buffs.cmds->destroy(client->buffs.cmds) : 0; client->buffs.cmds = NULL; if (FD_ISSET(client->sock, readf)) { if ((ret = get_cmd_buff(client->sock, &client->buffs)) == -1) return (fprintf(stderr, ERR_COMMAND), -1); else if (ret == -2) { store_answer_c(client, strdup(RPL_OVERFLOW), 0); fprintf(stderr, ERR_OVERFLOW); } } } } return (0); }
int check_and_write_clients(fd_set *writef, t_list clients, unsigned int i) { t_client *client; int ret; char *buff; while (++i < list_get_size(clients)) { if ((client = list_get_elem_at_position(clients, i)) != NULL) { if (FD_ISSET(client->sock, writef) && client->buff != NULL) { buff = client->buff; if ((ret = write(client->sock, buff, strlen(buff))) == -1) { client->off = 1; return (fprintf(stderr, ERR_ANSWER), 0); } else if ((unsigned int)ret != strlen(buff)) fprintf(stderr, ERR_WRITE); free(client->buff); client->buff = NULL; } } } return (0); }
void videoPlayer_print(videoPlayer_t * vidPlayer) { int maxSize = list_get_size(vidPlayer->list); for(int i =0;i<maxSize;i++) { film_t * film = list_get_element(vidPlayer->list,i); film_print(film); } }
static void dir_ctor(telf_ctx *ctx, telf_obj *obj, telf_dir_hdl *dir) { dir->ctx = ctx; dir->cursor = 0; dir->obj = obj; dir->n_entries = list_get_size(obj->entries) + 2; // for "." and ".." dir->get_entryname_func = direntname; }
telf_status libfs_readlink(void *obj_hdl, char **bufp, size_t *buf_lenp) { telf_obj *obj = obj_hdl; telf_status ret; Elf64_Shdr *shdr = NULL; char path[PATH_MAX]; char *buf = NULL; size_t buf_len = 0; int i; elf_obj_lock(obj); for (i = 0; i < list_get_size(obj->ctx->libpath); i++) { char *lp = list_get_nth(obj->ctx->libpath, i); char path[PATH_MAX] = ""; snprintf(path, sizeof path, "%s/%s", lp, obj->name); if (-1 == access(path, R_OK)) { if (ENOENT != errno) ERR("access: %s", strerror(errno)); continue; } buf = strdup(path); if (! buf) { ERR("malloc: %s", strerror(errno)); ret = ELF_ENOMEM; goto end; } buf_len = strlen(buf); break; } ret = ELF_SUCCESS; end: elf_obj_unlock(obj); if (bufp) *bufp = buf; else free(buf); if (buf_lenp) *buf_lenp = buf_len; return ret; }
/* listPrintHistory: formatted print of linked list required for reverse and number printing Parameters: - node*: a pointer to the head of a linked list */ void listPrintHistory(node* list, int count) { node* current = list; int size = list_get_size(list); while(current != NULL) { if(size <= count){ printf("%d:\t%s\n", size, current->data); } size--; current = current->next; } }
void gg_container_destroy(gg_widget_t *widget) { gg_container_t *container = GG_CONTAINER(widget); int i; for (i = 0; i < list_get_size(container->widget_list); i++) { gg_widget_t *item = GG_WIDGET(list_get_item(container->widget_list, i)); item->destroy(item); } list_destroy(container->widget_list); gg_widget_destroy(widget); }
TEST(INSERTION, IN_HEAD) { list_t* list = list_create(); // Inserting elements in head. node_t* foo = list_push_front(list, (void*) "foo"); node_t* bar = list_push_front(list, (void*) "bar"); EXPECT_EQ(list_get_size(list), 2); EXPECT_STREQ((const char*) foo->element, "foo"); EXPECT_STREQ((const char*) bar->element, "bar"); EXPECT_EQ(list->head, bar); EXPECT_EQ(list->tail, foo); list_destroy(list); }
t_server *get_current_server() { t_server *server; int size; size = (int)list_get_size(server_list) - 1; while (size >= 0) { if ((server = GET_AT(server_list, size)) != NULL) if (server->current == 1) return (server); --size; } return (NULL); }
int get_currents_index(int *srv, int *chn) { t_server *server; int size; int size2; t_channel *chan; size = (int)list_get_size(server_list); while (--size >= 0) if ((server = GET_AT(server_list, size)) != NULL) if (server->current == 1) { *srv = size; size2 = (int)list_get_size(server->channel_list); while (--size2 >= 0) if ((chan = (t_channel*)GET_AT(server->channel_list, size2)) != NULL) if (chan->current == 1) { *chn = size2; return (0); } } return (-1); }
void free_nodes_only(t_list list) { t_list tmp; unsigned int i; if (list == NULL) return ; i = -1; while (++i < list_get_size(list)) { tmp = list; list = list->next; free(tmp); } }
void process_fd(fd_set *fds) { int size; t_server *server; if (FD_ISSET(0, fds)) handle_write(); size = (int)list_get_size(server_list) - 1; while (size >= 0) { if ((server = (t_server*)GET_AT(server_list, size)) != NULL) if (FD_ISSET(server->sockfd, fds)) handle_input(server); --size; } }
void remove_server(t_list *list, t_server *server) { int size; t_server *srv; size = (int)list_get_size(*list) - 1; while (size >= 0) { if ((srv = (t_server*)GET_AT(*list, size)) != NULL) if (server == srv) { list_del_elem_at_position(list, (unsigned int)size); remove_all_channel(srv); free(srv); return ; } --size; } }
void list_destroy(linkedList *list) { void * data; /* Loop to remove each element from the list */ while (list_get_size(list) > 0) { if (list_remove_next(list, NULL, (void**)&data) == 0 && list->destroy != NULL) { /* Call the function that the user defines for destroy */ list->destroy(data); } } /* No more operations can be performed on this list now. As a * precaution, the structure should be cleared anyway. */ memset(list, 0, sizeof(linkedList)); return; }
int register_servers_fd(t_list list, fd_set *fds) { int size; t_server *server; int max; size = (int)list_get_size(list) - 1; max = -1; while (size >= 0) { if ((server = (t_server*)GET_AT(list, size)) != NULL) { FD_SET(server->sockfd, fds); if (server->sockfd > max) max = server->sockfd; } --size; } return (max); }
int send_all_graphics(t_server *srv, char *msg) { t_client *tmp; unsigned int i; i = 0; if (srv == NULL || msg == NULL) return (-1); while (i < list_get_size(srv->graphic_clients)) { if ((tmp = list_get_elem_at_position(srv->graphic_clients, i)) != NULL) { if (store_answer_c(tmp, strdup(msg), 0) == -1) return (fprintf(stderr, ERR_BUFFER), -1); } i++; } free(msg); return (0); }
int list_insert_next(linkedList *list, listElement *element, const void *data) { listElement *newElement; /* Attempt to allocate the memory for the new element */ if ((newElement = (listElement *)malloc(sizeof(listElement))) == NULL) { printf("list_insert_next : failed to create new element\n"); return -1; } newElement->data = (void*)data; if (element == NULL) { /* Add the new element at the HEAD of the list */ if (list_get_size(list) == 0) { list->tail = newElement; } newElement->next = list->head; list->head=newElement; } else { if (element->next == NULL) { list->tail = newElement; } newElement->next = element->next; element->next = newElement; } list->size++; return 0; }
TEST(INSERTION, MIXING_TAIL_AND_HEAD) { list_t* list = list_create(); char* baz = strdup("baz"); char* foo = strdup("foo"); // Inserting 1000 elements in head. for (size_t i = 0; i < 1000; ++i) { list_push_front(list, baz); } // Inserting 1000 elements in tail. for (size_t i = 0; i < 1000; ++i) { list_push_back(list, foo); } EXPECT_EQ(list_get_size(list), (1000 * 2)); EXPECT_STREQ((const char*) list->head->element, baz); EXPECT_STREQ((const char*) list->tail->element, foo); free(baz); free(foo); list_destroy(list); }
int send_all_players(t_server *srv, t_player *p, char *msg) { t_player *tmp; unsigned int i; i = 0; if (srv == NULL || msg == NULL) return (-1); while (i < list_get_size(srv->all_players)) { if ((tmp = list_get_elem_at_position(srv->all_players, i)) != NULL) { if (p == NULL && store_answer_p(tmp, strdup(msg), 0) == -1) return (fprintf(stderr, ERR_BUFFER), -1); else if (tmp->sock != p->sock && store_answer_p(tmp, strdup(msg), 0) == -1) return (fprintf(stderr, ERR_BUFFER), -1); } i++; } free(msg); return (0); }
static int waiting_list(t_server *srv) { struct timeb now; unsigned int i; t_client *cl; i = 0; ftime(&now); while (i < list_get_size(srv->queue_clients)) { if ((cl = list_get_elem_at_position(srv->queue_clients, i)) != NULL) { if (calculate_elapse(&cl->timer.val, &now) >= 10.0) { if (close(cl->sock) == -1) return (-1); list_del_elem_at_position(&srv->queue_clients, i); return (0); } } i++; } return (0); }
void *list_get_elem_at_position(t_list list, unsigned int position) { t_list tmp; unsigned int i; i = 0; tmp = list; if (list == NULL) return (NULL); if (position == 0) { return (list_get_elem_at_front(list)); } if (position > list_get_size(list)) return (NULL); while (tmp->next && i < position) { tmp = tmp->next; i++; } if (tmp != NULL) return (tmp->value); return (NULL); }