Updated TODO.
[mpdm.git] / mpdm_d.c
blob6e9a02803518fd0d0ec6fe4e33796a4253024f77
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 mpdm_d.c - Debugging utilities
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <wchar.h>
33 #include "mpdm.h"
35 /*******************
36 Data
37 ********************/
39 /*******************
40 Code
41 ********************/
43 static wchar_t *dump_1(const mpdm_t v, int l, wchar_t * ptr, int *size)
45 int n;
46 wchar_t *wptr;
48 /* indent */
49 for (n = 0; n < l; n++)
50 ptr = mpdm_poke(ptr, size, L" ", 2, sizeof(wchar_t));
52 if (v != NULL) {
53 char tmp[256];
54 int s;
56 sprintf(tmp, "%d,%c%c%c%c:", v->ref,
57 v->flags & MPDM_FILE ? 'F' :
58 (v->flags & MPDM_STRING ? 'S' :
59 (v->flags & MPDM_EXEC ? 'X' : '-')),
60 v->flags & MPDM_HASH ? 'H' :
61 (v->flags & MPDM_MULTIPLE ? 'M' : '-'),
62 v->flags & MPDM_FREE ? 'A' : '-',
63 v->flags & MPDM_IVAL ? 'I' : (v->flags & MPDM_RVAL ? 'R' : '-')
66 wptr = mpdm_mbstowcs(tmp, &s, -1);
67 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
68 free(wptr);
70 /* if it's a multiple value, add also the number
71 of elements */
72 if (v->flags & MPDM_MULTIPLE) {
73 sprintf(tmp, "[%d] ", mpdm_size(v));
74 wptr = mpdm_mbstowcs(tmp, &s, -1);
75 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
76 free(wptr);
80 /* add the visual representation of the value */
81 wptr = mpdm_string(v);
82 ptr = mpdm_poke(ptr, size, wptr, wcslen(wptr), sizeof(wchar_t));
83 ptr = mpdm_poke(ptr, size, L"\n", 1, sizeof(wchar_t));
85 if (v != NULL) {
86 /* if it's a hash, iterate it using hkeys
87 (and not assuming a hash is an array) */
88 if (v->flags & MPDM_HASH) {
89 mpdm_t w;
90 mpdm_t t;
92 w = mpdm_keys(v);
94 for (n = 0; n < mpdm_size(w); n++) {
95 t = mpdm_aget(w, n);
97 ptr = dump_1(t, l + 1, ptr, size);
98 ptr = dump_1(mpdm_hget(v, t), l + 2, ptr, size);
101 else
102 if (v->flags & MPDM_MULTIPLE) {
103 for (n = 0; n < mpdm_size(v); n++)
104 ptr = dump_1(mpdm_aget(v, n), l + 1, ptr, size);
108 return ptr;
113 * mpdm_dumper - Returns a visual representation of a complex value
114 * @v: The value
116 * Returns a visual representation of a complex value.
118 mpdm_t mpdm_dumper(const mpdm_t v)
120 int size = 0;
121 wchar_t *ptr;
123 ptr = dump_1(v, 0, NULL, &size);
124 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
126 return MPDM_ENS(ptr, size);
131 * mpdm_dump - Dumps a value to stdin.
132 * @v: The value
134 * Dumps a value to stdin. The value can be complex. This function
135 * is for debugging purposes only.
137 void mpdm_dump(const mpdm_t v)
139 mpdm_t w;
141 w = mpdm_dumper(v);
142 mpdm_write_wcs(stdout, mpdm_string(w));
143 mpdm_destroy(w);
148 * mpdm_dump_unref - Dumps all unreferenced values.
150 * Dumps all unreferenced values.
152 void mpdm_dump_unref(void)
154 mpdm_t v;
155 int count, unref;
157 /* loop all values */
158 v = mpdm->cur;
159 count = mpdm->count;
160 unref = 0;
162 printf("** Unreferenced values:\n");
164 while (count--) {
165 if (v->ref == 0) {
166 mpdm_dump(v);
167 unref++;
170 v = v->next;
173 printf("** Total unreferenced: %d\n", unref);