Updated TODO.
[mpdm.git] / mpdm_d.c
blob58087a878d07638d274eb4cda14c5530fa48a1da
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) =
39 NULL;
41 /** code **/
43 static wchar_t *dump_1(const mpdm_t v, int l, wchar_t * ptr, int *size)
44 /* dumps one value to the ptr dynamic string with 'l' indenting level */
46 int n;
47 wchar_t *wptr;
49 mpdm_ref(v);
51 /* indent */
52 for (n = 0; n < l; n++)
53 ptr = mpdm_pokews(ptr, size, L" ");
55 if (v != NULL) {
56 char tmp[256];
57 int s;
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_REGEX ? 'r' :
66 (v->flags & MPDM_FREE ? 'A' : '-'),
67 v->flags & MPDM_IVAL ? 'I' :
68 (v->flags & MPDM_RVAL ? 'R' : '-')
71 wptr = mpdm_mbstowcs(tmp, &s, -1);
72 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
73 free(wptr);
75 /* if it's a multiple value, add also the number
76 of elements */
77 if (v->flags & MPDM_MULTIPLE) {
78 sprintf(tmp, "[%d] ", mpdm_size(v));
79 wptr = mpdm_mbstowcs(tmp, &s, -1);
80 ptr = mpdm_poke(ptr, size, wptr, s, sizeof(wchar_t));
81 free(wptr);
85 /* add the visual representation of the value */
86 ptr = mpdm_pokev(ptr, size, v);
87 ptr = mpdm_pokewsn(ptr, size, L"\n", 1);
89 if (v != NULL) {
90 /* if it's a hash, iterate it */
91 if (v->flags & MPDM_HASH) {
92 int c = 0;
93 mpdm_t k, w;
95 while (mpdm_iterator(v, &c, &k, &w)) {
96 ptr = dump_1(k, l + 1, ptr, size);
97 ptr = dump_1(w, l + 2, ptr, size);
100 else
101 if (v->flags & MPDM_MULTIPLE) {
102 for (n = 0; n < mpdm_size(v); n++)
103 ptr = dump_1(mpdm_aget(v, n), l + 1, ptr, size);
107 mpdm_unref(v);
109 return ptr;
113 static wchar_t *do_dump(mpdm_t v, int *size)
115 wchar_t *ptr;
117 /* if no dumper plugin is defined, fall back to default */
118 if (mpdm_dump_1 == NULL)
119 mpdm_dump_1 = dump_1;
121 *size = 0;
122 ptr = mpdm_dump_1(v, 0, NULL, size);
123 ptr = mpdm_pokewsn(ptr, size, L"", 1);
125 return ptr;
130 * mpdm_dumper - Returns a visual representation of a complex value.
131 * @v: The value
133 * Returns a visual representation of a complex value.
135 mpdm_t mpdm_dumper(const mpdm_t v)
137 int size = 0;
138 wchar_t *ptr;
140 ptr = do_dump(v, &size);
142 return MPDM_ENS(ptr, size - 1);
147 * mpdm_dump - Dumps a value to stdin.
148 * @v: The value
150 * Dumps a value to stdin. The value can be complex. This function
151 * is for debugging purposes only.
153 void mpdm_dump(const mpdm_t v)
155 int size = 0;
156 wchar_t *ptr;
158 mpdm_ref(v);
160 ptr = do_dump(v, &size);
161 mpdm_write_wcs(stdout, ptr);
162 free(ptr);
164 mpdm_unrefnd(v);