File: undo.c

package info (click to toggle)
gnusound 0.6.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,488 kB
  • ctags: 1,778
  • sloc: ansic: 21,146; sh: 2,505; xml: 527; makefile: 171; perl: 157
file content (65 lines) | stat: -rw-r--r-- 1,718 bytes parent folder | download
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
/*
 * Copyright (C) 2002,2003 Pascal Haakmat.
 * Licensed under the GNU GPL.
 * Absolutely no warranty.
 */

#include <glib.h>

#include <config.h>
#include "snd.h"
#include "action.h"
#include "undo.h"
#include "pref.h"

GList *
undo_push(GList *undo_stack,
          action_group *ag) {
    GList *first;
    DEBUG("undo_stack[%d]: %p, action_group: %p\n",
          g_list_length(undo_stack), undo_stack, ag);
    if(!ag) {
        FAIL("can't push NULL action\n");
        return undo_stack;
    }

    if(g_list_length(undo_stack) > pref_get_as_int("undo_depth_max")) {
        DEBUG("undo stack depth exceeds maximum depth, destroying bottom...\n");
        first = g_list_first(undo_stack);
        undo_stack = g_list_remove_link(undo_stack, first);
        action_group_destroy((action_group *)first->data);
    }

    return g_list_append(undo_stack, ag);
}

GList *
undo_pop(GList *undo_stack) {
    GList *last = g_list_last(undo_stack);

    DEBUG("undo_stack[%d]: %p\n", 
          g_list_length(undo_stack), undo_stack);

    if(!last) {
        FAIL("no further undo information.\n");
        return undo_stack;
    }

    undo_stack = g_list_remove_link(undo_stack, last);
    action_group_do((action_group *)last->data);
    g_list_free(last);

    return undo_stack;
}

void 
undo_destroy(GList *undo_stack) {
    DEBUG("undo stack depth: %d\n", g_list_length(undo_stack));
    while(undo_stack && undo_stack->data) {
        DEBUG("destroying action_group %p\n", (action_group *)undo_stack->data);
        action_group_destroy((action_group *)undo_stack->data);
        undo_stack->data = NULL;
        undo_stack = g_list_remove(undo_stack, undo_stack->data);
    }
    g_list_free(undo_stack);
}