struct rspamd_cl_object_s *ov; /**< array or hash */
} value;
enum rspamd_cl_type type; /**< real type */
+ gint ref; /**< reference count */
struct rspamd_cl_object_s *next; /**< array handle */
UT_hash_handle hh; /**< hash handle */
} rspamd_cl_object_t;
gboolean rspamd_cl_parser_add_chunk (struct rspamd_cl_parser *parser, const guchar *data,
gsize len, GError **err);
+/**
+ * Load and add data from a file
+ * @param parser parser structure
+ * @param filename the name of file
+ * @param err if *err is NULL it is set to parser error
+ * @return TRUE if chunk has been added and FALSE in case of error
+ */
+gboolean rspamd_cl_parser_add_file (struct rspamd_cl_parser *parser, const gchar *filename,
+ GError **err);
+
/**
* Get a top object for a parser
* @param parser parser structure
*/
void rspamd_cl_obj_free (rspamd_cl_object_t *obj);
+/**
+ * Icrease reference count for an object
+ * @param obj object to ref
+ */
+static inline rspamd_cl_object_t *
+rspamd_cl_obj_ref (rspamd_cl_object_t *obj) {
+ obj->ref ++;
+ return obj;
+}
+
+/**
+ * Decrease reference count for an object
+ * @param obj object to unref
+ */
+static inline void
+rspamd_cl_obj_unref (rspamd_cl_object_t *obj) {
+ if (--obj->ref <= 0) {
+ rspamd_cl_obj_free (obj);
+ }
+}
+
/**
* Emit object to a string
* @param obj object
rspamd_cl_parser_get_object (struct rspamd_cl_parser *parser, GError **err)
{
if (parser->state != RSPAMD_RCL_STATE_INIT && parser->state != RSPAMD_RCL_STATE_ERROR) {
- return parser->top_obj;
+ return rspamd_cl_obj_ref (parser->top_obj);
}
return NULL;
struct rspamd_cl_pubkey *key, *ktmp;
if (parser->top_obj != NULL) {
- rspamd_cl_obj_free (parser->top_obj);
+ rspamd_cl_obj_unref (parser->top_obj);
}
LL_FOREACH_SAFE (parser->stack, stack, stmp) {
return rspamd_cl_include_url (data, len, parser, TRUE, err);
}
+
+gboolean
+rspamd_cl_parser_add_file (struct rspamd_cl_parser *parser, const gchar *filename,
+ GError **err)
+{
+ guchar *buf;
+ gsize len;
+ gboolean ret;
+
+ if (!rspamd_cl_fetch_file (filename, &buf, &len, err)) {
+ return FALSE;
+ }
+
+ ret = rspamd_cl_parser_add_chunk (parser, buf, len, err);
+
+ munmap (buf, len);
+
+ return ret;
+}
cur = rcl_test_valid;
while (*cur != NULL) {
- parser = rspamd_cl_parser_new ();
+ parser = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
rspamd_cl_pubkey_add (parser, test_pubkey, sizeof (test_pubkey) - 1, &err);
g_assert_no_error (err);
g_assert (parser != NULL);
emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_CONFIG);
g_assert (emitted != NULL);
msg_debug ("got config output: %s", emitted);
- parser2 = rspamd_cl_parser_new ();
+ parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
g_assert (parser2 != NULL);
rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
g_assert_no_error (err);
emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_JSON);
g_assert (emitted != NULL);
msg_debug ("got json output: %s", emitted);
- parser2 = rspamd_cl_parser_new ();
+ parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
g_assert (parser2 != NULL);
rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
g_assert_no_error (err);
emitted = rspamd_cl_object_emit (obj, RSPAMD_CL_EMIT_JSON_COMPACT);
g_assert (emitted != NULL);
msg_debug ("got json compacted output: %s", emitted);
- parser2 = rspamd_cl_parser_new ();
+ parser2 = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
g_assert (parser2 != NULL);
rspamd_cl_parser_add_chunk (parser2, emitted, strlen (emitted), &err);
g_assert_no_error (err);
/* Cleanup */
rspamd_cl_parser_free (parser);
+ rspamd_cl_obj_unref (obj);
cur ++;
}
+ /* Load a big json */
+ parser = rspamd_cl_parser_new (RSPAMD_CL_FLAG_KEY_LOWERCASE);
+ rspamd_cl_parser_add_file (parser, "./rcl_test.json", &err);
+ g_assert_no_error (err);
+ rspamd_cl_parser_free (parser);
}