2 * $Id: wildfire.c 6962 2009-01-18 19:33:45Z dothebart $
5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
12 #include <sys/types.h>
21 #include "libcitadel.h"
39 void DeleteJSONValue(void *vJsonValue
)
41 JsonValue
*Val
= (JsonValue
*) vJsonValue
;
42 FreeStrBuf(&Val
->Name
);
43 FreeStrBuf(&Val
->Value
);
44 DeleteHash(&Val
->SubValues
);
48 JsonValue
*NewJsonObject(const char *Key
, long keylen
)
52 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
53 memset(Ret
, 0, sizeof(JsonValue
));
54 Ret
->Type
= JSON_OBJECT
;
56 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
57 Ret
->SubValues
= NewHash(1, NULL
);
61 JsonValue
*NewJsonArray(const char *Key
, long keylen
)
65 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
66 memset(Ret
, 0, sizeof(JsonValue
));
67 Ret
->Type
= JSON_ARRAY
;
69 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
70 Ret
->SubValues
= NewHash(1, Flathash
);
75 JsonValue
*NewJsonNumber(const char *Key
, long keylen
, long Number
)
79 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
80 memset(Ret
, 0, sizeof(JsonValue
));
83 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
84 Ret
->Value
= NewStrBufPlain(NULL
, 64);
85 StrBufPrintf(Ret
->Value
, "%ld", Number
);
91 JsonValue
*NewJsonBigNumber(const char *Key
, long keylen
, double Number
)
95 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
96 memset(Ret
, 0, sizeof(JsonValue
));
99 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
100 Ret
->Value
= NewStrBufPlain(NULL
, 128);
101 StrBufPrintf(Ret
->Value
, "%f", Number
);
105 JsonValue
*NewJsonString(const char *Key
, long keylen
, StrBuf
*CopyMe
)
109 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
110 memset(Ret
, 0, sizeof(JsonValue
));
111 Ret
->Type
= JSON_STRING
;
113 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
114 Ret
->Value
= NewStrBufDup(CopyMe
);
118 JsonValue
*NewJsonPlainString(const char *Key
, long keylen
, const char *CopyMe
, long len
)
122 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
123 memset(Ret
, 0, sizeof(JsonValue
));
124 Ret
->Type
= JSON_STRING
;
126 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
127 Ret
->Value
= NewStrBufPlain(CopyMe
, len
);
131 JsonValue
*NewJsonNull(const char *Key
, long keylen
)
135 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
136 memset(Ret
, 0, sizeof(JsonValue
));
137 Ret
->Type
= JSON_NULL
;
139 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
140 Ret
->Value
= NewStrBufPlain(HKEY("nulll"));
144 JsonValue
*NewJsonBool(const char *Key
, long keylen
, int value
)
148 Ret
= (JsonValue
*) malloc(sizeof(JsonValue
));
149 memset(Ret
, 0, sizeof(JsonValue
));
150 Ret
->Type
= JSON_BOOL
;
152 Ret
->Name
= NewStrBufPlain(Key
, keylen
);
154 Ret
->Value
= NewStrBufPlain(HKEY("true"));
156 Ret
->Value
= NewStrBufPlain(HKEY("false"));
160 void JsonArrayAppend(JsonValue
*Array
, JsonValue
*Val
)
163 if (Array
->Type
!= JSON_ARRAY
)
164 return; /* todo assert! */
166 n
= GetCount(Array
->SubValues
);
167 Put(Array
->SubValues
, (const char*) &n
, sizeof(n
), Val
, DeleteJSONValue
);
170 void JsonObjectAppend(JsonValue
*Array
, JsonValue
*Val
)
172 if ((Array
->Type
!= JSON_OBJECT
) || (Val
->Name
== NULL
))
173 return; /* todo assert! */
175 Put(Array
->SubValues
, SKEY(Val
->Name
), Val
, DeleteJSONValue
);
182 void SerializeJson(StrBuf
*Target
, JsonValue
*Val
, int FreeVal
)
184 void *vValue
, *vPrevious
;
193 StrBufAppendBufPlain(Target
, HKEY("\""), 0);
194 StrECMAEscAppend(Target
, Val
->Value
, NULL
);
195 StrBufAppendBufPlain(Target
, HKEY("\""), 0);
198 StrBufAppendBuf(Target
, Val
->Value
, 0);
201 StrBufAppendBuf(Target
, Val
->Value
, 0);
204 StrBufAppendBuf(Target
, Val
->Value
, 0);
208 StrBufAppendBufPlain(Target
, HKEY("["), 0);
209 It
= GetNewHashPos(Val
->SubValues
, 0);
210 while (GetNextHashPos(Val
->SubValues
,
214 if (vPrevious
!= NULL
)
215 StrBufAppendBufPlain(Target
, HKEY(","), 0);
217 SubVal
= (JsonValue
*) vValue
;
218 SerializeJson(Target
, SubVal
, 0);
221 StrBufAppendBufPlain(Target
, HKEY("]"), 0);
226 StrBufAppendBufPlain(Target
, HKEY("{"), 0);
227 It
= GetNewHashPos(Val
->SubValues
, 0);
228 while (GetNextHashPos(Val
->SubValues
,
232 SubVal
= (JsonValue
*) vValue
;
234 if (vPrevious
!= NULL
) {
235 StrBufAppendBufPlain(Target
, HKEY(","), 0);
237 StrBufAppendBufPlain(Target
, HKEY("\""), 0);
238 StrBufAppendBuf(Target
, SubVal
->Name
, 0);
239 StrBufAppendBufPlain(Target
, HKEY("\":"), 0);
241 SerializeJson(Target
, SubVal
, 0);
244 StrBufAppendBufPlain(Target
, HKEY("}"), 0);
249 DeleteJSONValue(Val
);