-
Notifications
You must be signed in to change notification settings - Fork 1
/
undo.c
110 lines (90 loc) · 2.1 KB
/
undo.c
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
/*
* Copyright (c) 2013-2017 Ilya Kaliman
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "vimol.h"
struct node {
void *data;
struct node *next;
struct node *prev;
};
struct undo {
void *(*copy)(void *);
void (*free)(void *);
struct node *iter;
};
static void
free_all(struct undo *undo, struct node *node)
{
struct node *next;
while (node) {
next = node->next;
(undo->free)(node->data);
free(node);
node = next;
}
}
struct undo *
undo_create(void *data, void *(*copy)(void *), void (*free)(void *))
{
struct undo *undo;
undo = xcalloc(1, sizeof *undo);
undo->copy = copy;
undo->free = free;
undo->iter = xcalloc(1, sizeof *undo->iter);
undo->iter->data = data;
return (undo);
}
void
undo_free(struct undo *undo)
{
if (undo) {
while (undo->iter->prev)
undo->iter = undo->iter->prev;
free_all(undo, undo->iter);
free(undo);
}
}
void *
undo_get_data(struct undo *undo)
{
return (undo->iter->data);
}
void
undo_snapshot(struct undo *undo)
{
struct node *node;
free_all(undo, undo->iter->next);
node = xcalloc(1, sizeof *node);
node->data = (undo->copy)(undo->iter->data);
undo->iter->next = node;
node->prev = undo->iter;
undo->iter = node;
}
int
undo_undo(struct undo *undo)
{
if (undo->iter->prev == NULL)
return (0);
undo->iter = undo->iter->prev;
return (1);
}
int
undo_redo(struct undo *undo)
{
if (undo->iter->next == NULL)
return (0);
undo->iter = undo->iter->next;
return (1);
}