lnav/src/yajlpp.hh

318 lines
9.9 KiB
C++
Raw Normal View History

2013-05-31 15:01:31 +00:00
/**
* Copyright (c) 2013, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @file yajlpp.hh
*/
#ifndef _yajlpp_hh
#define _yajlpp_hh
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <string>
2013-06-02 21:20:15 +00:00
#include "pcrepp.hh"
2014-10-28 14:02:27 +00:00
#include "intern_string.hh"
2013-06-02 21:20:15 +00:00
2013-05-31 15:01:31 +00:00
#include "yajl/api/yajl_parse.h"
#include "yajl/api/yajl_gen.h"
inline
yajl_gen_status yajl_gen_pstring(yajl_gen hand, const char *str, size_t len)
{
if (len == (size_t)-1) {
len = strlen(str);
}
return yajl_gen_string(hand, (const unsigned char *)str, len);
}
inline
yajl_gen_status yajl_gen_string(yajl_gen hand, const std::string &str)
{
return yajl_gen_string(hand,
(const unsigned char *)str.c_str(),
str.length());
}
struct json_path_handler_base {
2013-06-16 01:07:50 +00:00
json_path_handler_base(const char *path) : jph_path(path), jph_regex(path)
{
2013-05-31 15:01:31 +00:00
memset(&this->jph_callbacks, 0, sizeof(this->jph_callbacks));
};
2013-06-16 01:07:50 +00:00
const char * jph_path;
pcrepp jph_regex;
2013-05-31 15:01:31 +00:00
yajl_callbacks jph_callbacks;
};
2013-07-23 12:55:08 +00:00
class yajlpp_parse_context;
2013-05-31 15:01:31 +00:00
struct json_path_handler : public json_path_handler_base {
2013-07-23 12:55:08 +00:00
json_path_handler(const char *path, int(*null_func)(yajlpp_parse_context *))
2013-06-16 01:07:50 +00:00
: json_path_handler_base(path)
{
2013-07-23 12:55:08 +00:00
this->jph_callbacks.yajl_null = (int (*)(void *))null_func;
2013-06-16 01:07:50 +00:00
};
2013-05-31 15:01:31 +00:00
2013-07-23 12:55:08 +00:00
json_path_handler(const char *path, int(*bool_func)(yajlpp_parse_context *, int))
2013-06-16 01:07:50 +00:00
: json_path_handler_base(path)
{
2013-07-23 12:55:08 +00:00
this->jph_callbacks.yajl_boolean = (int (*)(void *, int))bool_func;
2013-06-16 01:07:50 +00:00
}
2013-05-31 15:01:31 +00:00
2013-07-23 12:55:08 +00:00
json_path_handler(const char *path, int(*int_func)(yajlpp_parse_context *, long long))
2013-06-16 01:07:50 +00:00
: json_path_handler_base(path)
{
2013-07-23 12:55:08 +00:00
this->jph_callbacks.yajl_integer = (int (*)(void *, long long))int_func;
2013-06-16 01:07:50 +00:00
}
2013-05-31 15:01:31 +00:00
2013-07-23 12:55:08 +00:00
json_path_handler(const char *path, int(*double_func)(yajlpp_parse_context *, double))
2013-06-16 01:07:50 +00:00
: json_path_handler_base(path)
{
2013-07-23 12:55:08 +00:00
this->jph_callbacks.yajl_double = (int (*)(void *, double))double_func;
2013-06-16 01:07:50 +00:00
}
2013-05-31 15:01:31 +00:00
2013-06-16 01:07:50 +00:00
json_path_handler(const char *path,
2013-07-23 12:55:08 +00:00
int(*str_func)(yajlpp_parse_context *, const unsigned char *, size_t))
2013-06-16 01:07:50 +00:00
: json_path_handler_base(path)
{
2013-07-23 12:55:08 +00:00
this->jph_callbacks.yajl_string = (int (*)(void *, const unsigned char *, size_t))str_func;
2013-06-16 01:07:50 +00:00
}
2013-05-31 15:01:31 +00:00
json_path_handler(const char *path) : json_path_handler_base(path) { };
2013-06-16 01:07:50 +00:00
json_path_handler() : json_path_handler_base("") {};
json_path_handler &add_cb(int(*null_func)(yajlpp_parse_context *)) {
this->jph_callbacks.yajl_null = (int (*)(void *))null_func;
return *this;
};
json_path_handler &add_cb(int(*bool_func)(yajlpp_parse_context *, int))
{
this->jph_callbacks.yajl_boolean = (int (*)(void *, int))bool_func;
return *this;
}
json_path_handler &add_cb(int(*int_func)(yajlpp_parse_context *, long long))
{
this->jph_callbacks.yajl_integer = (int (*)(void *, long long))int_func;
return *this;
}
json_path_handler &add_cb(int(*double_func)(yajlpp_parse_context *, double))
{
this->jph_callbacks.yajl_double = (int (*)(void *, double))double_func;
return *this;
}
json_path_handler &add_cb(int(*str_func)(yajlpp_parse_context *, const unsigned char *, size_t))
{
this->jph_callbacks.yajl_string = (int (*)(void *, const unsigned char *, size_t))str_func;
return *this;
}
2013-05-31 15:01:31 +00:00
};
class yajlpp_parse_context {
public:
2013-07-23 12:55:08 +00:00
yajlpp_parse_context(std::string source,
struct json_path_handler *handlers = NULL)
: ypc_source(source), ypc_handlers(handlers), ypc_ignore_unused(false)
2013-06-16 01:07:50 +00:00
{
this->ypc_path.reserve(4096);
this->ypc_path.push_back('\0');
2013-05-31 15:01:31 +00:00
this->ypc_callbacks = DEFAULT_CALLBACKS;
memset(&this->ypc_alt_callbacks, 0, sizeof(this->ypc_alt_callbacks));
2013-05-31 15:01:31 +00:00
};
2014-10-28 14:02:27 +00:00
void get_path_fragment(int offset, const char **frag, size_t &len_out) const {
2013-05-31 15:01:31 +00:00
size_t start, end;
if (offset < 0) {
offset = this->ypc_path_index_stack.size() + offset;
}
start = this->ypc_path_index_stack[offset] + 1;
2013-06-16 01:07:50 +00:00
if ((offset + 1) < (int)this->ypc_path_index_stack.size()) {
2013-05-31 15:01:31 +00:00
end = this->ypc_path_index_stack[offset + 1];
2013-06-16 01:07:50 +00:00
}
else{
end = this->ypc_path.size() - 1;
2013-06-16 01:07:50 +00:00
}
2014-10-28 14:02:27 +00:00
*frag = &this->ypc_path[start];
len_out = end - start;
}
const intern_string_t get_path_fragment_i(int offset) const {
const char *frag;
size_t len;
this->get_path_fragment(offset, &frag, len);
return intern_string::lookup(frag, len);
};
std::string get_path_fragment(int offset) const
{
const char *frag;
size_t len;
this->get_path_fragment(offset, &frag, len);
return std::string(frag, len);
};
2015-04-25 19:15:51 +00:00
const intern_string_t get_path() const {
return intern_string::lookup(&this->ypc_path[1],
this->ypc_path.size() - 2);
};
2014-10-28 14:02:27 +00:00
bool is_level(size_t level) const {
return this->ypc_path_index_stack.size() == level;
2013-05-31 15:01:31 +00:00
};
void reset(struct json_path_handler *handlers) {
this->ypc_handlers = handlers;
this->ypc_path.clear();
this->ypc_path.push_back('\0');
this->ypc_path_index_stack.clear();
this->ypc_array_index.clear();
this->ypc_callbacks = DEFAULT_CALLBACKS;
memset(&this->ypc_alt_callbacks, 0, sizeof(this->ypc_alt_callbacks));
}
2014-10-28 14:02:27 +00:00
void set_static_handler(struct json_path_handler &jph) {
this->ypc_path.clear();
this->ypc_path.push_back('\0');
this->ypc_path_index_stack.clear();
this->ypc_array_index.clear();
if (jph.jph_callbacks.yajl_null != NULL)
this->ypc_callbacks.yajl_null = jph.jph_callbacks.yajl_null;
if (jph.jph_callbacks.yajl_boolean != NULL)
this->ypc_callbacks.yajl_boolean = jph.jph_callbacks.yajl_boolean;
if (jph.jph_callbacks.yajl_integer != NULL)
this->ypc_callbacks.yajl_integer = jph.jph_callbacks.yajl_integer;
if (jph.jph_callbacks.yajl_double != NULL)
this->ypc_callbacks.yajl_double = jph.jph_callbacks.yajl_double;
if (jph.jph_callbacks.yajl_string != NULL)
this->ypc_callbacks.yajl_string = jph.jph_callbacks.yajl_string;
}
2013-07-23 12:55:08 +00:00
const std::string ypc_source;
2013-05-31 15:01:31 +00:00
struct json_path_handler *ypc_handlers;
2013-06-16 01:07:50 +00:00
void * ypc_userdata;
yajl_callbacks ypc_callbacks;
yajl_callbacks ypc_alt_callbacks;
std::vector<char> ypc_path;
2013-06-16 01:07:50 +00:00
std::vector<size_t> ypc_path_index_stack;
std::vector<int> ypc_array_index;
2013-06-02 21:20:15 +00:00
pcre_context_static<30> ypc_pcre_context;
bool ypc_ignore_unused;
2013-05-31 15:01:31 +00:00
private:
static const yajl_callbacks DEFAULT_CALLBACKS;
void update_callbacks(void);
static int map_start(void *ctx);
static int map_key(void *ctx, const unsigned char *key, size_t len);
static int map_end(void *ctx);
static int array_start(void *ctx);
static int array_end(void *ctx);
2013-07-23 12:55:08 +00:00
static int handle_unused(void *ctx);
2013-05-31 15:01:31 +00:00
};
class yajlpp_generator {
public:
yajlpp_generator(yajl_gen handle) : yg_handle(handle) { };
2013-06-16 01:07:50 +00:00
void operator()(const std::string &str)
{
2013-05-31 15:01:31 +00:00
yajl_gen_string(this->yg_handle, str);
};
void operator()(const char *str)
{
yajl_gen_string(this->yg_handle, (const unsigned char *)str, strlen(str));
};
void operator()(const char *str, size_t len)
{
yajl_gen_string(this->yg_handle, (const unsigned char *)str, len);
};
2013-06-16 01:07:50 +00:00
void operator()(long long value)
{
2013-05-31 15:01:31 +00:00
yajl_gen_integer(this->yg_handle, value);
};
void operator()(bool value)
{
yajl_gen_bool(this->yg_handle, value);
};
void operator()()
{
yajl_gen_null(this->yg_handle);
};
2013-05-31 15:01:31 +00:00
private:
yajl_gen yg_handle;
};
class yajlpp_container_base {
public:
yajlpp_container_base(yajl_gen handle)
2013-06-16 01:07:50 +00:00
: gen(handle), ycb_handle(handle) {};
2013-05-31 15:01:31 +00:00
yajlpp_generator gen;
protected:
yajl_gen ycb_handle;
};
class yajlpp_map : public yajlpp_container_base {
public:
2013-06-16 01:07:50 +00:00
yajlpp_map(yajl_gen handle) : yajlpp_container_base(handle)
{
2013-05-31 15:01:31 +00:00
yajl_gen_map_open(handle);
};
~yajlpp_map() { yajl_gen_map_close(this->ycb_handle); };
};
class yajlpp_array : public yajlpp_container_base {
public:
2013-06-16 01:07:50 +00:00
yajlpp_array(yajl_gen handle) : yajlpp_container_base(handle)
{
2013-05-31 15:01:31 +00:00
yajl_gen_array_open(handle);
};
~yajlpp_array() { yajl_gen_array_close(this->ycb_handle); };
};
#endif