/* Listbox item adding function */ static inline void listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos) { if (l->list == NULL) { l->list = g_queue_new (); pos = LISTBOX_APPEND_AT_END; } switch (pos) { case LISTBOX_APPEND_AT_END: g_queue_push_tail (l->list, e); break; case LISTBOX_APPEND_BEFORE: g_queue_insert_before (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e); break; case LISTBOX_APPEND_AFTER: g_queue_insert_after (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e); break; case LISTBOX_APPEND_SORTED: g_queue_insert_sorted (l->list, e, (GCompareDataFunc) listbox_entry_cmp, NULL); break; default: break; } }
TEST_F(GQueueTest, peekLink) { int testData1 = 42; int testData2 = 1337; g_queue_push_tail(queue, &testData1); g_queue_push_tail(queue, &testData2); GList *link = g_queue_peek_head_link(queue); ASSERT_EQ(queue->head, link) << "peeking at the head of the queue should produce expected element"; link = g_queue_peek_tail_link(queue); ASSERT_EQ(queue->tail, link) << "peeking at the tail of the queue should produce expected element"; link = g_queue_peek_nth_link(queue, 0); ASSERT_EQ(queue->head, link) << "peeking at the first element of the queue should produce expected element"; link = g_queue_peek_nth_link(queue, 1); ASSERT_EQ(queue->tail, link) << "peeking at the second element of the queue should produce expected element"; }
int main(int argc, char** argv) { GQueue* q = g_queue_new(); g_queue_push_tail(q, "Alice "); g_queue_push_tail(q, "Bob "); g_queue_push_tail(q, "Fred "); g_queue_push_tail(q, "Jim "); printf("Starting out, the queue is: "); g_queue_foreach(q, (GFunc)printf, NULL); GList* fred_link = g_queue_peek_nth_link(q, 2); printf("\nThe link at index 2 contains %s\n", fred_link->data); g_queue_unlink(q, fred_link); g_list_free(fred_link); GList* jim_link = g_queue_peek_nth_link(q, 2); printf("Now index 2 contains %s\n", jim_link->data); g_queue_delete_link(q, jim_link); printf("Now the queue is: "); g_queue_foreach(q, (GFunc)printf, NULL); g_queue_free(q); return 0; }
WLEntry * listbox_get_nth_item (const WListbox * l, int pos) { if (!listbox_is_empty (l) && pos >= 0) { GList *item; item = g_queue_peek_nth_link (l->list, (guint) pos); if (item != NULL) return LENTRY (item->data); } return NULL; }
/** * g_queue_peek_nth: * @queue: a #GQueue * @n: the position of the element. * * Returns the @n'th element of @queue. * * Return value: The data for the @n'th element of @queue, or %NULL if @n is * off the end of @queue. * * Since: 2.4 **/ gpointer g_queue_peek_nth (GQueue *queue, guint n) { GList *link; g_return_val_if_fail (queue != NULL, NULL); link = g_queue_peek_nth_link (queue, n); if (link) return link->data; return NULL; }
/** * g_queue_push_nth: * @queue: a #GQueue * @data: the data for the new element * @n: the position to insert the new element. If @n is negative or * larger than the number of elements in the @queue, the element is * added to the end of the queue. * * Inserts a new element into @queue at the given position * * Since: 2.4 **/ void g_queue_push_nth (GQueue *queue, gpointer data, gint n) { g_return_if_fail (queue != NULL); if (n < 0 || n >= queue->length) { g_queue_push_tail (queue, data); return; } g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data); }
/** * g_queue_pop_nth_link: * @queue: a #GQueue * @n: the link's position * * Removes and returns the link at the given position. * * Return value: The @n'th link, or %NULL if @n is off the end of @queue. * * Since: 2.4 **/ GList* g_queue_pop_nth_link (GQueue *queue, guint n) { GList *link; g_return_val_if_fail (queue != NULL, NULL); if (n >= queue->length) return NULL; link = g_queue_peek_nth_link (queue, n); g_queue_unlink (queue, link); return link; }
static gboolean get_iter_from_index (GtkSourceCompletionModel *model, GtkTreeIter *iter, gint idx) { gint provider_index = 0; GList *l; ProviderInfo *info; if (idx < 0) { return FALSE; } /* Find the provider */ for (l = model->priv->providers; l != NULL; l = l->next) { gint new_index; info = l->data; if (!info->visible) { continue; } new_index = provider_index + info->proposals->length; if (idx < new_index) { break; } provider_index = new_index; } if (l == NULL) { return FALSE; } /* Find the node inside the provider */ iter->user_data = g_queue_peek_nth_link (info->proposals, idx - provider_index); return iter->user_data != NULL; }
/** * g_queue_pop_nth: * @queue: a #GQueue * @n: the position of the element. * * Removes the @n'th element of @queue. * * Return value: the element's data, or %NULL if @n is off the end of @queue. * * Since: 2.4 **/ gpointer g_queue_pop_nth (GQueue *queue, guint n) { GList *nth_link; gpointer result; g_return_val_if_fail (queue != NULL, NULL); if (n >= queue->length) return NULL; nth_link = g_queue_peek_nth_link (queue, n); result = nth_link->data; g_queue_delete_link (queue, nth_link); return result; }
static void lbm_uimflow_get_analysis(capture_file * cfile, seq_analysis_info_t * seq_info) { GList * list = NULL; gchar time_str[COL_MAX_LEN]; register_tap_listener("lbm_uim", (void *)seq_info, NULL, TL_REQUIRES_COLUMNS, NULL, lbm_uimflow_tap_packet, NULL); cf_retap_packets(cfile); remove_tap_listener((void *)seq_info); /* Fill in the timestamps. */ list = g_queue_peek_nth_link(seq_info->items, 0); while (list != NULL) { seq_analysis_item_t * seq_item = (seq_analysis_item_t *)list->data; set_fd_time(cfile->epan, frame_data_sequence_find(cfile->frames, seq_item->frame_number), time_str); seq_item->time_str = g_strdup(time_str); list = g_list_next(list); } }
void listbox_remove_current (WListbox * l) { if (!listbox_is_empty (l)) { GList *current; int length; current = g_queue_peek_nth_link (l->list, (guint) l->pos); listbox_entry_free (LENTRY (current->data)); g_queue_delete_link (l->list, current); length = g_queue_get_length (l->list); if (length == 0) l->top = l->pos = 0; else if (l->pos >= length) l->pos = length - 1; } }
static void push_instr_code_nthcdr(push_t *push, void *userdata) { push_val_t *val1, *val2; GList *link; int n; if (CH(push->code, 1) && CH(push->integer, 1)) { val1 = push_stack_pop(push->integer); val2 = push_stack_pop_code(push); if (push_code_length(val2->code) > 0) { n = MOD(val1->integer, push_code_length(val2->code)); if (n > 0) { link = g_queue_peek_nth_link(val2->code, n); push_stack_push_new(push, push->code, PUSH_TYPE_CODE, push_code_dup_ext(val2->code, link, NULL, NULL, NULL)); } } else { push_stack_push(push->code, val2); } } }
/** * g_queue_push_nth_link: * @queue: a #GQueue * @n: the position to insert the link. If this is negative or larger than * the number of elements in @queue, the link is added to the end of * @queue. * @link_: the link to add to @queue * * Inserts @link into @queue at the given position. * * Since: 2.4 **/ void g_queue_push_nth_link (GQueue *queue, gint n, GList *link_) { GList *next; GList *prev; g_return_if_fail (queue != NULL); g_return_if_fail (link_ != NULL); if (n < 0 || n >= queue->length) { g_queue_push_tail_link (queue, link_); return; } g_assert (queue->head); g_assert (queue->tail); next = g_queue_peek_nth_link (queue, n); prev = next->prev; if (prev) prev->next = link_; next->prev = link_; link_->next = next; link_->prev = prev; if (queue->head->prev) queue->head = queue->head->prev; if (queue->tail->next) queue->tail = queue->tail->next; queue->length++; }
static void listbox_draw (WListbox * l, gboolean focused) { Widget *w = WIDGET (l); const WDialog *h = w->owner; const gboolean disabled = (w->options & W_DISABLED) != 0; const int normalc = disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]; /* *INDENT-OFF* */ int selc = disabled ? DISABLED_COLOR : focused ? h->color[DLG_COLOR_HOT_FOCUS] : h->color[DLG_COLOR_FOCUS]; /* *INDENT-ON* */ int length = 0; GList *le = NULL; int pos; int i; int sel_line = -1; if (l->list != NULL) { length = g_queue_get_length (l->list); le = g_queue_peek_nth_link (l->list, (guint) l->top); } /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */ pos = (le == NULL) ? 0 : l->top; for (i = 0; i < w->lines; i++) { const char *text = ""; /* Display the entry */ if (pos == l->pos && sel_line == -1) { sel_line = i; tty_setcolor (selc); } else tty_setcolor (normalc); widget_move (l, i, 1); if (l->list != NULL && le != NULL && (i == 0 || pos < length)) { WLEntry *e = LENTRY (le->data); text = e->text; le = g_list_next (le); pos++; } tty_print_string (str_fit_to_term (text, w->cols - 2, J_LEFT_FIT)); } l->cursor_y = sel_line; if (l->scrollbar && length > w->lines) { tty_setcolor (normalc); listbox_drawscroll (l); } }
static void meh_screen_popup_favorite_toggle(App* app, Screen* screen) { g_assert(app != NULL); g_assert(screen != NULL); PopupData* data = meh_screen_popup_get_data(screen); ExecutableListData* exec_list_data = meh_exec_list_get_data(data->src_screen); /* updates the value of the executable */ gboolean new_value = data->executable->favorite == 1 ? FALSE : TRUE; if (meh_db_set_executable_favorite(app->db, data->executable, new_value)) { data->executable->favorite = new_value; } /* re-position the executable in the executables list if necessary */ if (g_queue_get_length(exec_list_data->executables) > 1) { int prev_selected = exec_list_data->selected_executable; unsigned int i = 0; /* retrieves the one which will move in the list */ Executable* to_move = g_queue_pop_nth(exec_list_data->executables, exec_list_data->selected_executable); /* find the good position for the moved executable */ for (i = 0; i < g_queue_get_length(exec_list_data->executables); i++) { gboolean exit = FALSE; Executable* ex = g_queue_peek_nth(exec_list_data->executables, i); /* if favorite, ensure to stay in the favorite zone */ if (new_value == TRUE) { if (ex->favorite == FALSE) { exit = TRUE; } } gchar* first = g_utf8_strup(ex->display_name, g_utf8_strlen(ex->display_name, -1)); gchar* second = g_utf8_strup(data->executable->display_name, g_utf8_strlen(ex->display_name, -1)); if (g_utf8_collate(first, second) > 0) { if (new_value == TRUE && ex->favorite == TRUE) { exit = TRUE; } else if (new_value == FALSE && ex->favorite == FALSE) { exit = TRUE; } } g_free(first); g_free(second); if (exit) { break; } } GList* after = g_queue_peek_nth_link(exec_list_data->executables, i); /* re-add it to the good position */ g_queue_insert_before(exec_list_data->executables, after, to_move); /* notify the screen of the new selected executable */ exec_list_data->selected_executable = i; /* redraw the executables list texts */ meh_exec_list_refresh_executables_widget(app, data->src_screen); /* move and redraw the selection */ meh_exec_list_after_cursor_move(app, data->src_screen, prev_selected); } /* finally close the popup */ meh_screen_popup_close(screen); }
void sequence_analysis_dump_to_file(FILE *of, seq_analysis_info_t *sainfo, unsigned int first_node) { guint32 i, display_items, display_nodes; guint32 start_position, end_position, item_width, header_length; seq_analysis_item_t *sai; guint16 first_conv_num = 0; gboolean several_convs = FALSE; gboolean first_packet = TRUE; GString *label_string, *empty_line, *separator_line, *tmp_str, *tmp_str2; const char *empty_header; char src_port[8], dst_port[8]; GList *list = NULL; char *addr_str; display_items = 0; if (sainfo->items != NULL) list = g_queue_peek_nth_link(sainfo->items, 0); while (list) { sai = (seq_analysis_item_t *)list->data; list = g_list_next(list); if (!sai->display) continue; display_items += 1; if (first_packet) { first_conv_num = sai->conv_num; first_packet = FALSE; } else if (sai->conv_num != first_conv_num) { several_convs = TRUE; } } /* if not items to display */ if (display_items == 0) { return; } label_string = g_string_new(""); empty_line = g_string_new(""); separator_line = g_string_new(""); tmp_str = g_string_new(""); tmp_str2 = g_string_new(""); display_nodes = sainfo->num_nodes; /* Write the conv. and time headers */ if (several_convs) { fprintf(of, CONV_TIME_HEADER); empty_header = CONV_TIME_EMPTY_HEADER; header_length = CONV_TIME_HEADER_LENGTH; } else{ fprintf(of, TIME_HEADER); empty_header = TIME_EMPTY_HEADER; header_length = TIME_HEADER_LENGTH; } /* Write the node names on top */ for (i=0; i<display_nodes; i+=2) { /* print the node identifiers */ addr_str = address_to_display(NULL, &(sainfo->nodes[i+first_node])); g_string_printf(label_string, "| %s", addr_str); wmem_free(NULL, addr_str); enlarge_string(label_string, NODE_CHARS_WIDTH*2, ' '); fprintf(of, "%s", label_string->str); g_string_printf(label_string, "| "); enlarge_string(label_string, NODE_CHARS_WIDTH, ' '); g_string_append(empty_line, label_string->str); } fprintf(of, "|\n%s", empty_header); g_string_printf(label_string, "| "); enlarge_string(label_string, NODE_CHARS_WIDTH, ' '); fprintf(of, "%s", label_string->str); /* Write the node names on top */ for (i=1; i<display_nodes; i+=2) { /* print the node identifiers */ addr_str = address_to_display(NULL, &(sainfo->nodes[i+first_node])); g_string_printf(label_string, "| %s", addr_str); wmem_free(NULL, addr_str); if (label_string->len < NODE_CHARS_WIDTH) { enlarge_string(label_string, NODE_CHARS_WIDTH, ' '); g_string_append(label_string, "| "); } enlarge_string(label_string, NODE_CHARS_WIDTH*2, ' '); fprintf(of, "%s", label_string->str); g_string_printf(label_string, "| "); enlarge_string(label_string, NODE_CHARS_WIDTH, ' '); g_string_append(empty_line, label_string->str); } fprintf(of, "\n"); g_string_append_c(empty_line, '|'); enlarge_string(separator_line, (guint32) empty_line->len + header_length, '-'); /* * Draw the items */ list = g_queue_peek_nth_link(sainfo->items, 0); while (list) { sai = (seq_analysis_item_t *)list->data; list = g_list_next(list); if (!sai->display) continue; start_position = (sai->src_node-first_node)*NODE_CHARS_WIDTH+NODE_CHARS_WIDTH/2; end_position = (sai->dst_node-first_node)*NODE_CHARS_WIDTH+NODE_CHARS_WIDTH/2; if (start_position > end_position) { item_width = start_position-end_position; } else if (start_position < end_position) { item_width = end_position-start_position; } else{ /* same origin and destination address */ end_position = start_position+NODE_CHARS_WIDTH; item_width = NODE_CHARS_WIDTH; } /* separator between conversations */ if (sai->conv_num != first_conv_num) { fprintf(of, "%s\n", separator_line->str); first_conv_num = sai->conv_num; } /* write the conversation number */ if (several_convs) { g_string_printf(label_string, "%i", sai->conv_num); enlarge_string(label_string, 5, ' '); fprintf(of, "%s", label_string->str); } if (sai->time_str != NULL) { g_string_printf(label_string, "|%s", sai->time_str); enlarge_string(label_string, 10, ' '); fprintf(of, "%s", label_string->str); } /* write the frame label */ g_string_printf(tmp_str, "%s", empty_line->str); overwrite(tmp_str, sai->frame_label, start_position, end_position ); fprintf(of, "%s", tmp_str->str); /* write the comments */ fprintf(of, "%s\n", sai->comment); /* write the arrow and frame label*/ fprintf(of, "%s", empty_header); g_string_printf(tmp_str, "%s", empty_line->str); g_string_truncate(tmp_str2, 0); if (start_position<end_position) { enlarge_string(tmp_str2, item_width-2, '-'); g_string_append_c(tmp_str2, '>'); } else{ g_string_printf(tmp_str2, "<"); enlarge_string(tmp_str2, item_width-1, '-'); } overwrite(tmp_str, tmp_str2->str, start_position, end_position ); g_snprintf(src_port, sizeof(src_port), "(%i)", sai->port_src); g_snprintf(dst_port, sizeof(dst_port), "(%i)", sai->port_dst); if (start_position<end_position) { overwrite(tmp_str, src_port, start_position-9, start_position-1); overwrite(tmp_str, dst_port, end_position+1, end_position+9); } else{ overwrite(tmp_str, src_port, start_position+1, start_position+9); overwrite(tmp_str, dst_port, end_position-9, end_position+1); } fprintf(of, "%s\n", tmp_str->str); } g_string_free(label_string, TRUE); g_string_free(empty_line, TRUE); g_string_free(separator_line, TRUE); g_string_free(tmp_str, TRUE); g_string_free(tmp_str2, TRUE); }