File: uundo_debug.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (132 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download | duplicates (6)
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
/*
    libuundo - micro-undo, a small, callback based undo/redo list implementation
    Copyright (C) 2017  Tibor 'Igor2' Palinkas

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    Author: libuundo [+at+] igor2.repo.hu
    VCS:    svn://repo.hu/libuundo/trunk
    Web:    http://repo.hu/projects/libuundo
*/

#include <stdio.h>
#include <string.h>
#include "uundo.h"
#include "uundo_debug.h"

void uundo_dump(uundo_list_t *lst, FILE *f, const char *prefix)
{
	char buff[8192];
	uundo_item_t *i;

	if (prefix == NULL)
		prefix = "";
	if (f == NULL)
		f = stdout;

	fprintf(f, "%sUndo list: num_undo=%ld num_redo=%ld freeze=%lu/%lu serial=%ld\n", prefix, (long)lst->num_undo, (long)lst->num_redo, lst->freeze_serial, lst->freeze_add, (long)lst->serial);
	for(i = lst->head; i != NULL; i = i->next) {
		char mark = ' ';
		strcpy(buff, "<unknown>");
		if (i->oper->item_print != NULL)
			i->oper->item_print(i->udata, buff, sizeof(buff));
		if ((i == lst->head) && (i == lst->tail))
			mark = '*';
		else if (i == lst->head)
			mark = 'h';
		else if (i == lst->tail)
			mark = 't';
		fprintf(f, "%s %c [%ld] %s\n", prefix, mark, (long)i->serial, buff);
	}
}

#define error(txt) \
do { \
	strcpy(msg, txt); \
	return msg; \
} while(0)

const char *uundo_check(const uundo_list_t *lst, char *msg)
{
	uundo_item_t *i, *last = NULL;
	int seen_tail = 0;
	static char msg_buff[256];
	size_t num_undo = 0, num_redo = 0;

	if (msg == NULL)
		msg = msg_buff;

	/* empty list */
	if (lst->head == NULL) {
		if (lst->num_undo != 0)
			error("lib-error: Empty list with non-zero num_undo");

		if (lst->num_redo != 0)
			error("lib-error: Empty list with non-zero num_redo");

		if (lst->tail != NULL)
			error("lib-error: Empty list with tail set");

		return NULL;
	}

	/* check first link */
	if (lst->head->prev != NULL)
		error("lib-error: first item's ->prev is not NULL");

	/* Check serial numbers and links */
	for(i = lst->head; i != NULL; i = i->next) {
		if (i != lst->head) { /* checks from the second item */
			if (i->prev->next != i) {
				sprintf(msg, "lib-error: wrong item->prev->next after %ld", (long)i->prev->serial);
				return msg;
			}
			if (i->serial < i->prev->serial) {
				sprintf(msg, "user-error: wrong serial after %ld", (long)i->prev->serial);
				return msg;
			}
		}
		if ((!seen_tail) && (lst->tail != NULL))
			num_undo++;
		else
			num_redo++;
		if (i == lst->tail)
			seen_tail = 1;
		last = i;
	}

	/* check last link */
	if (last == NULL)
		error("lib-error: no last item while head is not NULL");

	if (last->next != NULL)
		error("lib-error: last item's ->next is not NULL");

	if ((lst->tail != NULL) && (!seen_tail))
		error("lib-error: tail not found in the list");

	if (num_undo != lst->num_undo) {
		sprintf(msg, "lib-error: number of undo items mismatch: lst=%ld count=%ld", (long)lst->num_undo, (long)num_undo);
		return msg;
	}

	if (num_redo != lst->num_redo) {
		sprintf(msg, "lib-error: number of redo items mismatch: lst=%ld count=%ld", (long)lst->num_redo, (long)num_redo);
		return msg;
	}

	return NULL;
}