1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "prof_history.h"
struct p_history_t {
GList *items; // the history
GList *items_curr; // pointer to the current line in the history
GList *session; // a copy of the history for edits
GList *sess_curr; // pointer to the current item in the session
GList *sess_new; // pointer to a possible new element in the session
guint max_size;
};
static void _replace_history_with_session(PHistory history);
PHistory p_history_new(unsigned int size)
{
PHistory new_history = malloc(sizeof(struct p_history_t));
new_history->items = NULL;
new_history->items_curr = NULL;
new_history->session = NULL;
new_history->sess_curr = NULL;
new_history->sess_new = NULL;
new_history->max_size = size;
return new_history;
}
void p_history_append(PHistory history, char *item)
{
char *copied = strdup(item);
// if not editing history (no session)
if (history->session == NULL) {
// if already at max size
if (g_list_length(history->items) == history->max_size) {
// remove first element
GList *first = g_list_first(history->items);
const char *first_item = (char *) first->data;
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 {
// if adding a new element, copy the session over the history
if (history->sess_curr == history->sess_new) {
_replace_history_with_session(history);
// otherwise, adding edited history item
} else {
// remove the new item, from the session
history->session = g_list_reverse(history->session);
GList *first = g_list_first(history->session);
const char *first_item = (char *) first->data;
history->session = g_list_remove(history->session, first_item);
history->session = g_list_reverse(history->session);
// copy sess_curr to the end of the session
char *new_data = strdup(history->sess_curr->data);
history->session = g_list_append(history->session, new_data);
// 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) {
GList *first = g_list_first(history->items);
const char *first_item = (char *) first->data;
history->items = g_list_remove(history->items, first_item);
}
// reset the session
history->items_curr = NULL;
history->session = NULL;
history->sess_curr = NULL;
history->sess_new = NULL;
}
char * p_history_previous(PHistory history, char *item)
{
if (history->items == NULL) {
return item;
}
if (history->session == NULL) {
history->session = g_list_copy(history->items);
history->sess_curr = g_list_last(history->session);
} else {
history->sess_curr = g_list_previous(history->sess_curr);
}
// set to first if rolled over beginning
if (history->sess_curr == NULL) {
history->sess_curr = g_list_first(history->session);
}
char *curr = history->sess_curr->data;
char *result = malloc((strlen(curr) + 1) * sizeof(char));
strcpy(result, curr);
return result;
}
char * p_history_next(PHistory history, char *item)
{
if (history->session == NULL) {
return NULL;
} else {
history->sess_curr = g_list_next(history->sess_curr);
}
// set to last if rolled over end
if (history->sess_curr == NULL) {
history->sess_curr = g_list_last(history->session);
}
char *curr = history->sess_curr->data;
char *result = malloc((strlen(curr) + 1) * sizeof(char));
strcpy(result, curr);
return result;
}
|