Fix indentation in hash values.
[mpsl.git] / mpsl_d.c
blob48d3a323f098b891b4bc4793dd1dbaebdf71ce80
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2009 Angel Ortega <angel@triptico.com>
6 mpsl_d.c - Minimum Profit Scripting Language debugging functions
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 <wchar.h>
30 #include "mpdm.h"
31 #include "mpsl.h"
33 /** code **/
35 static wchar_t *dump_string(const mpdm_t v, wchar_t *ptr, int *size)
36 /* dumps a string, escaping special chars */
38 wchar_t *iptr = mpdm_string(v);
40 ptr = mpdm_poke(ptr, size, L"\"", 1, sizeof(wchar_t));
42 while (*iptr != L'\0') {
43 switch (*iptr) {
44 case '"':
45 ptr = mpdm_poke(ptr, size, L"\\\"", 2, sizeof(wchar_t));
46 break;
48 case '\'':
49 ptr = mpdm_poke(ptr, size, L"\\'", 2, sizeof(wchar_t));
50 break;
52 case '\r':
53 ptr = mpdm_poke(ptr, size, L"\\r", 2, sizeof(wchar_t));
54 break;
56 case '\n':
57 ptr = mpdm_poke(ptr, size, L"\\n", 2, sizeof(wchar_t));
58 break;
60 case '\t':
61 ptr = mpdm_poke(ptr, size, L"\\t", 2, sizeof(wchar_t));
62 break;
64 case '\\':
65 ptr = mpdm_poke(ptr, size, L"\\\\", 2, sizeof(wchar_t));
66 break;
68 default:
69 ptr = mpdm_poke(ptr, size, iptr, 1, sizeof(wchar_t));
70 break;
72 iptr++;
75 ptr = mpdm_poke(ptr, size, L"\"", 1, sizeof(wchar_t));
77 return ptr;
81 wchar_t *mpsl_dump_1(const mpdm_t v, int l, wchar_t *ptr, int *size)
82 /* dump plugin for mpdm_dump() */
84 int n;
86 /* indent */
87 for (n = 0; n < l; n++)
88 ptr = mpdm_poke(ptr, size, L" ", 2, sizeof(wchar_t));
90 if (v == NULL)
91 ptr = mpdm_poke(ptr, size, L"NULL", 4, sizeof(wchar_t));
92 else
93 if (MPDM_IS_EXEC(v)) {
94 ptr = mpdm_poke(ptr, size, L"sub { 1; }", 10, sizeof(wchar_t));
96 else
97 if (MPDM_IS_HASH(v)) {
98 mpdm_t a = mpdm_keys(v);
100 ptr = mpdm_poke(ptr, size, L"{\n", 2, sizeof(wchar_t));
102 for (n = 0; n < mpdm_size(a); n++) {
103 mpdm_t k = mpdm_aget(a, n);
104 mpdm_t w = mpdm_hget(v, k);
106 ptr = mpsl_dump_1(k, l + 1, ptr, size);
107 ptr = mpdm_poke(ptr, size, L" => ", 4, sizeof(wchar_t));
108 ptr = mpsl_dump_1(w, l + 1, ptr, size);
110 if (n < mpdm_size(a) - 1)
111 ptr = mpdm_poke(ptr, size, L",", 1, sizeof(wchar_t));
113 ptr = mpdm_poke(ptr, size, L"\n", 1, sizeof(wchar_t));
116 /* re-indent */
117 for (n = 0; n < l; n++)
118 ptr = mpdm_poke(ptr, size, L" ", 2, sizeof(wchar_t));
120 ptr = mpdm_poke(ptr, size, L"}", 1, sizeof(wchar_t));
122 else
123 if (MPDM_IS_ARRAY(v)) {
124 ptr = mpdm_poke(ptr, size, L"[\n", 2, sizeof(wchar_t));
126 for (n = 0; n < mpdm_size(v); n++) {
127 ptr = mpsl_dump_1(mpdm_aget(v, n), l + 1, ptr, size);
129 if (n < mpdm_size(v) - 1)
130 ptr = mpdm_poke(ptr, size, L",", 1, sizeof(wchar_t));
132 ptr = mpdm_poke(ptr, size, L"\n", 1, sizeof(wchar_t));
135 /* re-indent */
136 for (n = 0; n < l; n++)
137 ptr = mpdm_poke(ptr, size, L" ", 2, sizeof(wchar_t));
139 ptr = mpdm_poke(ptr, size, L"]", 1, sizeof(wchar_t));
141 else
142 if (MPDM_IS_STRING(v))
143 ptr = dump_string(v, ptr, size);
145 return ptr;