1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-08-04 18:44:14 -04:00
profanity/prof_history.c

182 lines
5.2 KiB
C
Raw Normal View History

2012-04-29 20:09:42 -04:00
#include <stdio.h>
#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-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-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-04-30 14:52:56 -04:00
// if adding a new item, copy the session over the history
2012-04-30 19:24:31 -04:00
if (history->sess_curr == history->sess_new) {
history->sess_curr->data = copied;
2012-04-29 21:36:45 -04:00
_replace_history_with_session(history);
// otherwise, adding edited history item
} else {
2012-04-30 14:52:56 -04:00
if (history->sess_new != NULL) {
2012-04-30 19:24:31 -04:00
// copy the current string to the last element in session
history->sess_new->data = copied;
} else {
g_list_append(history->session, copied);
2012-04-30 14:52:56 -04:00
}
2012-04-30 19:24:31 -04:00
2012-04-29 21:36:45 -04:00
// replace the edited version with the data from the history
history->sess_curr->data = strdup(history->items_curr->data);
// rewrite history from the session
_replace_history_with_session(history);
}
}
}
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-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-04-30 14:52:56 -04:00
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
// no history, or no session, return item
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
}
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;
}