about summary refs log tree commit diff stats
path: root/prof_history.c
blob: 3653d181c2f5d35b4821bb816a91f2b520ec1efc (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#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);
static gboolean _adding_new(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)
{
    // copy input, default to ""
    char *copied = "";
    if (item != NULL) {
        copied = strdup(item);
    }

    // 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)
    if (history->session == NULL) {
        
        // lose first element if hit max size
        if (g_list_length(history->items) == history->max_size) {

            // remove first element
            GList *first = g_list_first(history->items);
            char *first_item = 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 {
        
        // update the current item with the input
        history->sess_curr->data = copied;

        // 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);
            }
            
            _replace_history_with_session(history);

        // otherwise, adding edited history item
        } else {
            
            // 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
            history->sess_curr->data = strdup(history->items_curr->data);
            
            // rewrite history from the session
            _replace_history_with_session(history);
        }
    }
}

static gboolean _adding_new(PHistory history)
{
    return (history->sess_curr == g_list_last(history->session));
}

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)
{
    // no history
    if (history->items == NULL) {
        return NULL;
    }

    // copy input, default to ""
    char *copied = "";
    if (item != NULL) {
        copied = strdup(item);
    }
    
    // no session, create one
    if (history->session == NULL) {
        history->session = g_list_copy(history->items);
        history->sess_curr = g_list_last(history->session);
        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
    } else {
        // update the currently pointed to item with passed data
        history->sess_curr->data = copied;
        
        // move to previous
        history->sess_curr = g_list_previous(history->sess_curr);
        if (history->items_curr == NULL)
            history->items_curr = g_list_last(history->items);
        else 
            history->items_curr = g_list_previous(history->items_curr);

        // 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);
        }
    }

    char *result = strdup(history->sess_curr->data);
    return result;
}

char * p_history_next(PHistory history, char *item)
{

    // no history, or no session, return NULL
    if ((history->items == NULL) || (history->session == NULL)) {
        return NULL;
    }

    // copy input, default to ""
    char *copied = "";
    if (item != NULL) {
        copied = strdup(item);
    }

    // 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
    if (history->sess_curr == NULL) {
        history->sess_curr = g_list_last(history->session);
        history->items_curr = NULL;
    }

    char *result = strdup(history->sess_curr->data);
    return result;
}