void user_login(struct plugin_handle* plugin, struct plugin_user* user) { struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct cbuffer* buf = NULL; struct linked_list* found = (struct linked_list*) list_create(); sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", (int) data->history_connect); if (data->history_connect > 0 && list_size(found) > 0) { buf = cbuf_create(MAX_HISTORY_SIZE); cbuf_append(buf, "Chat history:\n\n"); struct chat_history_line* history_line; history_line = (struct chat_history_line*) list_get_last(found); while (history_line) { cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message); list_remove(found, history_line); hub_free(history_line); history_line = (struct chat_history_line*) list_get_last(found); } plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); } list_clear(found, &hub_free); list_destroy(found); }
main() { DynamicList* l = list_inicialize(); /*Aluno* Al1 = (Aluno*)malloc(sizeof(Aluno)); Aluno* Al2 = (Aluno*)malloc(sizeof(Aluno)); Al1->RA = 1516; Al1->nome = "JOAO"; Al2->RA = 2324; Al2->nome = "SIBA"; */ printf("\nInseriu? %s", list_add_last(l, 'a')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'b')?"sim":"não"); printf("\nFirst %c\n", list_get_first(l)); printf("\nLast %c\n", list_get_last(l)); printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não"); printf("\nFirst %c\n", list_get_first(l)); printf("\nLast %c\n", list_get_last(l)); list_print(l); printf("\nSize %d\n", list_size(l)); list_clear(l); printf("\nSize %d\n", list_size(l)); printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não"); list_print(l); list_remove_first(l); list_remove_first(l); list_remove_first(l); list_print(l); }
static void test_GetLast(void) { const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}}; const int nr_elements = sizeof(data)/sizeof(data[0]); TEST_ASSERT_NULL(list_get_last(mylist)); for(int i = 0; i < nr_elements; i++) { TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i]))); test_struct* ptr = list_get_last(mylist); TEST_ASSERT_NOT_NULL(ptr); TEST_ASSERT_EQUAL(0, memcmp(&(data[i]), ptr, sizeof(*ptr))); } }
static struct rb_node* pop(struct rb_tree* tree) { struct rb_node* n = list_get_last(tree->iterator.stack); if (n) list_remove(tree->iterator.stack, n); return n; }
/** get item from specific position */ void* list_get (linked_list* _this, int position) { // list is empty if (_this->size == 0) { //NODE_DBG("LinkedList#get: The list is empty."); return NULL; } else if (position >= _this->size) { // out of bound //NODE_DBG("LinkedList#get: Index out of bound"); return NULL; } // get head item if (position == 0) { return list_get_first(_this); } else if (position+1 == _this->size) { // get tail item return list_get_last(_this); } else { node* node = _this->head; int i = 0; // loop until position while (i < position) { node = node->next; i++; } return node->item; } }
/** * The callback function for handling the !history command. */ static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(MAX_HISTORY_SIZE); struct linked_list* found = (struct linked_list*) list_create(); struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer); int maxlines; if (arg) maxlines = arg->data.integer; else maxlines = data->history_default; sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", maxlines); size_t linecount = list_size(found); if (linecount > 0) { cbuf_append_format(buf, "*** %s: Chat History:\n\n", cmd->prefix); struct chat_history_line* history_line; history_line = (struct chat_history_line*) list_get_last(found); while (history_line) { cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message); list_remove(found, history_line); hub_free(history_line); history_line = (struct chat_history_line*) list_get_last(found); } } else { cbuf_append_format(buf, "*** %s: No messages found.", cmd->prefix); } plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); list_clear(found, &hub_free); list_destroy(found); return 0; }
void test_list(void) { int val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; list me = list_init(sizeof(int)); assert(me); assert(list_size(me) == 0); assert(list_is_empty(me)); for (int i = 0; i < 10; i++) { list_add_first(me, &val[i]); assert(list_size(me) == i + 1); int get = 0; list_get_first(&get, me); assert(get == val[i]); } assert(list_size(me) == 10); assert(!list_is_empty(me)); int get_arr[10] = {0}; list_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; list_get_at(&get, me, i); assert(get == val[9 - i]); assert(get_arr[i] == val[9 - i]); } for (int i = 0; i < 7; i++) { list_remove_last(me); } int trimmed[5] = {0}; list_copy_to_array(trimmed, me); assert(list_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); } int add = 3; list_add_last(me, &add); add = -1; list_add_at(me, 1, &add); add = -2; list_add_last(me, &add); assert(list_size(me) == 6); int get = 0xdeadbeef; list_get_first(&get, me); assert(get == 10); get = 0xdeadbeef; list_get_at(&get, me, 0); assert(get == 10); list_get_at(&get, me, 1); assert(get == -1); list_get_at(&get, me, 2); assert(get == 9); list_get_at(&get, me, 3); assert(get == 8); list_get_at(&get, me, 4); assert(get == 3); list_get_at(&get, me, 5); assert(get == -2); get = 0xdeadbeef; list_get_last(&get, me); assert(get == -2); list_remove_first(me); list_remove_at(me, 2); list_remove_last(me); assert(list_size(me) == 3); get = 345; list_get_first(&get, me); assert(get == -1); list_get_at(&get, me, 1); assert(get == 9); list_get_last(&get, me); assert(get == 3); int set = 12; list_set_first(me, &set); set = 13; list_set_at(me, 1, &set); set = 14; list_set_last(me, &set); int arr[3] = {0}; list_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); set = -5; list_set_at(me, 0, &set); set = -6; list_set_at(me, 1, &set); set = -7; list_set_at(me, 2, &set); list_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7); assert(list_set_at(me, 4, &set) == -EINVAL); assert(list_get_at(&set, me, 4) == -EINVAL); assert(list_remove_at(me, 4) == -EINVAL); assert(list_add_at(me, 5, &set) == -EINVAL); assert(list_set_at(me, -1, &set) == -EINVAL); assert(list_get_at(&set, me, -1) == -EINVAL); assert(list_remove_at(me, -1) == -EINVAL); assert(list_add_at(me, -1, &set) == -EINVAL); list_clear(me); assert(list_size(me) == 0); assert(list_is_empty(me)); assert(list_remove_first(me) == -EINVAL); assert(list_remove_last(me) == -EINVAL); me = list_destroy(me); assert(!me); }
static void test_GetLast_parameters(void) { TEST_ASSERT_NULL(list_get_last(NULL)); }