forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
152 lines (120 loc) · 3.43 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, University of Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#define TRYPUTC(c, s) if ( Sputcode(c, s) < 0 ) { return -1; }
static int
json_put_code(IOSTREAM *out, int c)
{ static char escape[128];
static int escape_initialized = FALSE;
if ( !escape_initialized )
{ memset(escape, 0, sizeof(escape));
escape['"'] = '"';
escape['\\'] = '\\';
escape['\b'] = 'b';
escape['\f'] = 'f';
escape['\n'] = 'n';
escape['\r'] = 'r';
escape['\t'] = 't';
escape_initialized = TRUE;
}
if ( c < 128 )
{ if ( escape[c] )
{ TRYPUTC('\\', out);
TRYPUTC(escape[c], out);
} else if ( c < ' ' ) /* control characters *must* be escaped */
{ TRYPUTC('\\', out);
if ( Sfprintf(out, "u%04x", c) < 0 )
return -1;
} else
{ TRYPUTC(c, out);
}
} else
{ TRYPUTC(c, out);
}
return 0;
}
#undef TRYPUTC
#define TRYPUTC(c, s) if ( Sputcode(c, s) < 0 ) { rc = FALSE; goto out; }
static foreign_t
json_write_string(term_t stream, term_t text)
{ IOSTREAM *out;
char *a;
pl_wchar_t *w;
size_t len;
int rc = TRUE;
if ( !PL_get_stream_handle(stream, &out) )
return FALSE;
if ( PL_get_nchars(text, &len, &a, CVT_ATOM|CVT_STRING|CVT_LIST) )
{ const char *ap;
size_t todo;
TRYPUTC('"', out);
for(todo=len, ap=a; todo-- > 0; ap++)
{ int c = *ap&0xff;
if ( json_put_code(out, c) < 0 )
{ rc = FALSE; goto out;
}
}
TRYPUTC('"', out);
} else if ( PL_get_wchars(text, &len, &w, CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
{ const pl_wchar_t *wp;
size_t todo;
TRYPUTC('"', out);
for(todo=len, wp=w; todo-- > 0; wp++)
{ int c = *wp;
if ( json_put_code(out, c) < 0 )
{ rc = FALSE; goto out;
}
}
TRYPUTC('"', out);
} else
{ rc = FALSE;
}
out:
PL_release_stream(out);
return rc;
}
static foreign_t
json_write_indent(term_t stream, term_t indent, term_t tab)
{ int i, t, n;
IOSTREAM *out;
if ( !PL_get_integer(indent, &i) ||
!PL_get_integer(tab, &t) )
return FALSE;
if ( PL_get_stream_handle(stream, &out) )
{ int rc = TRUE;
if ( !out->position || out->position->linepos > 0 )
{ TRYPUTC('\n', out);
}
for(n=0; n<i/t; n++)
TRYPUTC('\t', out);
for(n=0; n<i%t; n++)
TRYPUTC(' ', out);
out:
PL_release_stream(out);
return rc;
}
return FALSE;
}
install_t
install_json()
{ PL_register_foreign("json_write_string", 2, json_write_string, 0);
PL_register_foreign("json_write_indent", 3, json_write_indent, 0);
}