If mpdm_dump_1 is NULL, it's set back to dump_1() by mpdm_dumper().
[mpdm.git] / mpdm_d.c
blob9bb5bbaf339dc94a4d9081550ae97c3d785e6e5f
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2009 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 /* indent */
49 for (n = 0; n < l; n++)
50 ptr = mpdm_pokews(ptr, size, L" ");
52 if (v != NULL) {
53 char tmp[256];
54 int s;
56 if (v->flags & MPDM_DELETED)
57 strcpy(tmp, "**DELETED**");
58 else
59 sprintf(tmp, "%d,%c%c%c%c:", v->ref,
60 v->flags & MPDM_FILE ? 'F' :
61 (v->flags & MPDM_STRING ? 'S' :
62 (v->flags & MPDM_EXEC ? 'X' : '-')),
63 v->flags & MPDM_HASH ? 'H' :
64 (v->flags & MPDM_MULTIPLE ? 'M' : '-'),
65 v->flags & MPDM_FREE ? 'A' : '-',
66 v->flags & MPDM_IVAL ? 'I' :
67 (v->flags & MPDM_RVAL ? 'R' : '-')
70 wptr = mpdm_mbstowcs(tmp, &s, -1);
71 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
72 free(wptr);
74 /* if it's a multiple value, add also the number
75 of elements */
76 if (v->flags & MPDM_MULTIPLE) {
77 sprintf(tmp, "[%d] ", mpdm_size(v));
78 wptr = mpdm_mbstowcs(tmp, &s, -1);
79 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
80 free(wptr);
84 /* add the visual representation of the value */
85 wptr = mpdm_string(v);
86 ptr = mpdm_pokews(ptr, size, wptr);
87 ptr = mpdm_pokews(ptr, size, L"\n");
89 if (v != NULL) {
90 /* if it's a hash, iterate it using hkeys
91 (and not assuming a hash is an array) */
92 if (v->flags & MPDM_HASH) {
93 mpdm_t w;
94 mpdm_t t;
96 w = mpdm_keys(v);
98 for (n = 0; n < mpdm_size(w); n++) {
99 t = mpdm_aget(w, n);
101 ptr = dump_1(t, l + 1, ptr, size);
102 ptr = dump_1(mpdm_hget(v, t), l + 2, ptr, size);
105 else
106 if (v->flags & MPDM_MULTIPLE) {
107 for (n = 0; n < mpdm_size(v); n++)
108 ptr = dump_1(mpdm_aget(v, n), l + 1, ptr, size);
112 return ptr;
117 * mpdm_dumper - Returns a visual representation of a complex value
118 * @v: The value
120 * Returns a visual representation of a complex value.
122 mpdm_t mpdm_dumper(const mpdm_t v)
124 int size = 0;
125 wchar_t *ptr;
127 /* if no dumper plugin is defined, fall back to default */
128 if (mpdm_dump_1 == NULL)
129 mpdm_dump_1 = dump_1;
131 ptr = mpdm_dump_1(v, 0, NULL, &size);
132 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
134 return MPDM_ENS(ptr, size);
139 * mpdm_dump - Dumps a value to stdin.
140 * @v: The value
142 * Dumps a value to stdin. The value can be complex. This function
143 * is for debugging purposes only.
145 void mpdm_dump(const mpdm_t v)
147 mpdm_t w;
149 w = mpdm_dumper(v);
150 mpdm_write_wcs(stdout, mpdm_string(w));
151 mpdm_destroy(w);
156 * mpdm_dump_unref - Dumps all unreferenced values.
158 * Dumps all unreferenced values.
160 void mpdm_dump_unref(void)
162 mpdm_t v;
163 int count, unref;
165 /* loop all values */
166 v = mpdm->cur;
167 count = mpdm->count;
168 unref = 0;
170 printf("** Unreferenced values:\n");
172 while (count--) {
173 if (v->ref == 0) {
174 mpdm_dump(v);
175 unref++;
178 v = v->next;
181 printf("** Total unreferenced: %d\n", unref);