Executable values and functions now include a context.
[mpdm.git] / mpdm_d.c
blob5bba65f7ab3715de04bb6f3d352582b4ffc21aca
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 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 /** data **/
37 static wchar_t *dump_1(const mpdm_t v, int l, wchar_t * ptr, int *size);
38 wchar_t * (*mpdm_dump_1) (const mpdm_t v, int l, wchar_t *ptr, int *size) = NULL;
40 /** code **/
42 static wchar_t *dump_1(const mpdm_t v, int l, wchar_t *ptr, int *size)
43 /* dumps one value to the ptr dynamic string with 'l' indenting level */
45 int n;
46 wchar_t *wptr;
48 mpdm_ref(v);
50 /* indent */
51 for (n = 0; n < l; n++)
52 ptr = mpdm_pokews(ptr, size, L" ");
54 if (v != NULL) {
55 char tmp[256];
56 int s;
58 if (v->flags & MPDM_DELETED)
59 strcpy(tmp, "**DELETED**");
60 else
61 sprintf(tmp, "%d,%c%c%c%c:", v->ref,
62 v->flags & MPDM_FILE ? 'F' :
63 (v->flags & MPDM_STRING ? 'S' :
64 (v->flags & MPDM_EXEC ? 'X' : '-')),
65 v->flags & MPDM_HASH ? 'H' :
66 (v->flags & MPDM_MULTIPLE ? 'M' : '-'),
67 v->flags & MPDM_FREE ? 'A' : '-',
68 v->flags & MPDM_IVAL ? 'I' :
69 (v->flags & MPDM_RVAL ? 'R' : '-')
72 wptr = mpdm_mbstowcs(tmp, &s, -1);
73 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
74 free(wptr);
76 /* if it's a multiple value, add also the number
77 of elements */
78 if (v->flags & MPDM_MULTIPLE) {
79 sprintf(tmp, "[%d] ", mpdm_size(v));
80 wptr = mpdm_mbstowcs(tmp, &s, -1);
81 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
82 free(wptr);
86 /* add the visual representation of the value */
87 ptr = mpdm_pokev(ptr, size, v);
88 ptr = mpdm_pokewsn(ptr, size, L"\n", 1);
90 if (v != NULL) {
91 /* if it's a hash, iterate it */
92 if (v->flags & MPDM_HASH) {
93 int c = 0;
94 mpdm_t k, w;
96 while (mpdm_iterator(v, &c, &k, &w)) {
97 ptr = dump_1(k, l + 1, ptr, size);
98 ptr = dump_1(w, 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 mpdm_unrefnd(v);
110 return ptr;
115 * mpdm_dumper - Returns a visual representation of a complex value.
116 * @v: The value
118 * Returns a visual representation of a complex value.
120 mpdm_t mpdm_dumper(const mpdm_t v)
122 int size = 0;
123 wchar_t *ptr;
125 /* if no dumper plugin is defined, fall back to default */
126 if (mpdm_dump_1 == NULL)
127 mpdm_dump_1 = dump_1;
129 ptr = mpdm_dump_1(v, 0, NULL, &size);
130 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
132 return MPDM_ENS(ptr, size - 1);
137 * mpdm_dump - Dumps a value to stdin.
138 * @v: The value
140 * Dumps a value to stdin. The value can be complex. This function
141 * is for debugging purposes only.
143 void mpdm_dump(const mpdm_t v)
145 mpdm_t w;
147 w = mpdm_dumper(v);
148 mpdm_write_wcs(stdout, mpdm_string(w));
149 mpdm_destroy(w);
154 * mpdm_dump_unref - Dumps all unreferenced values.
156 * Dumps all unreferenced values.
158 void mpdm_dump_unref(void)
160 mpdm_t v;
161 int count, unref;
163 /* loop all values */
164 v = mpdm->cur;
165 count = mpdm->count;
166 unref = 0;
168 printf("** Unreferenced values:\n");
170 while (count--) {
171 if (v->ref == 0) {
172 mpdm_dump(v);
173 unref++;
176 v = v->next;
179 printf("** Total unreferenced: %d\n", unref);