1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00
profanity/prof_history.c

212 lines
6.5 KiB
C
Raw Normal View History

2012-04-29 20:09:42 -04:00
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "prof_history.h"
struct p_history_t {
GList *items; // the history
2012-04-29 21:36:45 -04:00
GList *items_curr; // pointer to the current line in the history
2012-04-29 20:09:42 -04:00
GList *session; // a copy of the history for edits
2012-04-29 21:36:45 -04:00
GList *sess_curr; // pointer to the current item in the session
GList *sess_new; // pointer to a possible new element in the session
2012-04-29 20:09:42 -04:00
guint max_size;
};
2012-04-29 21:36:45 -04:00
static void _replace_history_with_session(PHistory history);
2012-05-01 17:09:33 -04:00
static gboolean _adding_new(PHistory history);
2012-04-29 21:36:45 -04:00
2012-04-29 20:09:42 -04:00
PHistory p_history_new(unsigned int size)
{
PHistory new_history = malloc(sizeof(struct p_history_t));
new_history->items = NULL;
2012-04-29 21:36:45 -04:00
new_history->items_curr = NULL;
2012-04-29 20:09:42 -04:00
new_history->session = NULL;
2012-04-29 21:36:45 -04:00
new_history->sess_curr = NULL;
new_history->sess_new = NULL;
2012-04-29 20:09:42 -04:00
new_history->max_size = size;
return new_history;
}
void p_history_append(PHistory history, char *item)
{
2012-05-01 17:09:33 -04:00
// copy input, default to ""
2012-04-30 14:52:56 -04:00
char *copied = "";
if (item != NULL) {
copied = strdup(item);
}
2012-04-29 20:09:42 -04:00
2012-04-30 14:52:56 -04:00
// if no history yet, just add the item
if (history->items == NULL) {
history->items = g_list_append(history->items, copied);
return;
}
// if not editing history (no session started)
2012-04-29 21:36:45 -04:00
if (history->session == NULL) {
2012-04-30 14:52:56 -04:00
// lose first element if hit max size
2012-04-29 21:36:45 -04:00
if (g_list_length(history->items) == history->max_size) {
// remove first element
GList *first = g_list_first(history->items);
2012-04-30 14:52:56 -04:00
char *first_item = first->data;
2012-04-29 21:36:45 -04:00
history->items = g_list_remove(history->items, first_item);
}
// append the new item onto the history
history->items = g_list_append(history->items, copied);
// if editing history (session exists with possible changes)
} else {
2012-05-01 17:09:33 -04:00
// update the current item with the input
history->sess_curr->data = copied;
2012-04-30 14:52:56 -04:00
2012-05-01 17:09:33 -04:00
// if current points to last we're just adding the new item,
// so replace items with session removing the last element if its ""
if (_adding_new(history)) {
// remove last if ""
if (strcmp(history->sess_curr->data, "") == 0) {
history->session = g_list_reverse(history->session);
GList *first = g_list_first(history->session);
history->session = g_list_remove(history->session, first->data);
history->session = g_list_reverse(history->session);
}
2012-04-29 21:36:45 -04:00
_replace_history_with_session(history);
// otherwise, adding edited history item
} else {
2012-04-30 19:24:31 -04:00
2012-05-01 17:09:33 -04:00
// remove the last element, its either "" or some new data
// we want to discard
history->session = g_list_reverse(history->session);
GList *first = g_list_first(history->session);
history->session = g_list_remove(history->session, first->data);
history->session = g_list_reverse(history->session);
// copy the data at the current position and append it to the
// session
char *new = strdup(history->sess_curr->data);
history->session = g_list_append(history->session, new);
// replace the edited version with the data from the original
// history
2012-04-29 21:36:45 -04:00
history->sess_curr->data = strdup(history->items_curr->data);
// rewrite history from the session
_replace_history_with_session(history);
}
}
}
2012-05-01 17:09:33 -04:00
static gboolean _adding_new(PHistory history)
{
return (history->sess_curr == g_list_last(history->session));
}
2012-04-29 21:36:45 -04:00
static void _replace_history_with_session(PHistory history)
{
g_list_free(history->items);
history->items = g_list_copy(history->session);
// remove first if overrun max size
if (g_list_length(history->items) > history->max_size) {
2012-04-29 20:09:42 -04:00
GList *first = g_list_first(history->items);
const char *first_item = (char *) first->data;
history->items = g_list_remove(history->items, first_item);
}
2012-04-29 21:36:45 -04:00
// reset the session
history->items_curr = NULL;
history->session = NULL;
history->sess_curr = NULL;
history->sess_new = NULL;
2012-04-29 20:09:42 -04:00
}
2012-04-29 21:36:45 -04:00
char * p_history_previous(PHistory history, char *item)
2012-04-29 20:09:42 -04:00
{
2012-04-30 14:52:56 -04:00
// no history
2012-04-29 20:09:42 -04:00
if (history->items == NULL) {
2012-04-30 19:24:31 -04:00
return NULL;
2012-04-29 20:09:42 -04:00
}
2012-05-01 17:09:33 -04:00
// copy input, default to ""
2012-04-30 14:52:56 -04:00
char *copied = "";
if (item != NULL) {
copied = strdup(item);
}
// no session, create one
2012-04-29 20:09:42 -04:00
if (history->session == NULL) {
history->session = g_list_copy(history->items);
2012-04-29 21:36:45 -04:00
history->sess_curr = g_list_last(history->session);
2012-04-30 14:52:56 -04:00
history->items_curr = g_list_last(history->items);
// add the new item including empty string
g_list_append(history->session, copied);
history->sess_new = g_list_last(history->session);
char *result = strdup(history->sess_curr->data);
return result;
// session exists
2012-04-29 20:09:42 -04:00
} else {
2012-04-30 14:52:56 -04:00
// update the currently pointed to item with passed data
history->sess_curr->data = copied;
// move to previous
2012-04-29 21:36:45 -04:00
history->sess_curr = g_list_previous(history->sess_curr);
2012-05-01 17:09:33 -04:00
if (history->items_curr == NULL)
history->items_curr = g_list_last(history->items);
else
history->items_curr = g_list_previous(history->items_curr);
2012-04-29 20:09:42 -04:00
2012-04-30 14:52:56 -04:00
// set to first if rolled over beginning
if (history->sess_curr == NULL) {
history->sess_curr = g_list_first(history->session);
history->items_curr = g_list_first(history->items);
}
2012-04-29 20:09:42 -04:00
}
2012-04-30 14:52:56 -04:00
char *result = strdup(history->sess_curr->data);
2012-04-29 20:09:42 -04:00
return result;
}
2012-04-29 21:36:45 -04:00
char * p_history_next(PHistory history, char *item)
2012-04-29 20:09:42 -04:00
{
2012-04-30 14:52:56 -04:00
2012-05-01 17:09:33 -04:00
// no history, or no session, return NULL
2012-04-30 14:52:56 -04:00
if ((history->items == NULL) || (history->session == NULL)) {
2012-04-30 19:24:31 -04:00
return NULL;
2012-04-30 14:52:56 -04:00
}
2012-05-01 17:09:33 -04:00
// copy input, default to ""
2012-04-30 14:52:56 -04:00
char *copied = "";
if (item != NULL) {
copied = strdup(item);
2012-04-29 20:09:42 -04:00
}
2012-04-30 14:52:56 -04:00
// session exists
// update the currently pointed to item with passed data
history->sess_curr->data = copied;
// move to next
history->sess_curr = g_list_next(history->sess_curr);
history->items_curr = g_list_next(history->items_curr);
// set to last if rolled over beginning
2012-04-29 21:36:45 -04:00
if (history->sess_curr == NULL) {
history->sess_curr = g_list_last(history->session);
2012-04-30 14:52:56 -04:00
history->items_curr = NULL;
2012-04-29 20:09:42 -04:00
}
2012-04-30 14:52:56 -04:00
char *result = strdup(history->sess_curr->data);
2012-04-29 20:09:42 -04:00
return result;
}